| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | #include <com/sun/star/io/XOutputStream.hpp> |
| 21 | |
| 22 | |
| 23 | #include <comphelper/seekableinput.hxx> |
| 24 | |
| 25 | using namespace ::com::sun::star; |
| 26 | |
| 27 | namespace comphelper |
| 28 | { |
| 29 | |
| 30 | const sal_Int32 nConstBufferSize = 32000; |
| 31 | |
| 32 | |
| 33 | void copyInputToOutput_Impl( const uno::Reference< io::XInputStream >& xIn, |
| 34 | const uno::Reference< io::XOutputStream >& xOut ) |
| 35 | { |
| 36 | sal_Int32 nRead; |
| 37 | uno::Sequence< sal_Int8 > aSequence( nConstBufferSize ); |
| 38 | |
| 39 | do |
| 40 | { |
| 41 | nRead = xIn->readBytes( aSequence, nConstBufferSize ); |
| 42 | if ( nRead < nConstBufferSize ) |
| 43 | { |
| 44 | uno::Sequence< sal_Int8 > aTempBuf( aSequence.getConstArray(), nRead ); |
| 45 | xOut->writeBytes( aTempBuf ); |
| 46 | } |
| 47 | else |
| 48 | xOut->writeBytes( aSequence ); |
| 49 | } |
| 50 | while ( nRead == nConstBufferSize ); |
| 51 | } |
| 52 | |
| 53 | |
| 54 | OSeekableInputWrapper::OSeekableInputWrapper( |
| 55 | const uno::Reference< io::XInputStream >& xInStream, |
| 56 | const uno::Reference< lang::XMultiServiceFactory >& xFactory ) |
| 57 | : m_xFactory( xFactory ) |
| 58 | , m_xOriginalStream( xInStream ) |
| 59 | { |
| 60 | if ( !m_xFactory.is() ) |
| 61 | throw uno::RuntimeException(); |
| 62 | } |
| 63 | |
| 64 | |
| 65 | OSeekableInputWrapper::~OSeekableInputWrapper() |
| 66 | { |
| 67 | } |
| 68 | |
| 69 | |
| 70 | uno::Reference< io::XInputStream > OSeekableInputWrapper::CheckSeekableCanWrap( |
| 71 | const uno::Reference< io::XInputStream >& xInStream, |
| 72 | const uno::Reference< lang::XMultiServiceFactory >& xFactory ) |
| 73 | { |
| 74 | |
| 75 | uno::Reference< io::XSeekable > xSeek( xInStream, uno::UNO_QUERY ); |
| 76 | if ( xSeek.is() ) |
| 77 | return xInStream; |
| 78 | |
| 79 | uno::Reference< io::XInputStream > xNewStream( |
| 80 | static_cast< io::XInputStream* >( |
| 81 | new OSeekableInputWrapper( xInStream, xFactory ) ) ); |
| 82 | return xNewStream; |
| 83 | } |
| 84 | |
| 85 | |
| 86 | void OSeekableInputWrapper::PrepareCopy_Impl() |
| 87 | { |
| 88 | if ( !m_xCopyInput.is() ) |
| 89 | { |
| 90 | if ( !m_xFactory.is() ) |
| 91 | throw uno::RuntimeException(); |
| 92 | |
| 93 | uno::Reference< io::XOutputStream > xTempOut( |
| 94 | m_xFactory->createInstance( ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.io.TempFile")(&("com.sun.star.io.TempFile")[0]), ((sal_Int32)((sizeof ( "com.sun.star.io.TempFile") / sizeof (("com.sun.star.io.TempFile" )[0]))-1)), (((rtl_TextEncoding) 11))) ), |
| 95 | uno::UNO_QUERY ); |
| 96 | |
| 97 | if ( xTempOut.is() ) |
| 98 | { |
| 99 | copyInputToOutput_Impl( m_xOriginalStream, xTempOut ); |
| 100 | xTempOut->closeOutput(); |
| 101 | |
| 102 | uno::Reference< io::XSeekable > xTempSeek( xTempOut, uno::UNO_QUERY ); |
| 103 | if ( xTempSeek.is() ) |
| 104 | { |
| 105 | xTempSeek->seek( 0 ); |
| 106 | m_xCopyInput = uno::Reference< io::XInputStream >( xTempOut, uno::UNO_QUERY ); |
| 107 | if ( m_xCopyInput.is() ) |
| 108 | m_xCopySeek = xTempSeek; |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | if ( !m_xCopyInput.is() ) |
| 114 | throw io::IOException(); |
| 115 | } |
| 116 | |
| 117 | |
| 118 | |
| 119 | sal_Int32 SAL_CALL OSeekableInputWrapper::readBytes( uno::Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead ) |
| 120 | throw ( io::NotConnectedException, |
| 121 | io::BufferSizeExceededException, |
| 122 | io::IOException, |
| 123 | uno::RuntimeException ) |
| 124 | { |
| 125 | ::osl::MutexGuard aGuard( m_aMutex ); |
| 126 | |
| 127 | if ( !m_xOriginalStream.is() ) |
| 128 | throw io::NotConnectedException(); |
| 129 | |
| 130 | PrepareCopy_Impl(); |
| 131 | |
| 132 | return m_xCopyInput->readBytes( aData, nBytesToRead ); |
| 133 | } |
| 134 | |
| 135 | |
| 136 | sal_Int32 SAL_CALL OSeekableInputWrapper::readSomeBytes( uno::Sequence< sal_Int8 >& aData, sal_Int32 nMaxBytesToRead ) |
| 137 | throw ( io::NotConnectedException, |
| 138 | io::BufferSizeExceededException, |
| 139 | io::IOException, |
| 140 | uno::RuntimeException ) |
| 141 | { |
| 142 | ::osl::MutexGuard aGuard( m_aMutex ); |
| 143 | |
| 144 | if ( !m_xOriginalStream.is() ) |
| |
| 145 | throw io::NotConnectedException(); |
| 146 | |
| 147 | PrepareCopy_Impl(); |
| 148 | |
| 149 | return m_xCopyInput->readSomeBytes( aData, nMaxBytesToRead ); |
| 2 | | Called C++ object pointer is null |
|
| 150 | } |
| 151 | |
| 152 | |
| 153 | void SAL_CALL OSeekableInputWrapper::skipBytes( sal_Int32 nBytesToSkip ) |
| 154 | throw ( io::NotConnectedException, |
| 155 | io::BufferSizeExceededException, |
| 156 | io::IOException, |
| 157 | uno::RuntimeException ) |
| 158 | { |
| 159 | ::osl::MutexGuard aGuard( m_aMutex ); |
| 160 | |
| 161 | if ( !m_xOriginalStream.is() ) |
| 162 | throw io::NotConnectedException(); |
| 163 | |
| 164 | PrepareCopy_Impl(); |
| 165 | |
| 166 | m_xCopyInput->skipBytes( nBytesToSkip ); |
| 167 | } |
| 168 | |
| 169 | |
| 170 | sal_Int32 SAL_CALL OSeekableInputWrapper::available() |
| 171 | throw ( io::NotConnectedException, |
| 172 | io::IOException, |
| 173 | uno::RuntimeException ) |
| 174 | { |
| 175 | ::osl::MutexGuard aGuard( m_aMutex ); |
| 176 | |
| 177 | if ( !m_xOriginalStream.is() ) |
| 178 | throw io::NotConnectedException(); |
| 179 | |
| 180 | PrepareCopy_Impl(); |
| 181 | |
| 182 | return m_xCopyInput->available(); |
| 183 | } |
| 184 | |
| 185 | |
| 186 | void SAL_CALL OSeekableInputWrapper::closeInput() |
| 187 | throw ( io::NotConnectedException, |
| 188 | io::IOException, |
| 189 | uno::RuntimeException ) |
| 190 | { |
| 191 | ::osl::MutexGuard aGuard( m_aMutex ); |
| 192 | |
| 193 | if ( !m_xOriginalStream.is() ) |
| 194 | throw io::NotConnectedException(); |
| 195 | |
| 196 | m_xOriginalStream->closeInput(); |
| 197 | m_xOriginalStream = uno::Reference< io::XInputStream >(); |
| 198 | |
| 199 | if ( m_xCopyInput.is() ) |
| 200 | { |
| 201 | m_xCopyInput->closeInput(); |
| 202 | m_xCopyInput = uno::Reference< io::XInputStream >(); |
| 203 | } |
| 204 | |
| 205 | m_xCopySeek = uno::Reference< io::XSeekable >(); |
| 206 | } |
| 207 | |
| 208 | |
| 209 | |
| 210 | |
| 211 | void SAL_CALL OSeekableInputWrapper::seek( sal_Int64 location ) |
| 212 | throw ( lang::IllegalArgumentException, |
| 213 | io::IOException, |
| 214 | uno::RuntimeException ) |
| 215 | { |
| 216 | ::osl::MutexGuard aGuard( m_aMutex ); |
| 217 | |
| 218 | if ( !m_xOriginalStream.is() ) |
| 219 | throw io::NotConnectedException(); |
| 220 | |
| 221 | PrepareCopy_Impl(); |
| 222 | |
| 223 | m_xCopySeek->seek( location ); |
| 224 | } |
| 225 | |
| 226 | |
| 227 | sal_Int64 SAL_CALL OSeekableInputWrapper::getPosition() |
| 228 | throw ( io::IOException, |
| 229 | uno::RuntimeException ) |
| 230 | { |
| 231 | ::osl::MutexGuard aGuard( m_aMutex ); |
| 232 | |
| 233 | if ( !m_xOriginalStream.is() ) |
| 234 | throw io::NotConnectedException(); |
| 235 | |
| 236 | PrepareCopy_Impl(); |
| 237 | |
| 238 | return m_xCopySeek->getPosition(); |
| 239 | } |
| 240 | |
| 241 | |
| 242 | sal_Int64 SAL_CALL OSeekableInputWrapper::getLength() |
| 243 | throw ( io::IOException, |
| 244 | uno::RuntimeException ) |
| 245 | { |
| 246 | ::osl::MutexGuard aGuard( m_aMutex ); |
| 247 | |
| 248 | if ( !m_xOriginalStream.is() ) |
| 249 | throw io::NotConnectedException(); |
| 250 | |
| 251 | PrepareCopy_Impl(); |
| 252 | |
| 253 | return m_xCopySeek->getLength(); |
| 254 | } |
| 255 | |
| 256 | } |
| 257 | |
| 258 | |