xref: /AOO41X/main/io/source/stm/streamhelper.cxx (revision 3716f815df2d68347af345f8524e39097ef453f6) !
1*3716f815SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*3716f815SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*3716f815SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*3716f815SAndrew Rist  * distributed with this work for additional information
6*3716f815SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*3716f815SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*3716f815SAndrew Rist  * "License"); you may not use this file except in compliance
9*3716f815SAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11*3716f815SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13*3716f815SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*3716f815SAndrew Rist  * software distributed under the License is distributed on an
15*3716f815SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*3716f815SAndrew Rist  * KIND, either express or implied.  See the License for the
17*3716f815SAndrew Rist  * specific language governing permissions and limitations
18*3716f815SAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20*3716f815SAndrew Rist  *************************************************************/
21*3716f815SAndrew Rist 
22*3716f815SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_io.hxx"
26cdf0e10cSrcweir #include <rtl/alloc.h>
27cdf0e10cSrcweir 
28cdf0e10cSrcweir #include <limits>
29cdf0e10cSrcweir #include <string.h>
30cdf0e10cSrcweir 
31cdf0e10cSrcweir #include <com/sun/star/uno/Sequence.hxx>
32cdf0e10cSrcweir 
33cdf0e10cSrcweir #include <com/sun/star/uno/Exception.hpp>
34cdf0e10cSrcweir 
35cdf0e10cSrcweir using namespace ::com::sun::star::uno;
36cdf0e10cSrcweir 
37cdf0e10cSrcweir #include "streamhelper.hxx"
38cdf0e10cSrcweir 
39cdf0e10cSrcweir namespace io_stm {
40cdf0e10cSrcweir 
write(const Sequence<sal_Int8> & seq)41cdf0e10cSrcweir void MemFIFO::write( const Sequence< sal_Int8 > &seq )
42cdf0e10cSrcweir 	throw ( IFIFO_OutOfMemoryException,
43cdf0e10cSrcweir 			IFIFO_OutOfBoundsException )
44cdf0e10cSrcweir {
45cdf0e10cSrcweir 	try
46cdf0e10cSrcweir 	{
47cdf0e10cSrcweir 		writeAt(getSize(), seq );
48cdf0e10cSrcweir 	}
49cdf0e10cSrcweir 	catch( IRingBuffer_OutOfMemoryException & )
50cdf0e10cSrcweir 	{
51cdf0e10cSrcweir 		throw IFIFO_OutOfMemoryException();
52cdf0e10cSrcweir 	}
53cdf0e10cSrcweir 	catch( IRingBuffer_OutOfBoundsException & )
54cdf0e10cSrcweir 	{
55cdf0e10cSrcweir 		throw IFIFO_OutOfBoundsException();
56cdf0e10cSrcweir 	}
57cdf0e10cSrcweir }
58cdf0e10cSrcweir 
read(Sequence<sal_Int8> & seq,sal_Int32 nBufferLen)59cdf0e10cSrcweir void MemFIFO::read( Sequence<sal_Int8> &seq , sal_Int32 nBufferLen ) throw (IFIFO_OutOfBoundsException)
60cdf0e10cSrcweir {
61cdf0e10cSrcweir 	try
62cdf0e10cSrcweir 	{
63cdf0e10cSrcweir 		readAt(0, seq , nBufferLen);
64cdf0e10cSrcweir 		forgetFromStart( nBufferLen );
65cdf0e10cSrcweir 	}
66cdf0e10cSrcweir 	catch ( IRingBuffer_OutOfBoundsException & )
67cdf0e10cSrcweir 	{
68cdf0e10cSrcweir 		throw IFIFO_OutOfBoundsException();
69cdf0e10cSrcweir 	}
70cdf0e10cSrcweir }
71cdf0e10cSrcweir 
skip(sal_Int32 nBytesToSkip)72cdf0e10cSrcweir void MemFIFO::skip( sal_Int32 nBytesToSkip ) throw ( IFIFO_OutOfBoundsException )
73cdf0e10cSrcweir {
74cdf0e10cSrcweir 	try
75cdf0e10cSrcweir 	{
76cdf0e10cSrcweir 		forgetFromStart( nBytesToSkip );
77cdf0e10cSrcweir 	}
78cdf0e10cSrcweir 	catch( IRingBuffer_OutOfBoundsException & )
79cdf0e10cSrcweir 	{
80cdf0e10cSrcweir 		throw IFIFO_OutOfBoundsException();
81cdf0e10cSrcweir 	}
82cdf0e10cSrcweir }
83cdf0e10cSrcweir 
84cdf0e10cSrcweir 
85cdf0e10cSrcweir 
MemRingBuffer()86cdf0e10cSrcweir MemRingBuffer::MemRingBuffer()
87cdf0e10cSrcweir {
88cdf0e10cSrcweir 	m_nBufferLen 			= 0;
89cdf0e10cSrcweir 	m_p 					= 0;
90cdf0e10cSrcweir 	m_nStart 				= 0;
91cdf0e10cSrcweir 	m_nOccupiedBuffer		= 0;
92cdf0e10cSrcweir }
93cdf0e10cSrcweir 
~MemRingBuffer()94cdf0e10cSrcweir MemRingBuffer::~MemRingBuffer()
95cdf0e10cSrcweir {
96cdf0e10cSrcweir 	if( m_p ) {
97cdf0e10cSrcweir 		rtl_freeMemory( m_p );
98cdf0e10cSrcweir 	}
99cdf0e10cSrcweir }
100cdf0e10cSrcweir 
resizeBuffer(sal_Int32 nMinSize)101cdf0e10cSrcweir void MemRingBuffer::resizeBuffer( sal_Int32 nMinSize ) throw( IRingBuffer_OutOfMemoryException)
102cdf0e10cSrcweir {
103cdf0e10cSrcweir 	sal_Int32 nNewLen = 1;
104cdf0e10cSrcweir 
105cdf0e10cSrcweir 	while( nMinSize > nNewLen ) {
106cdf0e10cSrcweir 		nNewLen = nNewLen << 1;
107cdf0e10cSrcweir 	}
108cdf0e10cSrcweir 
109cdf0e10cSrcweir 	// buffer never shrinks !
110cdf0e10cSrcweir 	if( nNewLen < m_nBufferLen ) {
111cdf0e10cSrcweir 		nNewLen = m_nBufferLen;
112cdf0e10cSrcweir 	}
113cdf0e10cSrcweir 
114cdf0e10cSrcweir 	if( nNewLen != m_nBufferLen ) {
115cdf0e10cSrcweir 		m_p = ( sal_Int8 * ) rtl_reallocateMemory( m_p , nNewLen );
116cdf0e10cSrcweir 		if( !m_p ) {
117cdf0e10cSrcweir 			throw IRingBuffer_OutOfMemoryException();
118cdf0e10cSrcweir 		}
119cdf0e10cSrcweir 
120cdf0e10cSrcweir 		if( m_nStart + m_nOccupiedBuffer > m_nBufferLen ) {
121cdf0e10cSrcweir 			memmove( &( m_p[m_nStart+(nNewLen-m_nBufferLen)]) , &(m_p[m_nStart]) , m_nBufferLen - m_nStart );
122cdf0e10cSrcweir 			m_nStart += nNewLen - m_nBufferLen;
123cdf0e10cSrcweir 		}
124cdf0e10cSrcweir 		m_nBufferLen = nNewLen;
125cdf0e10cSrcweir 	}
126cdf0e10cSrcweir }
127cdf0e10cSrcweir 
128cdf0e10cSrcweir 
readAt(sal_Int32 nPos,Sequence<sal_Int8> & seq,sal_Int32 nBytesToRead) const129cdf0e10cSrcweir void MemRingBuffer::readAt( sal_Int32 nPos, Sequence<sal_Int8> &seq , sal_Int32 nBytesToRead ) const
130cdf0e10cSrcweir 														throw(IRingBuffer_OutOfBoundsException)
131cdf0e10cSrcweir {
132cdf0e10cSrcweir 	if( nPos + nBytesToRead > m_nOccupiedBuffer ) {
133cdf0e10cSrcweir 		throw IRingBuffer_OutOfBoundsException();
134cdf0e10cSrcweir 	}
135cdf0e10cSrcweir 
136cdf0e10cSrcweir 	sal_Int32 nStartReadingPos = nPos + m_nStart;
137cdf0e10cSrcweir 	if( nStartReadingPos >= m_nBufferLen ) {
138cdf0e10cSrcweir 		nStartReadingPos -= m_nBufferLen;
139cdf0e10cSrcweir 	}
140cdf0e10cSrcweir 
141cdf0e10cSrcweir 	seq.realloc( nBytesToRead );
142cdf0e10cSrcweir 
143cdf0e10cSrcweir 	if( nStartReadingPos + nBytesToRead > m_nBufferLen ) {
144cdf0e10cSrcweir 		sal_Int32 nDeltaLen = m_nBufferLen - nStartReadingPos;
145cdf0e10cSrcweir 		memcpy( seq.getArray() , &(m_p[nStartReadingPos]) , nDeltaLen );
146cdf0e10cSrcweir 		memcpy( &(seq.getArray()[nDeltaLen]), m_p , nBytesToRead - nDeltaLen );
147cdf0e10cSrcweir 	}
148cdf0e10cSrcweir 	else {
149cdf0e10cSrcweir 		memcpy( seq.getArray() , &(m_p[nStartReadingPos]) , nBytesToRead );
150cdf0e10cSrcweir 	}
151cdf0e10cSrcweir }
152cdf0e10cSrcweir 
153cdf0e10cSrcweir 
writeAt(sal_Int32 nPos,const Sequence<sal_Int8> & seq)154cdf0e10cSrcweir void MemRingBuffer::writeAt( sal_Int32 nPos, const Sequence<sal_Int8> &seq )
155cdf0e10cSrcweir 														throw (IRingBuffer_OutOfBoundsException,
156cdf0e10cSrcweir 																IRingBuffer_OutOfMemoryException )
157cdf0e10cSrcweir {
158cdf0e10cSrcweir 	checkInvariants();
159cdf0e10cSrcweir 	sal_Int32 nLen = seq.getLength();
160cdf0e10cSrcweir 
161cdf0e10cSrcweir 	if( nPos < 0 || nPos > std::numeric_limits< sal_Int32 >::max() - nLen )
162cdf0e10cSrcweir 	{
163cdf0e10cSrcweir 		throw IRingBuffer_OutOfBoundsException();
164cdf0e10cSrcweir 	}
165cdf0e10cSrcweir 
166cdf0e10cSrcweir 	if( nPos + nLen - m_nOccupiedBuffer > 0 ) {
167cdf0e10cSrcweir 		resizeBuffer( nPos + seq.getLength() );
168cdf0e10cSrcweir 	}
169cdf0e10cSrcweir 
170cdf0e10cSrcweir 	sal_Int32 nStartWritingIndex = m_nStart + nPos;
171cdf0e10cSrcweir 	if( nStartWritingIndex >= m_nBufferLen ) {
172cdf0e10cSrcweir 		nStartWritingIndex -= m_nBufferLen;
173cdf0e10cSrcweir 	}
174cdf0e10cSrcweir 
175cdf0e10cSrcweir 	if( nLen + nStartWritingIndex > m_nBufferLen ) {
176cdf0e10cSrcweir 		// two area copy
177cdf0e10cSrcweir 		memcpy( &(m_p[nStartWritingIndex]) , seq.getConstArray(), m_nBufferLen-nStartWritingIndex );
178cdf0e10cSrcweir 		memcpy( m_p , &( seq.getConstArray()[m_nBufferLen-nStartWritingIndex] ),
179cdf0e10cSrcweir 										nLen - (m_nBufferLen-nStartWritingIndex) );
180cdf0e10cSrcweir 
181cdf0e10cSrcweir 	}
182cdf0e10cSrcweir 	else {
183cdf0e10cSrcweir 		// one area copy
184cdf0e10cSrcweir 		memcpy( &( m_p[nStartWritingIndex]), seq.getConstArray() , nLen );
185cdf0e10cSrcweir 	}
186cdf0e10cSrcweir 	m_nOccupiedBuffer = Max( nPos + seq.getLength() , m_nOccupiedBuffer );
187cdf0e10cSrcweir 	checkInvariants();
188cdf0e10cSrcweir }
189cdf0e10cSrcweir 
190cdf0e10cSrcweir 
getSize() const191cdf0e10cSrcweir sal_Int32 MemRingBuffer::getSize()  const throw()
192cdf0e10cSrcweir {
193cdf0e10cSrcweir 	return m_nOccupiedBuffer;
194cdf0e10cSrcweir }
195cdf0e10cSrcweir 
forgetFromStart(sal_Int32 nBytesToForget)196cdf0e10cSrcweir void MemRingBuffer::forgetFromStart( sal_Int32 nBytesToForget ) throw (IRingBuffer_OutOfBoundsException)
197cdf0e10cSrcweir {
198cdf0e10cSrcweir 	checkInvariants();
199cdf0e10cSrcweir 	if( nBytesToForget > m_nOccupiedBuffer ) {
200cdf0e10cSrcweir 		throw IRingBuffer_OutOfBoundsException();
201cdf0e10cSrcweir 	}
202cdf0e10cSrcweir 	m_nStart += nBytesToForget;
203cdf0e10cSrcweir 	if( m_nStart >= m_nBufferLen ) {
204cdf0e10cSrcweir 		m_nStart = m_nStart - m_nBufferLen;
205cdf0e10cSrcweir 	}
206cdf0e10cSrcweir 	m_nOccupiedBuffer -= nBytesToForget;
207cdf0e10cSrcweir 	checkInvariants();
208cdf0e10cSrcweir }
209cdf0e10cSrcweir 
210cdf0e10cSrcweir 
forgetFromEnd(sal_Int32 nBytesToForget)211cdf0e10cSrcweir void MemRingBuffer::forgetFromEnd( sal_Int32 nBytesToForget ) throw (IRingBuffer_OutOfBoundsException)
212cdf0e10cSrcweir {
213cdf0e10cSrcweir 	checkInvariants();
214cdf0e10cSrcweir 	if( nBytesToForget > m_nOccupiedBuffer ) {
215cdf0e10cSrcweir 		throw IRingBuffer_OutOfBoundsException();
216cdf0e10cSrcweir 	}
217cdf0e10cSrcweir 	m_nOccupiedBuffer -= nBytesToForget;
218cdf0e10cSrcweir 	checkInvariants();
219cdf0e10cSrcweir }
220cdf0e10cSrcweir 
221cdf0e10cSrcweir 
shrink()222cdf0e10cSrcweir void MemRingBuffer::shrink() throw ()
223cdf0e10cSrcweir {
224cdf0e10cSrcweir 	checkInvariants();
225cdf0e10cSrcweir 
226cdf0e10cSrcweir 	// Up to now, only shrinking of while buffer works.
227cdf0e10cSrcweir 	// No other shrinking supported up to now.
228cdf0e10cSrcweir 	if( ! m_nOccupiedBuffer ) {
229cdf0e10cSrcweir 		if( m_p ) {
230cdf0e10cSrcweir 			free( m_p );
231cdf0e10cSrcweir 		}
232cdf0e10cSrcweir 		m_p = 0;
233cdf0e10cSrcweir 		m_nBufferLen = 0;
234cdf0e10cSrcweir 		m_nStart = 0;
235cdf0e10cSrcweir 	}
236cdf0e10cSrcweir 
237cdf0e10cSrcweir 	checkInvariants();
238cdf0e10cSrcweir }
239cdf0e10cSrcweir 
240cdf0e10cSrcweir }
241