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_comphelper.hxx" 26 27 #include "comphelper_module.hxx" 28 29 #include <com/sun/star/io/XStream.hpp> 30 #include <com/sun/star/io/XSeekableInputStream.hpp> 31 #include <com/sun/star/io/XTruncate.hpp> 32 #include <com/sun/star/uno/XComponentContext.hpp> 33 #include <cppuhelper/implbase4.hxx> 34 35 #include <string.h> 36 #include <vector> 37 38 using ::rtl::OUString; 39 using ::cppu::OWeakObject; 40 using ::cppu::WeakImplHelper4; 41 using namespace ::com::sun::star::io; 42 using namespace ::com::sun::star::uno; 43 using namespace ::com::sun::star::lang; 44 using namespace ::osl; 45 46 namespace comphelper 47 { 48 49 class UNOMemoryStream : public WeakImplHelper4 < XStream, XSeekableInputStream, XOutputStream, XTruncate > 50 { 51 public: 52 UNOMemoryStream(); 53 virtual ~UNOMemoryStream(); 54 55 // XStream 56 virtual Reference< XInputStream > SAL_CALL getInputStream( ) throw (RuntimeException); 57 virtual Reference< XOutputStream > SAL_CALL getOutputStream( ) throw (RuntimeException); 58 59 // XInputStream 60 virtual sal_Int32 SAL_CALL readBytes( Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead ) throw (NotConnectedException, BufferSizeExceededException, IOException, RuntimeException); 61 virtual sal_Int32 SAL_CALL readSomeBytes( Sequence< sal_Int8 >& aData, sal_Int32 nMaxBytesToRead ) throw (NotConnectedException, BufferSizeExceededException, IOException, RuntimeException); 62 virtual void SAL_CALL skipBytes( sal_Int32 nBytesToSkip ) throw (NotConnectedException, BufferSizeExceededException, IOException, RuntimeException); 63 virtual sal_Int32 SAL_CALL available() throw (NotConnectedException, IOException, RuntimeException); 64 virtual void SAL_CALL closeInput() throw (NotConnectedException, IOException, RuntimeException); 65 66 // XSeekable 67 virtual void SAL_CALL seek( sal_Int64 location ) throw (IllegalArgumentException, IOException, RuntimeException); 68 virtual sal_Int64 SAL_CALL getPosition() throw (IOException, RuntimeException); 69 virtual sal_Int64 SAL_CALL getLength() throw (IOException, RuntimeException); 70 71 // XOutputStream 72 virtual void SAL_CALL writeBytes( const Sequence< sal_Int8 >& aData ) throw (NotConnectedException, BufferSizeExceededException, IOException, RuntimeException); 73 virtual void SAL_CALL flush() throw (NotConnectedException, BufferSizeExceededException, IOException, RuntimeException); 74 virtual void SAL_CALL closeOutput() throw (NotConnectedException, BufferSizeExceededException, IOException, RuntimeException); 75 76 // XTruncate 77 virtual void SAL_CALL truncate() throw (::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException); 78 79 // XServiceInfo - static versions (used for component registration) 80 static ::rtl::OUString SAL_CALL getImplementationName_static(); 81 static Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames_static(); 82 static Reference< XInterface > SAL_CALL Create( const Reference< ::com::sun::star::uno::XComponentContext >& ); 83 84 private: 85 std::vector< sal_Int8 > maData; 86 sal_Int32 mnCursor; 87 }; 88 89 UNOMemoryStream::UNOMemoryStream() 90 : mnCursor(0) 91 { 92 } 93 94 UNOMemoryStream::~UNOMemoryStream() 95 { 96 } 97 98 // XStream 99 Reference< XInputStream > SAL_CALL UNOMemoryStream::getInputStream( ) throw (RuntimeException) 100 { 101 return this; 102 } 103 104 Reference< XOutputStream > SAL_CALL UNOMemoryStream::getOutputStream( ) throw (RuntimeException) 105 { 106 return this; 107 } 108 109 // XInputStream 110 sal_Int32 SAL_CALL UNOMemoryStream::readBytes( Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead ) throw (NotConnectedException, BufferSizeExceededException, IOException, RuntimeException) 111 { 112 if( nBytesToRead < 0 ) 113 throw IOException(); 114 115 nBytesToRead = std::min( nBytesToRead, available() ); 116 aData.realloc( nBytesToRead ); 117 118 if( nBytesToRead ) 119 { 120 sal_Int8* pData = static_cast<sal_Int8*>(&(*maData.begin())); 121 sal_Int8* pCursor = &((pData)[mnCursor]); 122 memcpy( (void*)aData.getArray(), (void*)pCursor, nBytesToRead ); 123 124 mnCursor += nBytesToRead; 125 } 126 127 return nBytesToRead; 128 } 129 130 sal_Int32 SAL_CALL UNOMemoryStream::readSomeBytes( Sequence< sal_Int8 >& aData, sal_Int32 nMaxBytesToRead ) throw (NotConnectedException, BufferSizeExceededException, IOException, RuntimeException) 131 { 132 return readBytes( aData, nMaxBytesToRead ); 133 } 134 135 void SAL_CALL UNOMemoryStream::skipBytes( sal_Int32 nBytesToSkip ) throw (NotConnectedException, BufferSizeExceededException, IOException, RuntimeException) 136 { 137 if( nBytesToSkip < 0 ) 138 throw IOException(); 139 140 mnCursor += std::min( nBytesToSkip, available() ); 141 } 142 143 sal_Int32 SAL_CALL UNOMemoryStream::available() throw (NotConnectedException, IOException, RuntimeException) 144 { 145 return static_cast< sal_Int32 >( maData.size() ) - mnCursor; 146 } 147 148 void SAL_CALL UNOMemoryStream::closeInput() throw (NotConnectedException, IOException, RuntimeException) 149 { 150 mnCursor = 0; 151 } 152 153 // XSeekable 154 void SAL_CALL UNOMemoryStream::seek( sal_Int64 location ) throw (IllegalArgumentException, IOException, RuntimeException) 155 { 156 if( (location < 0) || (location > SAL_MAX_INT32) ) 157 throw IllegalArgumentException( OUString(RTL_CONSTASCII_USTRINGPARAM("this implementation does not support more than 2GB!")), Reference< XInterface >(static_cast<OWeakObject*>(this)), 0 ); 158 159 // seek operation should be able to resize the stream 160 if ( location > static_cast< sal_Int64 >( maData.size() ) ) 161 maData.resize( static_cast< sal_Int32 >( location ) ); 162 163 if ( location > static_cast< sal_Int64 >( maData.size() ) ) 164 maData.resize( static_cast< sal_Int32 >( location ) ); 165 166 mnCursor = static_cast< sal_Int32 >( location ); 167 } 168 169 sal_Int64 SAL_CALL UNOMemoryStream::getPosition() throw (IOException, RuntimeException) 170 { 171 return static_cast< sal_Int64 >( mnCursor ); 172 } 173 174 sal_Int64 SAL_CALL UNOMemoryStream::getLength() throw (IOException, RuntimeException) 175 { 176 return static_cast< sal_Int64 >( maData.size() ); 177 } 178 179 // XOutputStream 180 void SAL_CALL UNOMemoryStream::writeBytes( const Sequence< sal_Int8 >& aData ) throw (NotConnectedException, BufferSizeExceededException, IOException, RuntimeException) 181 { 182 const sal_Int32 nBytesToWrite( aData.getLength() ); 183 if( nBytesToWrite ) 184 { 185 sal_Int64 nNewSize = static_cast< sal_Int64 >( mnCursor + nBytesToWrite ); 186 if( nNewSize > SAL_MAX_INT32 ) 187 { 188 OSL_ASSERT(false); 189 throw IOException( OUString(RTL_CONSTASCII_USTRINGPARAM("this implementation does not support more than 2GB!")), Reference< XInterface >(static_cast<OWeakObject*>(this)) ); 190 } 191 192 if( static_cast< sal_Int32 >( nNewSize ) > static_cast< sal_Int32 >( maData.size() ) ) 193 maData.resize( static_cast< sal_Int32 >( nNewSize ) ); 194 195 sal_Int8* pData = static_cast<sal_Int8*>(&(*maData.begin())); 196 sal_Int8* pCursor = &(pData[mnCursor]); 197 memcpy( (void*)pCursor, (void*)aData.getConstArray(), nBytesToWrite ); 198 199 mnCursor += nBytesToWrite; 200 } 201 } 202 203 void SAL_CALL UNOMemoryStream::flush() throw (NotConnectedException, BufferSizeExceededException, IOException, RuntimeException) 204 { 205 } 206 207 void SAL_CALL UNOMemoryStream::closeOutput() throw (NotConnectedException, BufferSizeExceededException, IOException, RuntimeException) 208 { 209 mnCursor = 0; 210 } 211 212 //XTruncate 213 void SAL_CALL UNOMemoryStream::truncate() throw (IOException, RuntimeException) 214 { 215 maData.resize( 0 ); 216 mnCursor = 0; 217 } 218 219 ::rtl::OUString SAL_CALL UNOMemoryStream::getImplementationName_static() 220 { 221 static const OUString sImplName( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.comp.MemoryStream" ) ); 222 return sImplName; 223 } 224 225 Sequence< ::rtl::OUString > SAL_CALL UNOMemoryStream::getSupportedServiceNames_static() 226 { 227 Sequence< OUString > aSeq(1); 228 aSeq[0] = getImplementationName_static(); 229 return aSeq; 230 } 231 232 Reference< XInterface > SAL_CALL UNOMemoryStream::Create( 233 const Reference< XComponentContext >& ) 234 { 235 return static_cast<OWeakObject*>(new UNOMemoryStream()); 236 } 237 238 } // namespace comphelper 239 240 void createRegistryInfo_UNOMemoryStream() 241 { 242 static ::comphelper::module::OAutoRegistration< ::comphelper::UNOMemoryStream > aAutoRegistration; 243 } 244