xref: /AOO41X/main/xmlhelp/source/cxxhelp/provider/bufferedinputstream.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 
29*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
30*cdf0e10cSrcweir #include "precompiled_xmlhelp.hxx"
31*cdf0e10cSrcweir #include <rtl/memory.h>
32*cdf0e10cSrcweir #include "bufferedinputstream.hxx"
33*cdf0e10cSrcweir 
34*cdf0e10cSrcweir 
35*cdf0e10cSrcweir using namespace cppu;
36*cdf0e10cSrcweir using namespace com::sun::star::uno;
37*cdf0e10cSrcweir using namespace com::sun::star::lang;
38*cdf0e10cSrcweir using namespace com::sun::star::io;
39*cdf0e10cSrcweir using namespace chelp;
40*cdf0e10cSrcweir 
41*cdf0e10cSrcweir 
42*cdf0e10cSrcweir Reference<XInputStream> chelp::turnToSeekable(const Reference<XInputStream>& xInputStream)
43*cdf0e10cSrcweir {
44*cdf0e10cSrcweir 	if( ! xInputStream.is() )
45*cdf0e10cSrcweir 		return xInputStream;
46*cdf0e10cSrcweir 
47*cdf0e10cSrcweir 	Reference<XSeekable> xSeekable(xInputStream,UNO_QUERY);
48*cdf0e10cSrcweir 
49*cdf0e10cSrcweir 	if( xSeekable.is() )
50*cdf0e10cSrcweir 		return xInputStream;
51*cdf0e10cSrcweir 
52*cdf0e10cSrcweir 	return new BufferedInputStream(xInputStream);
53*cdf0e10cSrcweir }
54*cdf0e10cSrcweir 
55*cdf0e10cSrcweir 
56*cdf0e10cSrcweir 
57*cdf0e10cSrcweir BufferedInputStream::BufferedInputStream(const Reference<XInputStream>& xInputStream)
58*cdf0e10cSrcweir 	: m_nBufferLocation(0),
59*cdf0e10cSrcweir 	  m_nBufferSize(0),
60*cdf0e10cSrcweir 	  m_pBuffer(new sal_Int8[1]) // Initialize with one to avoid gcc compiler warnings
61*cdf0e10cSrcweir {
62*cdf0e10cSrcweir 	try
63*cdf0e10cSrcweir 	{
64*cdf0e10cSrcweir 		sal_Int32 num;
65*cdf0e10cSrcweir 		sal_Int8  *tmp;
66*cdf0e10cSrcweir 		Sequence< sal_Int8 > aData(4096);
67*cdf0e10cSrcweir 		do{
68*cdf0e10cSrcweir 			num = xInputStream->readBytes(aData,4096);
69*cdf0e10cSrcweir 			if( num > 0 )
70*cdf0e10cSrcweir 			{
71*cdf0e10cSrcweir 				tmp = m_pBuffer;
72*cdf0e10cSrcweir 				m_pBuffer = new sal_Int8[m_nBufferSize+num];
73*cdf0e10cSrcweir 				rtl_copyMemory((void *)(m_pBuffer),
74*cdf0e10cSrcweir 							   (void *)(tmp),
75*cdf0e10cSrcweir 							   sal_uInt32(m_nBufferSize));
76*cdf0e10cSrcweir 				rtl_copyMemory((void *)(m_pBuffer+m_nBufferSize),
77*cdf0e10cSrcweir 							   (void *)(aData.getArray()),
78*cdf0e10cSrcweir 							   sal_uInt32(num));
79*cdf0e10cSrcweir 				m_nBufferSize += num;
80*cdf0e10cSrcweir 				delete[] tmp;
81*cdf0e10cSrcweir 			}
82*cdf0e10cSrcweir 		} while( num == 4096 );
83*cdf0e10cSrcweir 	}
84*cdf0e10cSrcweir 	catch( const NotConnectedException&)
85*cdf0e10cSrcweir 	{
86*cdf0e10cSrcweir 	}
87*cdf0e10cSrcweir 	catch( const BufferSizeExceededException&)
88*cdf0e10cSrcweir 	{
89*cdf0e10cSrcweir 	}
90*cdf0e10cSrcweir 	catch( const IOException&)
91*cdf0e10cSrcweir 	{
92*cdf0e10cSrcweir 	}
93*cdf0e10cSrcweir 	catch( const RuntimeException&)
94*cdf0e10cSrcweir 	{
95*cdf0e10cSrcweir 	}
96*cdf0e10cSrcweir 	xInputStream->closeInput();
97*cdf0e10cSrcweir }
98*cdf0e10cSrcweir 
99*cdf0e10cSrcweir 
100*cdf0e10cSrcweir BufferedInputStream::~BufferedInputStream()
101*cdf0e10cSrcweir {
102*cdf0e10cSrcweir 	delete[] m_pBuffer;
103*cdf0e10cSrcweir }
104*cdf0e10cSrcweir 
105*cdf0e10cSrcweir 
106*cdf0e10cSrcweir Any SAL_CALL BufferedInputStream::queryInterface( const Type& rType ) throw( RuntimeException )
107*cdf0e10cSrcweir {
108*cdf0e10cSrcweir 	Any aRet = ::cppu::queryInterface( rType,
109*cdf0e10cSrcweir 									   SAL_STATIC_CAST( XInputStream*,this ),
110*cdf0e10cSrcweir 									   SAL_STATIC_CAST( XSeekable*,this ) );
111*cdf0e10cSrcweir 
112*cdf0e10cSrcweir 	return aRet.hasValue() ? aRet : OWeakObject::queryInterface( rType );
113*cdf0e10cSrcweir }
114*cdf0e10cSrcweir 
115*cdf0e10cSrcweir 
116*cdf0e10cSrcweir void SAL_CALL BufferedInputStream::acquire( void ) throw()
117*cdf0e10cSrcweir {
118*cdf0e10cSrcweir 	OWeakObject::acquire();
119*cdf0e10cSrcweir }
120*cdf0e10cSrcweir 
121*cdf0e10cSrcweir 
122*cdf0e10cSrcweir void SAL_CALL BufferedInputStream::release( void ) throw()
123*cdf0e10cSrcweir {
124*cdf0e10cSrcweir 	OWeakObject::release();
125*cdf0e10cSrcweir }
126*cdf0e10cSrcweir 
127*cdf0e10cSrcweir 
128*cdf0e10cSrcweir 
129*cdf0e10cSrcweir sal_Int32 SAL_CALL BufferedInputStream::readBytes( Sequence< sal_Int8 >& aData,sal_Int32 nBytesToRead )
130*cdf0e10cSrcweir 	throw( NotConnectedException,
131*cdf0e10cSrcweir 		   BufferSizeExceededException,
132*cdf0e10cSrcweir 		   IOException,
133*cdf0e10cSrcweir 		   RuntimeException)
134*cdf0e10cSrcweir {
135*cdf0e10cSrcweir 	osl::MutexGuard aGuard( m_aMutex );
136*cdf0e10cSrcweir 
137*cdf0e10cSrcweir 	if( 0 > nBytesToRead )
138*cdf0e10cSrcweir 		throw BufferSizeExceededException();
139*cdf0e10cSrcweir 
140*cdf0e10cSrcweir 	if( m_nBufferLocation + nBytesToRead > m_nBufferSize )
141*cdf0e10cSrcweir 		nBytesToRead = m_nBufferSize - m_nBufferLocation;
142*cdf0e10cSrcweir 
143*cdf0e10cSrcweir 	if( aData.getLength() < nBytesToRead )
144*cdf0e10cSrcweir 		aData.realloc(nBytesToRead);
145*cdf0e10cSrcweir 
146*cdf0e10cSrcweir 	rtl_copyMemory((void*)(aData.getArray()),
147*cdf0e10cSrcweir 				   (void*)(m_pBuffer+m_nBufferLocation),
148*cdf0e10cSrcweir 				   nBytesToRead);
149*cdf0e10cSrcweir 
150*cdf0e10cSrcweir 	return nBytesToRead;
151*cdf0e10cSrcweir }
152*cdf0e10cSrcweir 
153*cdf0e10cSrcweir 
154*cdf0e10cSrcweir sal_Int32 SAL_CALL BufferedInputStream::readSomeBytes(
155*cdf0e10cSrcweir 	Sequence< sal_Int8 >& aData,sal_Int32 nMaxBytesToRead )
156*cdf0e10cSrcweir 	throw( NotConnectedException,
157*cdf0e10cSrcweir 		   BufferSizeExceededException,
158*cdf0e10cSrcweir 		   IOException,
159*cdf0e10cSrcweir 		   RuntimeException)
160*cdf0e10cSrcweir {
161*cdf0e10cSrcweir 	return readBytes(aData,nMaxBytesToRead);
162*cdf0e10cSrcweir }
163*cdf0e10cSrcweir 
164*cdf0e10cSrcweir 
165*cdf0e10cSrcweir 
166*cdf0e10cSrcweir void SAL_CALL BufferedInputStream::skipBytes( sal_Int32 nBytesToSkip )
167*cdf0e10cSrcweir 	throw( NotConnectedException,
168*cdf0e10cSrcweir 		   BufferSizeExceededException,
169*cdf0e10cSrcweir 		   IOException,
170*cdf0e10cSrcweir 		   RuntimeException )
171*cdf0e10cSrcweir {
172*cdf0e10cSrcweir 	try
173*cdf0e10cSrcweir 	{
174*cdf0e10cSrcweir 		seek(m_nBufferLocation+nBytesToSkip);
175*cdf0e10cSrcweir 	}
176*cdf0e10cSrcweir 	catch( const IllegalArgumentException& )
177*cdf0e10cSrcweir 	{
178*cdf0e10cSrcweir 		throw BufferSizeExceededException();
179*cdf0e10cSrcweir 	}
180*cdf0e10cSrcweir }
181*cdf0e10cSrcweir 
182*cdf0e10cSrcweir 
183*cdf0e10cSrcweir 
184*cdf0e10cSrcweir sal_Int32 SAL_CALL BufferedInputStream::available( void )
185*cdf0e10cSrcweir 	throw( NotConnectedException,
186*cdf0e10cSrcweir 		   IOException,
187*cdf0e10cSrcweir 		   RuntimeException )
188*cdf0e10cSrcweir {
189*cdf0e10cSrcweir 	osl::MutexGuard aGuard( m_aMutex );
190*cdf0e10cSrcweir 	return  m_nBufferSize-m_nBufferLocation;
191*cdf0e10cSrcweir }
192*cdf0e10cSrcweir 
193*cdf0e10cSrcweir 
194*cdf0e10cSrcweir 
195*cdf0e10cSrcweir void SAL_CALL BufferedInputStream::closeInput( void )
196*cdf0e10cSrcweir 	throw( NotConnectedException,
197*cdf0e10cSrcweir 		   IOException,
198*cdf0e10cSrcweir 		   RuntimeException )
199*cdf0e10cSrcweir {
200*cdf0e10cSrcweir }
201*cdf0e10cSrcweir 
202*cdf0e10cSrcweir 
203*cdf0e10cSrcweir void SAL_CALL BufferedInputStream::seek( sal_Int64 location )
204*cdf0e10cSrcweir 	throw( IllegalArgumentException,
205*cdf0e10cSrcweir 		   IOException,
206*cdf0e10cSrcweir 		   RuntimeException )
207*cdf0e10cSrcweir {
208*cdf0e10cSrcweir 	if( 0 <= location && location < m_nBufferSize )
209*cdf0e10cSrcweir 	{
210*cdf0e10cSrcweir 		osl::MutexGuard aGuard( m_aMutex );
211*cdf0e10cSrcweir 		m_nBufferLocation = sal::static_int_cast<sal_Int32>( location );
212*cdf0e10cSrcweir 	}
213*cdf0e10cSrcweir 	else
214*cdf0e10cSrcweir 		throw IllegalArgumentException();
215*cdf0e10cSrcweir }
216*cdf0e10cSrcweir 
217*cdf0e10cSrcweir 
218*cdf0e10cSrcweir 
219*cdf0e10cSrcweir sal_Int64 SAL_CALL BufferedInputStream::getPosition( void )
220*cdf0e10cSrcweir 	throw( IOException,
221*cdf0e10cSrcweir 		   RuntimeException )
222*cdf0e10cSrcweir {
223*cdf0e10cSrcweir 	osl::MutexGuard aGuard( m_aMutex );
224*cdf0e10cSrcweir 	return m_nBufferLocation;
225*cdf0e10cSrcweir }
226*cdf0e10cSrcweir 
227*cdf0e10cSrcweir 
228*cdf0e10cSrcweir 
229*cdf0e10cSrcweir sal_Int64 SAL_CALL BufferedInputStream::getLength( void ) throw( IOException,RuntimeException )
230*cdf0e10cSrcweir {
231*cdf0e10cSrcweir 	osl::MutexGuard aGuard( m_aMutex );
232*cdf0e10cSrcweir 	return m_nBufferSize;
233*cdf0e10cSrcweir }
234