1*dde7d3faSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*dde7d3faSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*dde7d3faSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*dde7d3faSAndrew Rist  * distributed with this work for additional information
6*dde7d3faSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*dde7d3faSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*dde7d3faSAndrew Rist  * "License"); you may not use this file except in compliance
9*dde7d3faSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*dde7d3faSAndrew Rist  *
11*dde7d3faSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*dde7d3faSAndrew Rist  *
13*dde7d3faSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*dde7d3faSAndrew Rist  * software distributed under the License is distributed on an
15*dde7d3faSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*dde7d3faSAndrew Rist  * KIND, either express or implied.  See the License for the
17*dde7d3faSAndrew Rist  * specific language governing permissions and limitations
18*dde7d3faSAndrew Rist  * under the License.
19*dde7d3faSAndrew Rist  *
20*dde7d3faSAndrew Rist  *************************************************************/
21*dde7d3faSAndrew Rist 
22*dde7d3faSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_comphelper.hxx"
26cdf0e10cSrcweir #include <comphelper/seqstream.hxx>
27cdf0e10cSrcweir 
28cdf0e10cSrcweir #include <memory.h> // for memcpy
29cdf0e10cSrcweir 
30cdf0e10cSrcweir namespace comphelper
31cdf0e10cSrcweir {
32cdf0e10cSrcweir using namespace ::com::sun::star::lang;
33cdf0e10cSrcweir using namespace ::com::sun::star::io;
34cdf0e10cSrcweir using namespace ::com::sun::star::uno;
35cdf0e10cSrcweir using namespace ::osl;
36cdf0e10cSrcweir 
37cdf0e10cSrcweir //---------------------------------------------------------------------------------------------
38cdf0e10cSrcweir // class SequenceInputStream
39cdf0e10cSrcweir //---------------------------------------------------------------------------------------------
40cdf0e10cSrcweir 
41cdf0e10cSrcweir //------------------------------------------------------------------
SequenceInputStream(const ByteSequence & rData)42cdf0e10cSrcweir SequenceInputStream::SequenceInputStream(const ByteSequence& rData)
43cdf0e10cSrcweir :	m_aData(rData)
44cdf0e10cSrcweir ,	m_nPos(0)
45cdf0e10cSrcweir {
46cdf0e10cSrcweir }
47cdf0e10cSrcweir 
48cdf0e10cSrcweir // checks if closed, returns available size, not mutex-protected
49cdf0e10cSrcweir //------------------------------------------------------------------
avail()50cdf0e10cSrcweir inline sal_Int32 SequenceInputStream::avail()
51cdf0e10cSrcweir {
52cdf0e10cSrcweir 	if (m_nPos == -1)
53cdf0e10cSrcweir 		throw NotConnectedException(::rtl::OUString(), *this);
54cdf0e10cSrcweir 
55cdf0e10cSrcweir 	return m_aData.getLength() - m_nPos;
56cdf0e10cSrcweir }
57cdf0e10cSrcweir 
58cdf0e10cSrcweir // com::sun::star::io::XInputStream
59cdf0e10cSrcweir //------------------------------------------------------------------
readBytes(Sequence<sal_Int8> & aData,sal_Int32 nBytesToRead)60cdf0e10cSrcweir sal_Int32 SAL_CALL SequenceInputStream::readBytes( Sequence<sal_Int8>& aData, sal_Int32 nBytesToRead )
61cdf0e10cSrcweir 	throw(NotConnectedException, BufferSizeExceededException,
62cdf0e10cSrcweir 		  IOException, RuntimeException)
63cdf0e10cSrcweir {
64cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
65cdf0e10cSrcweir 
66cdf0e10cSrcweir 	sal_Int32 nAvail = avail();
67cdf0e10cSrcweir 
68cdf0e10cSrcweir 	if (nBytesToRead < 0)
69cdf0e10cSrcweir 		throw BufferSizeExceededException(::rtl::OUString(),*this);
70cdf0e10cSrcweir 
71cdf0e10cSrcweir 	if (nAvail < nBytesToRead)
72cdf0e10cSrcweir 		nBytesToRead = nAvail;
73cdf0e10cSrcweir 
74cdf0e10cSrcweir 	aData.realloc(nBytesToRead);
75cdf0e10cSrcweir 	memcpy(aData.getArray(), m_aData.getConstArray() + m_nPos, nBytesToRead);
76cdf0e10cSrcweir 	m_nPos += nBytesToRead;
77cdf0e10cSrcweir 
78cdf0e10cSrcweir 	return nBytesToRead;
79cdf0e10cSrcweir }
80cdf0e10cSrcweir 
81cdf0e10cSrcweir //------------------------------------------------------------------
readSomeBytes(Sequence<sal_Int8> & aData,sal_Int32 nMaxBytesToRead)82cdf0e10cSrcweir sal_Int32 SAL_CALL SequenceInputStream::readSomeBytes( Sequence<sal_Int8>& aData, sal_Int32 nMaxBytesToRead )
83cdf0e10cSrcweir 	throw(NotConnectedException, BufferSizeExceededException,
84cdf0e10cSrcweir 		  IOException, RuntimeException)
85cdf0e10cSrcweir {
86cdf0e10cSrcweir 	// all data is available at once
87cdf0e10cSrcweir 	return readBytes(aData, nMaxBytesToRead);
88cdf0e10cSrcweir }
89cdf0e10cSrcweir 
90cdf0e10cSrcweir //------------------------------------------------------------------
skipBytes(sal_Int32 nBytesToSkip)91cdf0e10cSrcweir void SAL_CALL SequenceInputStream::skipBytes( sal_Int32 nBytesToSkip )
92cdf0e10cSrcweir 	throw(NotConnectedException, BufferSizeExceededException,
93cdf0e10cSrcweir 		  IOException, RuntimeException)
94cdf0e10cSrcweir {
95cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
96cdf0e10cSrcweir 
97cdf0e10cSrcweir 	sal_Int32 nAvail = avail();
98cdf0e10cSrcweir 
99cdf0e10cSrcweir 	if (nBytesToSkip < 0)
100cdf0e10cSrcweir 		throw BufferSizeExceededException(::rtl::OUString(),*this);
101cdf0e10cSrcweir 
102cdf0e10cSrcweir 	if (nAvail < nBytesToSkip)
103cdf0e10cSrcweir 		nBytesToSkip = nAvail;
104cdf0e10cSrcweir 
105cdf0e10cSrcweir 	m_nPos += nBytesToSkip;
106cdf0e10cSrcweir }
107cdf0e10cSrcweir 
108cdf0e10cSrcweir //------------------------------------------------------------------
available()109cdf0e10cSrcweir sal_Int32 SAL_CALL SequenceInputStream::available(  )
110cdf0e10cSrcweir 	throw(NotConnectedException, IOException, RuntimeException)
111cdf0e10cSrcweir {
112cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
113cdf0e10cSrcweir 
114cdf0e10cSrcweir 	return avail();
115cdf0e10cSrcweir }
116cdf0e10cSrcweir 
117cdf0e10cSrcweir //------------------------------------------------------------------
closeInput()118cdf0e10cSrcweir void SAL_CALL SequenceInputStream::closeInput(  )
119cdf0e10cSrcweir 	throw(NotConnectedException, IOException, RuntimeException)
120cdf0e10cSrcweir {
121cdf0e10cSrcweir 	if (m_nPos == -1)
122cdf0e10cSrcweir 		throw NotConnectedException(::rtl::OUString(), *this);
123cdf0e10cSrcweir 
124cdf0e10cSrcweir 	m_nPos = -1;
125cdf0e10cSrcweir }
126cdf0e10cSrcweir 
seek(sal_Int64 location)127cdf0e10cSrcweir void SAL_CALL SequenceInputStream::seek( sal_Int64 location ) throw (IllegalArgumentException, IOException, RuntimeException)
128cdf0e10cSrcweir {
129cdf0e10cSrcweir     if ( location > m_aData.getLength() || location < 0 || location > SAL_MAX_INT32 )
130cdf0e10cSrcweir         throw IllegalArgumentException();
131cdf0e10cSrcweir     m_nPos = (sal_Int32) location;
132cdf0e10cSrcweir }
133cdf0e10cSrcweir 
getPosition()134cdf0e10cSrcweir sal_Int64 SAL_CALL SequenceInputStream::getPosition() throw (IOException, RuntimeException)
135cdf0e10cSrcweir {
136cdf0e10cSrcweir     return m_nPos;
137cdf0e10cSrcweir }
138cdf0e10cSrcweir 
getLength()139cdf0e10cSrcweir sal_Int64 SAL_CALL SequenceInputStream::getLength(  ) throw (IOException, RuntimeException)
140cdf0e10cSrcweir {
141cdf0e10cSrcweir     return m_aData.getLength();
142cdf0e10cSrcweir }
143cdf0e10cSrcweir 
144cdf0e10cSrcweir //--------------------------------------------------------------------------
OSequenceOutputStream(Sequence<sal_Int8> & _rSeq,double _nResizeFactor,sal_Int32 _nMinimumResize,sal_Int32 _nMaximumResize)145cdf0e10cSrcweir OSequenceOutputStream::OSequenceOutputStream(Sequence< sal_Int8 >& _rSeq, double _nResizeFactor, sal_Int32 _nMinimumResize, sal_Int32 _nMaximumResize)
146cdf0e10cSrcweir 	:m_rSequence(_rSeq)
147cdf0e10cSrcweir 	,m_nResizeFactor(_nResizeFactor)
148cdf0e10cSrcweir 	,m_nMinimumResize(_nMinimumResize)
149cdf0e10cSrcweir 	,m_nMaximumResize(_nMaximumResize)
150cdf0e10cSrcweir 	,m_nSize(0)	// starting at position 0
151cdf0e10cSrcweir 	,m_bConnected(sal_True)
152cdf0e10cSrcweir {
153cdf0e10cSrcweir 	OSL_ENSURE(m_nResizeFactor > 1, "OSequenceOutputStream::OSequenceOutputStream : invalid resize factor !");
154cdf0e10cSrcweir 	OSL_ENSURE((m_nMaximumResize < 0) || (m_nMaximumResize > m_nMinimumResize),
155cdf0e10cSrcweir 		"OSequenceOutputStream::OSequenceOutputStream : these limits don't make any sense !");
156cdf0e10cSrcweir 
157cdf0e10cSrcweir 	if (m_nResizeFactor <= 1)
158cdf0e10cSrcweir 		m_nResizeFactor = 1.3;
159cdf0e10cSrcweir 	if ((m_nMaximumResize >= 0) && (m_nMaximumResize <= m_nMinimumResize))
160cdf0e10cSrcweir 		m_nMaximumResize = m_nMinimumResize * 2;
161cdf0e10cSrcweir 		// this heuristic is as good as any other ... supply better parameters if you don't like it :)
162cdf0e10cSrcweir }
163cdf0e10cSrcweir 
164cdf0e10cSrcweir //--------------------------------------------------------------------------
writeBytes(const Sequence<sal_Int8> & _rData)165cdf0e10cSrcweir void SAL_CALL OSequenceOutputStream::writeBytes( const Sequence< sal_Int8 >& _rData ) throw(NotConnectedException, BufferSizeExceededException, IOException, RuntimeException)
166cdf0e10cSrcweir {
167cdf0e10cSrcweir 	MutexGuard aGuard(m_aMutex);
168cdf0e10cSrcweir 	if (!m_bConnected)
169cdf0e10cSrcweir 		throw NotConnectedException();
170cdf0e10cSrcweir 
171cdf0e10cSrcweir 	// ensure the sequence has enoungh space left
172cdf0e10cSrcweir 	if (m_nSize + _rData.getLength() > m_rSequence.getLength())
173cdf0e10cSrcweir 	{
174cdf0e10cSrcweir 		sal_Int32 nCurrentLength = m_rSequence.getLength();
175cdf0e10cSrcweir 		sal_Int32 nNewLength = static_cast< sal_Int32 >(
176cdf0e10cSrcweir             nCurrentLength * m_nResizeFactor);
177cdf0e10cSrcweir 
178cdf0e10cSrcweir 		if (m_nMinimumResize > nNewLength - nCurrentLength)
179cdf0e10cSrcweir 			// we have a minimum so it's not too inefficient for small sequences and small write requests
180cdf0e10cSrcweir 			nNewLength = nCurrentLength + m_nMinimumResize;
181cdf0e10cSrcweir 
182cdf0e10cSrcweir 		if ((m_nMaximumResize > 0) && (nNewLength - nCurrentLength > m_nMaximumResize))
183cdf0e10cSrcweir 			// such a large step is not allowed
184cdf0e10cSrcweir 			nNewLength = nCurrentLength + m_nMaximumResize;
185cdf0e10cSrcweir 
186cdf0e10cSrcweir 		if (nNewLength < m_nSize + _rData.getLength())
187cdf0e10cSrcweir 		{	// it's not enough .... the data would not fit
188cdf0e10cSrcweir 
189cdf0e10cSrcweir 			// let's take the double amount of the length of the data to be written, as the next write
190cdf0e10cSrcweir 			// request could be as large as this one
191cdf0e10cSrcweir 			sal_Int32 nNewGrowth = _rData.getLength() * 2;
192cdf0e10cSrcweir 			if ((m_nMaximumResize > 0) && (nNewGrowth > m_nMaximumResize))
193cdf0e10cSrcweir 			{	// we came to the limit, again ...
194cdf0e10cSrcweir 				nNewGrowth = m_nMaximumResize;
195cdf0e10cSrcweir 				if (nNewGrowth + nCurrentLength < m_nSize + _rData.getLength())
196cdf0e10cSrcweir 					// but it would not fit if we respect the limit
197cdf0e10cSrcweir 					nNewGrowth = m_nSize + _rData.getLength() - nCurrentLength;
198cdf0e10cSrcweir 			}
199cdf0e10cSrcweir 			nNewLength = nCurrentLength + nNewGrowth;
200cdf0e10cSrcweir 		}
201cdf0e10cSrcweir 
202cdf0e10cSrcweir 		// round it off to the next multiple of 4 ...
203cdf0e10cSrcweir 		nNewLength = (nNewLength + 3) / 4 * 4;
204cdf0e10cSrcweir 
205cdf0e10cSrcweir 		m_rSequence.realloc(nNewLength);
206cdf0e10cSrcweir 	}
207cdf0e10cSrcweir 
208cdf0e10cSrcweir 	OSL_ENSURE(m_rSequence.getLength() >= m_nSize + _rData.getLength(),
209cdf0e10cSrcweir 		"ooops ... the realloc algorithm seems to be wrong :( !");
210cdf0e10cSrcweir 
211cdf0e10cSrcweir 	memcpy(m_rSequence.getArray() + m_nSize, _rData.getConstArray(), _rData.getLength());
212cdf0e10cSrcweir 	m_nSize += _rData.getLength();
213cdf0e10cSrcweir }
214cdf0e10cSrcweir 
215cdf0e10cSrcweir //--------------------------------------------------------------------------
flush()216cdf0e10cSrcweir void SAL_CALL OSequenceOutputStream::flush(  ) throw(NotConnectedException, BufferSizeExceededException, IOException, RuntimeException)
217cdf0e10cSrcweir {
218cdf0e10cSrcweir 	MutexGuard aGuard(m_aMutex);
219cdf0e10cSrcweir 	if (!m_bConnected)
220cdf0e10cSrcweir 		throw NotConnectedException();
221cdf0e10cSrcweir 
222cdf0e10cSrcweir     // cut the sequence to the real size
223cdf0e10cSrcweir     m_rSequence.realloc(m_nSize);
224cdf0e10cSrcweir }
225cdf0e10cSrcweir 
226cdf0e10cSrcweir //--------------------------------------------------------------------------
closeOutput()227cdf0e10cSrcweir void SAL_CALL OSequenceOutputStream::closeOutput(  ) throw(NotConnectedException, BufferSizeExceededException, IOException, RuntimeException)
228cdf0e10cSrcweir {
229cdf0e10cSrcweir 	MutexGuard aGuard(m_aMutex);
230cdf0e10cSrcweir 	if (!m_bConnected)
231cdf0e10cSrcweir 		throw NotConnectedException();
232cdf0e10cSrcweir 
233cdf0e10cSrcweir 	// cut the sequence to the real size
234cdf0e10cSrcweir 	m_rSequence.realloc(m_nSize);
235cdf0e10cSrcweir 	// and don't allow any further accesses
236cdf0e10cSrcweir 	m_bConnected = sal_False;
237cdf0e10cSrcweir }
238cdf0e10cSrcweir 
239cdf0e10cSrcweir } // namespace comphelper
240