xref: /trunk/main/comphelper/source/streaming/memorystream.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_comphelper.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include "comphelper_module.hxx"
32*cdf0e10cSrcweir 
33*cdf0e10cSrcweir #include <com/sun/star/io/XStream.hpp>
34*cdf0e10cSrcweir #include <com/sun/star/io/XSeekableInputStream.hpp>
35*cdf0e10cSrcweir #include <com/sun/star/io/XTruncate.hpp>
36*cdf0e10cSrcweir #include <com/sun/star/uno/XComponentContext.hpp>
37*cdf0e10cSrcweir #include <cppuhelper/implbase4.hxx>
38*cdf0e10cSrcweir 
39*cdf0e10cSrcweir #include <string.h>
40*cdf0e10cSrcweir #include <vector>
41*cdf0e10cSrcweir 
42*cdf0e10cSrcweir using ::rtl::OUString;
43*cdf0e10cSrcweir using ::cppu::OWeakObject;
44*cdf0e10cSrcweir using ::cppu::WeakImplHelper4;
45*cdf0e10cSrcweir using namespace ::com::sun::star::io;
46*cdf0e10cSrcweir using namespace ::com::sun::star::uno;
47*cdf0e10cSrcweir using namespace ::com::sun::star::lang;
48*cdf0e10cSrcweir using namespace ::osl;
49*cdf0e10cSrcweir 
50*cdf0e10cSrcweir namespace comphelper
51*cdf0e10cSrcweir {
52*cdf0e10cSrcweir 
53*cdf0e10cSrcweir class UNOMemoryStream : public WeakImplHelper4 < XStream, XSeekableInputStream, XOutputStream, XTruncate >
54*cdf0e10cSrcweir {
55*cdf0e10cSrcweir public:
56*cdf0e10cSrcweir     UNOMemoryStream();
57*cdf0e10cSrcweir     virtual ~UNOMemoryStream();
58*cdf0e10cSrcweir 
59*cdf0e10cSrcweir     // XStream
60*cdf0e10cSrcweir     virtual Reference< XInputStream > SAL_CALL getInputStream(  ) throw (RuntimeException);
61*cdf0e10cSrcweir     virtual Reference< XOutputStream > SAL_CALL getOutputStream(  ) throw (RuntimeException);
62*cdf0e10cSrcweir 
63*cdf0e10cSrcweir     // XInputStream
64*cdf0e10cSrcweir     virtual sal_Int32 SAL_CALL readBytes( Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead ) throw (NotConnectedException, BufferSizeExceededException, IOException, RuntimeException);
65*cdf0e10cSrcweir     virtual sal_Int32 SAL_CALL readSomeBytes( Sequence< sal_Int8 >& aData, sal_Int32 nMaxBytesToRead ) throw (NotConnectedException, BufferSizeExceededException, IOException, RuntimeException);
66*cdf0e10cSrcweir     virtual void SAL_CALL skipBytes( sal_Int32 nBytesToSkip ) throw (NotConnectedException, BufferSizeExceededException, IOException, RuntimeException);
67*cdf0e10cSrcweir     virtual sal_Int32 SAL_CALL available() throw (NotConnectedException, IOException, RuntimeException);
68*cdf0e10cSrcweir     virtual void SAL_CALL closeInput() throw (NotConnectedException, IOException, RuntimeException);
69*cdf0e10cSrcweir 
70*cdf0e10cSrcweir     // XSeekable
71*cdf0e10cSrcweir     virtual void SAL_CALL seek( sal_Int64 location ) throw (IllegalArgumentException, IOException, RuntimeException);
72*cdf0e10cSrcweir     virtual sal_Int64 SAL_CALL getPosition() throw (IOException, RuntimeException);
73*cdf0e10cSrcweir     virtual sal_Int64 SAL_CALL getLength() throw (IOException, RuntimeException);
74*cdf0e10cSrcweir 
75*cdf0e10cSrcweir     // XOutputStream
76*cdf0e10cSrcweir     virtual void SAL_CALL writeBytes( const Sequence< sal_Int8 >& aData ) throw (NotConnectedException, BufferSizeExceededException, IOException, RuntimeException);
77*cdf0e10cSrcweir     virtual void SAL_CALL flush() throw (NotConnectedException, BufferSizeExceededException, IOException, RuntimeException);
78*cdf0e10cSrcweir     virtual void SAL_CALL closeOutput() throw (NotConnectedException, BufferSizeExceededException, IOException, RuntimeException);
79*cdf0e10cSrcweir 
80*cdf0e10cSrcweir     // XTruncate
81*cdf0e10cSrcweir     virtual void SAL_CALL truncate() throw (::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException);
82*cdf0e10cSrcweir 
83*cdf0e10cSrcweir     // XServiceInfo - static versions (used for component registration)
84*cdf0e10cSrcweir     static ::rtl::OUString SAL_CALL getImplementationName_static();
85*cdf0e10cSrcweir     static Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames_static();
86*cdf0e10cSrcweir     static Reference< XInterface > SAL_CALL Create( const Reference< ::com::sun::star::uno::XComponentContext >& );
87*cdf0e10cSrcweir 
88*cdf0e10cSrcweir private:
89*cdf0e10cSrcweir     std::vector< sal_Int8 > maData;
90*cdf0e10cSrcweir     sal_Int32 mnCursor;
91*cdf0e10cSrcweir };
92*cdf0e10cSrcweir 
93*cdf0e10cSrcweir UNOMemoryStream::UNOMemoryStream()
94*cdf0e10cSrcweir : mnCursor(0)
95*cdf0e10cSrcweir {
96*cdf0e10cSrcweir }
97*cdf0e10cSrcweir 
98*cdf0e10cSrcweir UNOMemoryStream::~UNOMemoryStream()
99*cdf0e10cSrcweir {
100*cdf0e10cSrcweir }
101*cdf0e10cSrcweir 
102*cdf0e10cSrcweir // XStream
103*cdf0e10cSrcweir Reference< XInputStream > SAL_CALL UNOMemoryStream::getInputStream(  ) throw (RuntimeException)
104*cdf0e10cSrcweir {
105*cdf0e10cSrcweir     return this;
106*cdf0e10cSrcweir }
107*cdf0e10cSrcweir 
108*cdf0e10cSrcweir Reference< XOutputStream > SAL_CALL UNOMemoryStream::getOutputStream(  ) throw (RuntimeException)
109*cdf0e10cSrcweir {
110*cdf0e10cSrcweir     return this;
111*cdf0e10cSrcweir }
112*cdf0e10cSrcweir 
113*cdf0e10cSrcweir // XInputStream
114*cdf0e10cSrcweir sal_Int32 SAL_CALL UNOMemoryStream::readBytes( Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead ) throw (NotConnectedException, BufferSizeExceededException, IOException, RuntimeException)
115*cdf0e10cSrcweir {
116*cdf0e10cSrcweir     if( nBytesToRead < 0 )
117*cdf0e10cSrcweir         throw IOException();
118*cdf0e10cSrcweir 
119*cdf0e10cSrcweir     nBytesToRead = std::min( nBytesToRead, available() );
120*cdf0e10cSrcweir     aData.realloc( nBytesToRead );
121*cdf0e10cSrcweir 
122*cdf0e10cSrcweir     if( nBytesToRead )
123*cdf0e10cSrcweir     {
124*cdf0e10cSrcweir         sal_Int8* pData = static_cast<sal_Int8*>(&(*maData.begin()));
125*cdf0e10cSrcweir         sal_Int8* pCursor = &((pData)[mnCursor]);
126*cdf0e10cSrcweir         memcpy( (void*)aData.getArray(), (void*)pCursor, nBytesToRead );
127*cdf0e10cSrcweir 
128*cdf0e10cSrcweir         mnCursor += nBytesToRead;
129*cdf0e10cSrcweir     }
130*cdf0e10cSrcweir 
131*cdf0e10cSrcweir     return nBytesToRead;
132*cdf0e10cSrcweir }
133*cdf0e10cSrcweir 
134*cdf0e10cSrcweir sal_Int32 SAL_CALL UNOMemoryStream::readSomeBytes( Sequence< sal_Int8 >& aData, sal_Int32 nMaxBytesToRead ) throw (NotConnectedException, BufferSizeExceededException, IOException, RuntimeException)
135*cdf0e10cSrcweir {
136*cdf0e10cSrcweir     return readBytes( aData, nMaxBytesToRead );
137*cdf0e10cSrcweir }
138*cdf0e10cSrcweir 
139*cdf0e10cSrcweir void SAL_CALL UNOMemoryStream::skipBytes( sal_Int32 nBytesToSkip ) throw (NotConnectedException, BufferSizeExceededException, IOException, RuntimeException)
140*cdf0e10cSrcweir {
141*cdf0e10cSrcweir     if( nBytesToSkip < 0 )
142*cdf0e10cSrcweir         throw IOException();
143*cdf0e10cSrcweir 
144*cdf0e10cSrcweir     mnCursor += std::min( nBytesToSkip, available() );
145*cdf0e10cSrcweir }
146*cdf0e10cSrcweir 
147*cdf0e10cSrcweir sal_Int32 SAL_CALL UNOMemoryStream::available() throw (NotConnectedException, IOException, RuntimeException)
148*cdf0e10cSrcweir {
149*cdf0e10cSrcweir     return static_cast< sal_Int32 >( maData.size() ) - mnCursor;
150*cdf0e10cSrcweir }
151*cdf0e10cSrcweir 
152*cdf0e10cSrcweir void SAL_CALL UNOMemoryStream::closeInput() throw (NotConnectedException, IOException, RuntimeException)
153*cdf0e10cSrcweir {
154*cdf0e10cSrcweir     mnCursor = 0;
155*cdf0e10cSrcweir }
156*cdf0e10cSrcweir 
157*cdf0e10cSrcweir // XSeekable
158*cdf0e10cSrcweir void SAL_CALL UNOMemoryStream::seek( sal_Int64 location ) throw (IllegalArgumentException, IOException, RuntimeException)
159*cdf0e10cSrcweir {
160*cdf0e10cSrcweir     if( (location < 0) || (location > SAL_MAX_INT32) )
161*cdf0e10cSrcweir         throw IllegalArgumentException( OUString(RTL_CONSTASCII_USTRINGPARAM("this implementation does not support more than 2GB!")), Reference< XInterface >(static_cast<OWeakObject*>(this)), 0 );
162*cdf0e10cSrcweir 
163*cdf0e10cSrcweir     // seek operation should be able to resize the stream
164*cdf0e10cSrcweir     if ( location > static_cast< sal_Int64 >( maData.size() ) )
165*cdf0e10cSrcweir         maData.resize( static_cast< sal_Int32 >( location ) );
166*cdf0e10cSrcweir 
167*cdf0e10cSrcweir     if ( location > static_cast< sal_Int64 >( maData.size() ) )
168*cdf0e10cSrcweir         maData.resize( static_cast< sal_Int32 >( location ) );
169*cdf0e10cSrcweir 
170*cdf0e10cSrcweir     mnCursor = static_cast< sal_Int32 >( location );
171*cdf0e10cSrcweir }
172*cdf0e10cSrcweir 
173*cdf0e10cSrcweir sal_Int64 SAL_CALL UNOMemoryStream::getPosition() throw (IOException, RuntimeException)
174*cdf0e10cSrcweir {
175*cdf0e10cSrcweir     return static_cast< sal_Int64 >( mnCursor );
176*cdf0e10cSrcweir }
177*cdf0e10cSrcweir 
178*cdf0e10cSrcweir sal_Int64 SAL_CALL UNOMemoryStream::getLength() throw (IOException, RuntimeException)
179*cdf0e10cSrcweir {
180*cdf0e10cSrcweir     return static_cast< sal_Int64 >( maData.size() );
181*cdf0e10cSrcweir }
182*cdf0e10cSrcweir 
183*cdf0e10cSrcweir // XOutputStream
184*cdf0e10cSrcweir void SAL_CALL UNOMemoryStream::writeBytes( const Sequence< sal_Int8 >& aData ) throw (NotConnectedException, BufferSizeExceededException, IOException, RuntimeException)
185*cdf0e10cSrcweir {
186*cdf0e10cSrcweir     const sal_Int32 nBytesToWrite( aData.getLength() );
187*cdf0e10cSrcweir     if( nBytesToWrite )
188*cdf0e10cSrcweir     {
189*cdf0e10cSrcweir         sal_Int64 nNewSize = static_cast< sal_Int64 >( mnCursor + nBytesToWrite );
190*cdf0e10cSrcweir         if( nNewSize > SAL_MAX_INT32 )
191*cdf0e10cSrcweir         {
192*cdf0e10cSrcweir             OSL_ASSERT(false);
193*cdf0e10cSrcweir             throw IOException( OUString(RTL_CONSTASCII_USTRINGPARAM("this implementation does not support more than 2GB!")), Reference< XInterface >(static_cast<OWeakObject*>(this)) );
194*cdf0e10cSrcweir         }
195*cdf0e10cSrcweir 
196*cdf0e10cSrcweir         if( static_cast< sal_Int32 >( nNewSize ) > static_cast< sal_Int32 >( maData.size() ) )
197*cdf0e10cSrcweir             maData.resize( static_cast< sal_Int32 >( nNewSize ) );
198*cdf0e10cSrcweir 
199*cdf0e10cSrcweir         sal_Int8* pData = static_cast<sal_Int8*>(&(*maData.begin()));
200*cdf0e10cSrcweir         sal_Int8* pCursor = &(pData[mnCursor]);
201*cdf0e10cSrcweir         memcpy( (void*)pCursor, (void*)aData.getConstArray(), nBytesToWrite );
202*cdf0e10cSrcweir 
203*cdf0e10cSrcweir         mnCursor += nBytesToWrite;
204*cdf0e10cSrcweir     }
205*cdf0e10cSrcweir }
206*cdf0e10cSrcweir 
207*cdf0e10cSrcweir void SAL_CALL UNOMemoryStream::flush() throw (NotConnectedException, BufferSizeExceededException, IOException, RuntimeException)
208*cdf0e10cSrcweir {
209*cdf0e10cSrcweir }
210*cdf0e10cSrcweir 
211*cdf0e10cSrcweir void SAL_CALL UNOMemoryStream::closeOutput() throw (NotConnectedException, BufferSizeExceededException, IOException, RuntimeException)
212*cdf0e10cSrcweir {
213*cdf0e10cSrcweir     mnCursor = 0;
214*cdf0e10cSrcweir }
215*cdf0e10cSrcweir 
216*cdf0e10cSrcweir //XTruncate
217*cdf0e10cSrcweir void SAL_CALL UNOMemoryStream::truncate() throw (IOException, RuntimeException)
218*cdf0e10cSrcweir {
219*cdf0e10cSrcweir     maData.resize( 0 );
220*cdf0e10cSrcweir     mnCursor = 0;
221*cdf0e10cSrcweir }
222*cdf0e10cSrcweir 
223*cdf0e10cSrcweir ::rtl::OUString SAL_CALL UNOMemoryStream::getImplementationName_static()
224*cdf0e10cSrcweir {
225*cdf0e10cSrcweir     static const OUString sImplName( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.comp.MemoryStream" ) );
226*cdf0e10cSrcweir     return sImplName;
227*cdf0e10cSrcweir }
228*cdf0e10cSrcweir 
229*cdf0e10cSrcweir Sequence< ::rtl::OUString > SAL_CALL UNOMemoryStream::getSupportedServiceNames_static()
230*cdf0e10cSrcweir {
231*cdf0e10cSrcweir     Sequence< OUString > aSeq(1);
232*cdf0e10cSrcweir     aSeq[0] = getImplementationName_static();
233*cdf0e10cSrcweir     return aSeq;
234*cdf0e10cSrcweir }
235*cdf0e10cSrcweir 
236*cdf0e10cSrcweir Reference< XInterface > SAL_CALL UNOMemoryStream::Create(
237*cdf0e10cSrcweir     const Reference< XComponentContext >& )
238*cdf0e10cSrcweir {
239*cdf0e10cSrcweir     return static_cast<OWeakObject*>(new UNOMemoryStream());
240*cdf0e10cSrcweir }
241*cdf0e10cSrcweir 
242*cdf0e10cSrcweir } // namespace comphelper
243*cdf0e10cSrcweir 
244*cdf0e10cSrcweir void createRegistryInfo_UNOMemoryStream()
245*cdf0e10cSrcweir {
246*cdf0e10cSrcweir     static ::comphelper::module::OAutoRegistration< ::comphelper::UNOMemoryStream > aAutoRegistration;
247*cdf0e10cSrcweir }
248