1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: memorystream.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_comphelper.hxx"
34 #include "comphelper_module.hxx"
36 #include <com/sun/star/io/XStream.hpp>
37 #include <com/sun/star/io/XSeekableInputStream.hpp>
38 #include <com/sun/star/io/XTruncate.hpp>
39 #include <com/sun/star/uno/XComponentContext.hpp>
40 #include <cppuhelper/implbase4.hxx>
45 using ::rtl::OUString
;
46 using ::cppu::OWeakObject
;
47 using ::cppu::WeakImplHelper4
;
48 using namespace ::com::sun::star::io
;
49 using namespace ::com::sun::star::uno
;
50 using namespace ::com::sun::star::lang
;
51 using namespace ::osl
;
56 class UNOMemoryStream
: public WeakImplHelper4
< XStream
, XSeekableInputStream
, XOutputStream
, XTruncate
>
60 virtual ~UNOMemoryStream();
63 virtual Reference
< XInputStream
> SAL_CALL
getInputStream( ) throw (RuntimeException
);
64 virtual Reference
< XOutputStream
> SAL_CALL
getOutputStream( ) throw (RuntimeException
);
67 virtual sal_Int32 SAL_CALL
readBytes( Sequence
< sal_Int8
>& aData
, sal_Int32 nBytesToRead
) throw (NotConnectedException
, BufferSizeExceededException
, IOException
, RuntimeException
);
68 virtual sal_Int32 SAL_CALL
readSomeBytes( Sequence
< sal_Int8
>& aData
, sal_Int32 nMaxBytesToRead
) throw (NotConnectedException
, BufferSizeExceededException
, IOException
, RuntimeException
);
69 virtual void SAL_CALL
skipBytes( sal_Int32 nBytesToSkip
) throw (NotConnectedException
, BufferSizeExceededException
, IOException
, RuntimeException
);
70 virtual sal_Int32 SAL_CALL
available() throw (NotConnectedException
, IOException
, RuntimeException
);
71 virtual void SAL_CALL
closeInput() throw (NotConnectedException
, IOException
, RuntimeException
);
74 virtual void SAL_CALL
seek( sal_Int64 location
) throw (IllegalArgumentException
, IOException
, RuntimeException
);
75 virtual sal_Int64 SAL_CALL
getPosition() throw (IOException
, RuntimeException
);
76 virtual sal_Int64 SAL_CALL
getLength() throw (IOException
, RuntimeException
);
79 virtual void SAL_CALL
writeBytes( const Sequence
< sal_Int8
>& aData
) throw (NotConnectedException
, BufferSizeExceededException
, IOException
, RuntimeException
);
80 virtual void SAL_CALL
flush() throw (NotConnectedException
, BufferSizeExceededException
, IOException
, RuntimeException
);
81 virtual void SAL_CALL
closeOutput() throw (NotConnectedException
, BufferSizeExceededException
, IOException
, RuntimeException
);
84 virtual void SAL_CALL
truncate() throw (::com::sun::star::io::IOException
, ::com::sun::star::uno::RuntimeException
);
86 // XServiceInfo - static versions (used for component registration)
87 static ::rtl::OUString SAL_CALL
getImplementationName_static();
88 static Sequence
< ::rtl::OUString
> SAL_CALL
getSupportedServiceNames_static();
89 static Reference
< XInterface
> SAL_CALL
Create( const Reference
< ::com::sun::star::uno::XComponentContext
>& );
92 std::vector
< sal_Int8
> maData
;
96 UNOMemoryStream::UNOMemoryStream()
101 UNOMemoryStream::~UNOMemoryStream()
106 Reference
< XInputStream
> SAL_CALL
UNOMemoryStream::getInputStream( ) throw (RuntimeException
)
111 Reference
< XOutputStream
> SAL_CALL
UNOMemoryStream::getOutputStream( ) throw (RuntimeException
)
117 sal_Int32 SAL_CALL
UNOMemoryStream::readBytes( Sequence
< sal_Int8
>& aData
, sal_Int32 nBytesToRead
) throw (NotConnectedException
, BufferSizeExceededException
, IOException
, RuntimeException
)
119 if( nBytesToRead
< 0 )
122 nBytesToRead
= std::min( nBytesToRead
, available() );
123 aData
.realloc( nBytesToRead
);
127 sal_Int8
* pData
= static_cast<sal_Int8
*>(&(*maData
.begin()));
128 sal_Int8
* pCursor
= &((pData
)[mnCursor
]);
129 memcpy( (void*)aData
.getArray(), (void*)pCursor
, nBytesToRead
);
131 mnCursor
+= nBytesToRead
;
137 sal_Int32 SAL_CALL
UNOMemoryStream::readSomeBytes( Sequence
< sal_Int8
>& aData
, sal_Int32 nMaxBytesToRead
) throw (NotConnectedException
, BufferSizeExceededException
, IOException
, RuntimeException
)
139 return readBytes( aData
, nMaxBytesToRead
);
142 void SAL_CALL
UNOMemoryStream::skipBytes( sal_Int32 nBytesToSkip
) throw (NotConnectedException
, BufferSizeExceededException
, IOException
, RuntimeException
)
144 if( nBytesToSkip
< 0 )
147 mnCursor
+= std::min( nBytesToSkip
, available() );
150 sal_Int32 SAL_CALL
UNOMemoryStream::available() throw (NotConnectedException
, IOException
, RuntimeException
)
152 return static_cast< sal_Int32
>( maData
.size() ) - mnCursor
;
155 void SAL_CALL
UNOMemoryStream::closeInput() throw (NotConnectedException
, IOException
, RuntimeException
)
161 void SAL_CALL
UNOMemoryStream::seek( sal_Int64 location
) throw (IllegalArgumentException
, IOException
, RuntimeException
)
163 if( (location
< 0) || (location
> SAL_MAX_INT32
) )
164 throw IllegalArgumentException( OUString(RTL_CONSTASCII_USTRINGPARAM("this implementation does not support more than 2GB!")), Reference
< XInterface
>(static_cast<OWeakObject
*>(this)), 0 );
166 // seek operation should be able to resize the stream
167 if ( location
> static_cast< sal_Int64
>( maData
.size() ) )
168 maData
.resize( static_cast< sal_Int32
>( location
) );
170 if ( location
> static_cast< sal_Int64
>( maData
.size() ) )
171 maData
.resize( static_cast< sal_Int32
>( location
) );
173 mnCursor
= static_cast< sal_Int32
>( location
);
176 sal_Int64 SAL_CALL
UNOMemoryStream::getPosition() throw (IOException
, RuntimeException
)
178 return static_cast< sal_Int64
>( mnCursor
);
181 sal_Int64 SAL_CALL
UNOMemoryStream::getLength() throw (IOException
, RuntimeException
)
183 return static_cast< sal_Int64
>( maData
.size() );
187 void SAL_CALL
UNOMemoryStream::writeBytes( const Sequence
< sal_Int8
>& aData
) throw (NotConnectedException
, BufferSizeExceededException
, IOException
, RuntimeException
)
189 const sal_Int32
nBytesToWrite( aData
.getLength() );
192 sal_Int64 nNewSize
= static_cast< sal_Int64
>( mnCursor
+ nBytesToWrite
);
193 if( nNewSize
> SAL_MAX_INT32
)
196 throw IOException( OUString(RTL_CONSTASCII_USTRINGPARAM("this implementation does not support more than 2GB!")), Reference
< XInterface
>(static_cast<OWeakObject
*>(this)) );
199 if( static_cast< sal_Int32
>( nNewSize
) > static_cast< sal_Int32
>( maData
.size() ) )
200 maData
.resize( static_cast< sal_Int32
>( nNewSize
) );
202 sal_Int8
* pData
= static_cast<sal_Int8
*>(&(*maData
.begin()));
203 sal_Int8
* pCursor
= &(pData
[mnCursor
]);
204 memcpy( (void*)pCursor
, (void*)aData
.getConstArray(), nBytesToWrite
);
206 mnCursor
+= nBytesToWrite
;
210 void SAL_CALL
UNOMemoryStream::flush() throw (NotConnectedException
, BufferSizeExceededException
, IOException
, RuntimeException
)
214 void SAL_CALL
UNOMemoryStream::closeOutput() throw (NotConnectedException
, BufferSizeExceededException
, IOException
, RuntimeException
)
220 void SAL_CALL
UNOMemoryStream::truncate() throw (IOException
, RuntimeException
)
226 ::rtl::OUString SAL_CALL
UNOMemoryStream::getImplementationName_static()
228 static const OUString
sImplName( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.comp.MemoryStream" ) );
232 Sequence
< ::rtl::OUString
> SAL_CALL
UNOMemoryStream::getSupportedServiceNames_static()
234 Sequence
< OUString
> aSeq(1);
235 aSeq
[0] = getImplementationName_static();
239 Reference
< XInterface
> SAL_CALL
UNOMemoryStream::Create(
240 const Reference
< XComponentContext
>& )
242 return static_cast<OWeakObject
*>(new UNOMemoryStream());
245 } // namespace comphelper
247 void createRegistryInfo_UNOMemoryStream()
249 static ::comphelper::module::OAutoRegistration
< ::comphelper::UNOMemoryStream
> aAutoRegistration
;