xref: /AOO41X/main/comphelper/source/streaming/seqstream.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 #include <comphelper/seqstream.hxx>
31*cdf0e10cSrcweir 
32*cdf0e10cSrcweir #include <memory.h> // for memcpy
33*cdf0e10cSrcweir 
34*cdf0e10cSrcweir namespace comphelper
35*cdf0e10cSrcweir {
36*cdf0e10cSrcweir using namespace ::com::sun::star::lang;
37*cdf0e10cSrcweir using namespace ::com::sun::star::io;
38*cdf0e10cSrcweir using namespace ::com::sun::star::uno;
39*cdf0e10cSrcweir using namespace ::osl;
40*cdf0e10cSrcweir 
41*cdf0e10cSrcweir //---------------------------------------------------------------------------------------------
42*cdf0e10cSrcweir // class SequenceInputStream
43*cdf0e10cSrcweir //---------------------------------------------------------------------------------------------
44*cdf0e10cSrcweir 
45*cdf0e10cSrcweir //------------------------------------------------------------------
46*cdf0e10cSrcweir SequenceInputStream::SequenceInputStream(const ByteSequence& rData)
47*cdf0e10cSrcweir :	m_aData(rData)
48*cdf0e10cSrcweir ,	m_nPos(0)
49*cdf0e10cSrcweir {
50*cdf0e10cSrcweir }
51*cdf0e10cSrcweir 
52*cdf0e10cSrcweir // checks if closed, returns available size, not mutex-protected
53*cdf0e10cSrcweir //------------------------------------------------------------------
54*cdf0e10cSrcweir inline sal_Int32 SequenceInputStream::avail()
55*cdf0e10cSrcweir {
56*cdf0e10cSrcweir 	if (m_nPos == -1)
57*cdf0e10cSrcweir 		throw NotConnectedException(::rtl::OUString(), *this);
58*cdf0e10cSrcweir 
59*cdf0e10cSrcweir 	return m_aData.getLength() - m_nPos;
60*cdf0e10cSrcweir }
61*cdf0e10cSrcweir 
62*cdf0e10cSrcweir // com::sun::star::io::XInputStream
63*cdf0e10cSrcweir //------------------------------------------------------------------
64*cdf0e10cSrcweir sal_Int32 SAL_CALL SequenceInputStream::readBytes( Sequence<sal_Int8>& aData, sal_Int32 nBytesToRead )
65*cdf0e10cSrcweir 	throw(NotConnectedException, BufferSizeExceededException,
66*cdf0e10cSrcweir 		  IOException, RuntimeException)
67*cdf0e10cSrcweir {
68*cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
69*cdf0e10cSrcweir 
70*cdf0e10cSrcweir 	sal_Int32 nAvail = avail();
71*cdf0e10cSrcweir 
72*cdf0e10cSrcweir 	if (nBytesToRead < 0)
73*cdf0e10cSrcweir 		throw BufferSizeExceededException(::rtl::OUString(),*this);
74*cdf0e10cSrcweir 
75*cdf0e10cSrcweir 	if (nAvail < nBytesToRead)
76*cdf0e10cSrcweir 		nBytesToRead = nAvail;
77*cdf0e10cSrcweir 
78*cdf0e10cSrcweir 	aData.realloc(nBytesToRead);
79*cdf0e10cSrcweir 	memcpy(aData.getArray(), m_aData.getConstArray() + m_nPos, nBytesToRead);
80*cdf0e10cSrcweir 	m_nPos += nBytesToRead;
81*cdf0e10cSrcweir 
82*cdf0e10cSrcweir 	return nBytesToRead;
83*cdf0e10cSrcweir }
84*cdf0e10cSrcweir 
85*cdf0e10cSrcweir //------------------------------------------------------------------
86*cdf0e10cSrcweir sal_Int32 SAL_CALL SequenceInputStream::readSomeBytes( Sequence<sal_Int8>& aData, sal_Int32 nMaxBytesToRead )
87*cdf0e10cSrcweir 	throw(NotConnectedException, BufferSizeExceededException,
88*cdf0e10cSrcweir 		  IOException, RuntimeException)
89*cdf0e10cSrcweir {
90*cdf0e10cSrcweir 	// all data is available at once
91*cdf0e10cSrcweir 	return readBytes(aData, nMaxBytesToRead);
92*cdf0e10cSrcweir }
93*cdf0e10cSrcweir 
94*cdf0e10cSrcweir //------------------------------------------------------------------
95*cdf0e10cSrcweir void SAL_CALL SequenceInputStream::skipBytes( sal_Int32 nBytesToSkip )
96*cdf0e10cSrcweir 	throw(NotConnectedException, BufferSizeExceededException,
97*cdf0e10cSrcweir 		  IOException, RuntimeException)
98*cdf0e10cSrcweir {
99*cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
100*cdf0e10cSrcweir 
101*cdf0e10cSrcweir 	sal_Int32 nAvail = avail();
102*cdf0e10cSrcweir 
103*cdf0e10cSrcweir 	if (nBytesToSkip < 0)
104*cdf0e10cSrcweir 		throw BufferSizeExceededException(::rtl::OUString(),*this);
105*cdf0e10cSrcweir 
106*cdf0e10cSrcweir 	if (nAvail < nBytesToSkip)
107*cdf0e10cSrcweir 		nBytesToSkip = nAvail;
108*cdf0e10cSrcweir 
109*cdf0e10cSrcweir 	m_nPos += nBytesToSkip;
110*cdf0e10cSrcweir }
111*cdf0e10cSrcweir 
112*cdf0e10cSrcweir //------------------------------------------------------------------
113*cdf0e10cSrcweir sal_Int32 SAL_CALL SequenceInputStream::available(  )
114*cdf0e10cSrcweir 	throw(NotConnectedException, IOException, RuntimeException)
115*cdf0e10cSrcweir {
116*cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
117*cdf0e10cSrcweir 
118*cdf0e10cSrcweir 	return avail();
119*cdf0e10cSrcweir }
120*cdf0e10cSrcweir 
121*cdf0e10cSrcweir //------------------------------------------------------------------
122*cdf0e10cSrcweir void SAL_CALL SequenceInputStream::closeInput(  )
123*cdf0e10cSrcweir 	throw(NotConnectedException, IOException, RuntimeException)
124*cdf0e10cSrcweir {
125*cdf0e10cSrcweir 	if (m_nPos == -1)
126*cdf0e10cSrcweir 		throw NotConnectedException(::rtl::OUString(), *this);
127*cdf0e10cSrcweir 
128*cdf0e10cSrcweir 	m_nPos = -1;
129*cdf0e10cSrcweir }
130*cdf0e10cSrcweir 
131*cdf0e10cSrcweir void SAL_CALL SequenceInputStream::seek( sal_Int64 location ) throw (IllegalArgumentException, IOException, RuntimeException)
132*cdf0e10cSrcweir {
133*cdf0e10cSrcweir     if ( location > m_aData.getLength() || location < 0 || location > SAL_MAX_INT32 )
134*cdf0e10cSrcweir         throw IllegalArgumentException();
135*cdf0e10cSrcweir     m_nPos = (sal_Int32) location;
136*cdf0e10cSrcweir }
137*cdf0e10cSrcweir 
138*cdf0e10cSrcweir sal_Int64 SAL_CALL SequenceInputStream::getPosition() throw (IOException, RuntimeException)
139*cdf0e10cSrcweir {
140*cdf0e10cSrcweir     return m_nPos;
141*cdf0e10cSrcweir }
142*cdf0e10cSrcweir 
143*cdf0e10cSrcweir sal_Int64 SAL_CALL SequenceInputStream::getLength(  ) throw (IOException, RuntimeException)
144*cdf0e10cSrcweir {
145*cdf0e10cSrcweir     return m_aData.getLength();
146*cdf0e10cSrcweir }
147*cdf0e10cSrcweir 
148*cdf0e10cSrcweir //--------------------------------------------------------------------------
149*cdf0e10cSrcweir OSequenceOutputStream::OSequenceOutputStream(Sequence< sal_Int8 >& _rSeq, double _nResizeFactor, sal_Int32 _nMinimumResize, sal_Int32 _nMaximumResize)
150*cdf0e10cSrcweir 	:m_rSequence(_rSeq)
151*cdf0e10cSrcweir 	,m_nResizeFactor(_nResizeFactor)
152*cdf0e10cSrcweir 	,m_nMinimumResize(_nMinimumResize)
153*cdf0e10cSrcweir 	,m_nMaximumResize(_nMaximumResize)
154*cdf0e10cSrcweir 	,m_nSize(0)	// starting at position 0
155*cdf0e10cSrcweir 	,m_bConnected(sal_True)
156*cdf0e10cSrcweir {
157*cdf0e10cSrcweir 	OSL_ENSURE(m_nResizeFactor > 1, "OSequenceOutputStream::OSequenceOutputStream : invalid resize factor !");
158*cdf0e10cSrcweir 	OSL_ENSURE((m_nMaximumResize < 0) || (m_nMaximumResize > m_nMinimumResize),
159*cdf0e10cSrcweir 		"OSequenceOutputStream::OSequenceOutputStream : these limits don't make any sense !");
160*cdf0e10cSrcweir 
161*cdf0e10cSrcweir 	if (m_nResizeFactor <= 1)
162*cdf0e10cSrcweir 		m_nResizeFactor = 1.3;
163*cdf0e10cSrcweir 	if ((m_nMaximumResize >= 0) && (m_nMaximumResize <= m_nMinimumResize))
164*cdf0e10cSrcweir 		m_nMaximumResize = m_nMinimumResize * 2;
165*cdf0e10cSrcweir 		// this heuristic is as good as any other ... supply better parameters if you don't like it :)
166*cdf0e10cSrcweir }
167*cdf0e10cSrcweir 
168*cdf0e10cSrcweir //--------------------------------------------------------------------------
169*cdf0e10cSrcweir void SAL_CALL OSequenceOutputStream::writeBytes( const Sequence< sal_Int8 >& _rData ) throw(NotConnectedException, BufferSizeExceededException, IOException, RuntimeException)
170*cdf0e10cSrcweir {
171*cdf0e10cSrcweir 	MutexGuard aGuard(m_aMutex);
172*cdf0e10cSrcweir 	if (!m_bConnected)
173*cdf0e10cSrcweir 		throw NotConnectedException();
174*cdf0e10cSrcweir 
175*cdf0e10cSrcweir 	// ensure the sequence has enoungh space left
176*cdf0e10cSrcweir 	if (m_nSize + _rData.getLength() > m_rSequence.getLength())
177*cdf0e10cSrcweir 	{
178*cdf0e10cSrcweir 		sal_Int32 nCurrentLength = m_rSequence.getLength();
179*cdf0e10cSrcweir 		sal_Int32 nNewLength = static_cast< sal_Int32 >(
180*cdf0e10cSrcweir             nCurrentLength * m_nResizeFactor);
181*cdf0e10cSrcweir 
182*cdf0e10cSrcweir 		if (m_nMinimumResize > nNewLength - nCurrentLength)
183*cdf0e10cSrcweir 			// we have a minimum so it's not too inefficient for small sequences and small write requests
184*cdf0e10cSrcweir 			nNewLength = nCurrentLength + m_nMinimumResize;
185*cdf0e10cSrcweir 
186*cdf0e10cSrcweir 		if ((m_nMaximumResize > 0) && (nNewLength - nCurrentLength > m_nMaximumResize))
187*cdf0e10cSrcweir 			// such a large step is not allowed
188*cdf0e10cSrcweir 			nNewLength = nCurrentLength + m_nMaximumResize;
189*cdf0e10cSrcweir 
190*cdf0e10cSrcweir 		if (nNewLength < m_nSize + _rData.getLength())
191*cdf0e10cSrcweir 		{	// it's not enough .... the data would not fit
192*cdf0e10cSrcweir 
193*cdf0e10cSrcweir 			// let's take the double amount of the length of the data to be written, as the next write
194*cdf0e10cSrcweir 			// request could be as large as this one
195*cdf0e10cSrcweir 			sal_Int32 nNewGrowth = _rData.getLength() * 2;
196*cdf0e10cSrcweir 			if ((m_nMaximumResize > 0) && (nNewGrowth > m_nMaximumResize))
197*cdf0e10cSrcweir 			{	// we came to the limit, again ...
198*cdf0e10cSrcweir 				nNewGrowth = m_nMaximumResize;
199*cdf0e10cSrcweir 				if (nNewGrowth + nCurrentLength < m_nSize + _rData.getLength())
200*cdf0e10cSrcweir 					// but it would not fit if we respect the limit
201*cdf0e10cSrcweir 					nNewGrowth = m_nSize + _rData.getLength() - nCurrentLength;
202*cdf0e10cSrcweir 			}
203*cdf0e10cSrcweir 			nNewLength = nCurrentLength + nNewGrowth;
204*cdf0e10cSrcweir 		}
205*cdf0e10cSrcweir 
206*cdf0e10cSrcweir 		// round it off to the next multiple of 4 ...
207*cdf0e10cSrcweir 		nNewLength = (nNewLength + 3) / 4 * 4;
208*cdf0e10cSrcweir 
209*cdf0e10cSrcweir 		m_rSequence.realloc(nNewLength);
210*cdf0e10cSrcweir 	}
211*cdf0e10cSrcweir 
212*cdf0e10cSrcweir 	OSL_ENSURE(m_rSequence.getLength() >= m_nSize + _rData.getLength(),
213*cdf0e10cSrcweir 		"ooops ... the realloc algorithm seems to be wrong :( !");
214*cdf0e10cSrcweir 
215*cdf0e10cSrcweir 	memcpy(m_rSequence.getArray() + m_nSize, _rData.getConstArray(), _rData.getLength());
216*cdf0e10cSrcweir 	m_nSize += _rData.getLength();
217*cdf0e10cSrcweir }
218*cdf0e10cSrcweir 
219*cdf0e10cSrcweir //--------------------------------------------------------------------------
220*cdf0e10cSrcweir void SAL_CALL OSequenceOutputStream::flush(  ) throw(NotConnectedException, BufferSizeExceededException, IOException, RuntimeException)
221*cdf0e10cSrcweir {
222*cdf0e10cSrcweir 	MutexGuard aGuard(m_aMutex);
223*cdf0e10cSrcweir 	if (!m_bConnected)
224*cdf0e10cSrcweir 		throw NotConnectedException();
225*cdf0e10cSrcweir 
226*cdf0e10cSrcweir     // cut the sequence to the real size
227*cdf0e10cSrcweir     m_rSequence.realloc(m_nSize);
228*cdf0e10cSrcweir }
229*cdf0e10cSrcweir 
230*cdf0e10cSrcweir //--------------------------------------------------------------------------
231*cdf0e10cSrcweir void SAL_CALL OSequenceOutputStream::closeOutput(  ) throw(NotConnectedException, BufferSizeExceededException, IOException, RuntimeException)
232*cdf0e10cSrcweir {
233*cdf0e10cSrcweir 	MutexGuard aGuard(m_aMutex);
234*cdf0e10cSrcweir 	if (!m_bConnected)
235*cdf0e10cSrcweir 		throw NotConnectedException();
236*cdf0e10cSrcweir 
237*cdf0e10cSrcweir 	// cut the sequence to the real size
238*cdf0e10cSrcweir 	m_rSequence.realloc(m_nSize);
239*cdf0e10cSrcweir 	// and don't allow any further accesses
240*cdf0e10cSrcweir 	m_bConnected = sal_False;
241*cdf0e10cSrcweir }
242*cdf0e10cSrcweir 
243*cdf0e10cSrcweir } // namespace comphelper
244