1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 22 23 24 // MARKER(update_precomp.py): autogen include statement, do not remove 25 #include "precompiled_unotools.hxx" 26 #include <unotools/streamhelper.hxx> 27 #include <tools/debug.hxx> 28 29 namespace utl 30 { 31 32 //------------------------------------------------------------------------------ 33 void SAL_CALL OInputStreamHelper::acquire() throw () 34 { 35 InputStreamHelper_Base::acquire(); 36 } 37 38 //------------------------------------------------------------------------------ 39 void SAL_CALL OInputStreamHelper::release() throw () 40 { 41 InputStreamHelper_Base::release(); 42 } 43 44 //------------------------------------------------------------------------------ 45 sal_Int32 SAL_CALL OInputStreamHelper::readBytes(staruno::Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead) 46 { 47 if (!m_xLockBytes.Is()) 48 throw stario::NotConnectedException(::rtl::OUString(), static_cast<staruno::XWeak*>(this)); 49 50 if (nBytesToRead < 0) 51 throw stario::BufferSizeExceededException(::rtl::OUString(), static_cast<staruno::XWeak*>(this)); 52 53 ::osl::MutexGuard aGuard( m_aMutex ); 54 aData.realloc(nBytesToRead); 55 56 sal_Size nRead; 57 ErrCode nError = m_xLockBytes->ReadAt(m_nActPos, (void*)aData.getArray(), nBytesToRead, &nRead); 58 // FIXME nRead could be truncated on 64-bit arches 59 m_nActPos += (sal_uInt32)nRead; 60 61 if (nError != ERRCODE_NONE) 62 throw stario::IOException(::rtl::OUString(), static_cast<staruno::XWeak*>(this)); 63 64 // adjust sequence if data read is lower than the desired data 65 if (nRead < (sal_uInt32)nBytesToRead) 66 aData.realloc( nRead ); 67 68 return nRead; 69 } 70 71 void SAL_CALL OInputStreamHelper::seek( sal_Int64 location ) 72 { 73 ::osl::MutexGuard aGuard( m_aMutex ); 74 // cast is truncating, but position would be truncated as soon as 75 // put into SvLockBytes anyway 76 m_nActPos = sal::static_int_cast<sal_uInt32>(location); 77 } 78 79 sal_Int64 SAL_CALL OInputStreamHelper::getPosition( ) 80 { 81 return m_nActPos; 82 } 83 84 sal_Int64 SAL_CALL OInputStreamHelper::getLength( ) 85 { 86 if (!m_xLockBytes.Is()) 87 return 0; 88 89 ::osl::MutexGuard aGuard( m_aMutex ); 90 SvLockBytesStat aStat; 91 m_xLockBytes->Stat( &aStat, SVSTATFLAG_DEFAULT ); 92 return aStat.nSize; 93 } 94 95 //------------------------------------------------------------------------------ 96 sal_Int32 SAL_CALL OInputStreamHelper::readSomeBytes(staruno::Sequence< sal_Int8 >& aData, 97 sal_Int32 nMaxBytesToRead) 98 { 99 // read all data desired 100 return readBytes(aData, nMaxBytesToRead); 101 } 102 103 //------------------------------------------------------------------------------ 104 void SAL_CALL OInputStreamHelper::skipBytes(sal_Int32 nBytesToSkip) 105 { 106 ::osl::MutexGuard aGuard( m_aMutex ); 107 if (!m_xLockBytes.Is()) 108 throw stario::NotConnectedException(::rtl::OUString(), static_cast<staruno::XWeak*>(this)); 109 110 if (nBytesToSkip < 0) 111 throw stario::BufferSizeExceededException(::rtl::OUString(), static_cast<staruno::XWeak*>(this)); 112 113 m_nActPos += nBytesToSkip; 114 } 115 116 //------------------------------------------------------------------------------ 117 sal_Int32 SAL_CALL OInputStreamHelper::available() 118 { 119 ::osl::MutexGuard aGuard( m_aMutex ); 120 if (!m_xLockBytes.Is()) 121 throw stario::NotConnectedException(::rtl::OUString(), static_cast<staruno::XWeak*>(this)); 122 123 return m_nAvailable; 124 } 125 126 //------------------------------------------------------------------------------ 127 void SAL_CALL OInputStreamHelper::closeInput() 128 { 129 ::osl::MutexGuard aGuard( m_aMutex ); 130 if (!m_xLockBytes.Is()) 131 throw stario::NotConnectedException(::rtl::OUString(), static_cast<staruno::XWeak*>(this)); 132 133 m_xLockBytes = NULL; 134 } 135 136 /*************************************************************************/ 137 //------------------------------------------------------------------------------ 138 void SAL_CALL OOutputStreamHelper::acquire() throw () 139 { 140 OutputStreamHelper_Base::acquire(); 141 } 142 143 //------------------------------------------------------------------------------ 144 void SAL_CALL OOutputStreamHelper::release() throw () 145 { 146 OutputStreamHelper_Base::release(); 147 } 148 // stario::XOutputStream 149 //------------------------------------------------------------------------------ 150 void SAL_CALL OOutputStreamHelper::writeBytes(const staruno::Sequence< sal_Int8 >& aData) 151 { 152 ::osl::MutexGuard aGuard( m_aMutex ); 153 if (!m_xLockBytes.Is()) 154 throw stario::NotConnectedException(::rtl::OUString(), static_cast<staruno::XWeak*>(this)); 155 156 sal_Size nWritten; 157 ErrCode nError = m_xLockBytes->WriteAt( m_nActPos, aData.getConstArray(), aData.getLength(), &nWritten ); 158 // FIXME nWritten could be truncated on 64-bit arches 159 m_nActPos += (sal_uInt32)nWritten; 160 161 if (nError != ERRCODE_NONE || 162 sal::static_int_cast<sal_Int32>(nWritten) != aData.getLength()) 163 { 164 throw stario::IOException(::rtl::OUString(),static_cast<staruno::XWeak*>(this)); 165 } 166 } 167 168 //------------------------------------------------------------------ 169 void SAL_CALL OOutputStreamHelper::flush() 170 { 171 ::osl::MutexGuard aGuard( m_aMutex ); 172 if (!m_xLockBytes.Is()) 173 throw stario::NotConnectedException(::rtl::OUString(), static_cast<staruno::XWeak*>(this)); 174 175 ErrCode nError = m_xLockBytes->Flush(); 176 if (nError != ERRCODE_NONE) 177 throw stario::IOException(::rtl::OUString(),static_cast<staruno::XWeak*>(this)); 178 } 179 180 //------------------------------------------------------------------ 181 void SAL_CALL OOutputStreamHelper::closeOutput( ) 182 { 183 ::osl::MutexGuard aGuard( m_aMutex ); 184 if (!m_xLockBytes.Is()) 185 throw stario::NotConnectedException(::rtl::OUString(), static_cast<staruno::XWeak*>(this)); 186 187 m_xLockBytes = NULL; 188 } 189 190 } // namespace utl 191