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_svl.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include "oinputstreamcontainer.hxx"
32*cdf0e10cSrcweir #include <cppuhelper/typeprovider.hxx>
33*cdf0e10cSrcweir 
34*cdf0e10cSrcweir using namespace ::com::sun::star;
35*cdf0e10cSrcweir 
36*cdf0e10cSrcweir //-----------------------------------------------
37*cdf0e10cSrcweir OFSInputStreamContainer::OFSInputStreamContainer( const uno::Reference< io::XInputStream >& xStream )
38*cdf0e10cSrcweir : m_xInputStream( xStream )
39*cdf0e10cSrcweir , m_xSeekable( xStream, uno::UNO_QUERY )
40*cdf0e10cSrcweir , m_bSeekable( sal_False )
41*cdf0e10cSrcweir , m_bDisposed( sal_False )
42*cdf0e10cSrcweir , m_pListenersContainer( NULL )
43*cdf0e10cSrcweir {
44*cdf0e10cSrcweir 	m_bSeekable = m_xSeekable.is();
45*cdf0e10cSrcweir }
46*cdf0e10cSrcweir 
47*cdf0e10cSrcweir //-----------------------------------------------
48*cdf0e10cSrcweir OFSInputStreamContainer::~OFSInputStreamContainer()
49*cdf0e10cSrcweir {
50*cdf0e10cSrcweir 	if ( m_pListenersContainer )
51*cdf0e10cSrcweir 	{
52*cdf0e10cSrcweir 		delete m_pListenersContainer;
53*cdf0e10cSrcweir 		m_pListenersContainer = NULL;
54*cdf0e10cSrcweir 	}
55*cdf0e10cSrcweir }
56*cdf0e10cSrcweir 
57*cdf0e10cSrcweir //-----------------------------------------------
58*cdf0e10cSrcweir uno::Sequence< uno::Type > SAL_CALL OFSInputStreamContainer::getTypes()
59*cdf0e10cSrcweir 		throw ( uno::RuntimeException )
60*cdf0e10cSrcweir {
61*cdf0e10cSrcweir 	static ::cppu::OTypeCollection* pTypeCollection = NULL ;
62*cdf0e10cSrcweir 
63*cdf0e10cSrcweir 	if ( pTypeCollection == NULL )
64*cdf0e10cSrcweir 	{
65*cdf0e10cSrcweir 		::osl::MutexGuard aGuard( m_aMutex ) ;
66*cdf0e10cSrcweir 
67*cdf0e10cSrcweir 		if ( pTypeCollection == NULL )
68*cdf0e10cSrcweir 		{
69*cdf0e10cSrcweir 			if ( m_bSeekable )
70*cdf0e10cSrcweir 			{
71*cdf0e10cSrcweir 				static ::cppu::OTypeCollection aTypeCollection(
72*cdf0e10cSrcweir             			::getCppuType(( const uno::Reference< io::XStream >* )NULL ),
73*cdf0e10cSrcweir             			::getCppuType(( const uno::Reference< io::XInputStream >* )NULL ),
74*cdf0e10cSrcweir             			::getCppuType(( const uno::Reference< io::XSeekable >* )NULL ) );
75*cdf0e10cSrcweir 
76*cdf0e10cSrcweir 				pTypeCollection = &aTypeCollection ;
77*cdf0e10cSrcweir 			}
78*cdf0e10cSrcweir 			else
79*cdf0e10cSrcweir 			{
80*cdf0e10cSrcweir 				static ::cppu::OTypeCollection aTypeCollection(
81*cdf0e10cSrcweir             			::getCppuType(( const uno::Reference< io::XStream >* )NULL ),
82*cdf0e10cSrcweir             			::getCppuType(( const uno::Reference< io::XInputStream >* )NULL ) );
83*cdf0e10cSrcweir 
84*cdf0e10cSrcweir 				pTypeCollection = &aTypeCollection ;
85*cdf0e10cSrcweir 			}
86*cdf0e10cSrcweir 		}
87*cdf0e10cSrcweir 	}
88*cdf0e10cSrcweir 
89*cdf0e10cSrcweir 	return pTypeCollection->getTypes() ;
90*cdf0e10cSrcweir 
91*cdf0e10cSrcweir }
92*cdf0e10cSrcweir 
93*cdf0e10cSrcweir //-----------------------------------------------
94*cdf0e10cSrcweir uno::Any SAL_CALL OFSInputStreamContainer::queryInterface( const uno::Type& rType )
95*cdf0e10cSrcweir 		throw( uno::RuntimeException )
96*cdf0e10cSrcweir {
97*cdf0e10cSrcweir 	// Attention:
98*cdf0e10cSrcweir 	//	Don't use mutex or guard in this method!!! Is a method of XInterface.
99*cdf0e10cSrcweir 
100*cdf0e10cSrcweir 	uno::Any aReturn;
101*cdf0e10cSrcweir 	if ( m_bSeekable )
102*cdf0e10cSrcweir 		aReturn = uno::Any( ::cppu::queryInterface( rType,
103*cdf0e10cSrcweir 									   	static_cast< io::XStream* >( this ),
104*cdf0e10cSrcweir 									   	static_cast< io::XInputStream* >( this ),
105*cdf0e10cSrcweir 									   	static_cast< io::XSeekable* >( this ) ) );
106*cdf0e10cSrcweir 	else
107*cdf0e10cSrcweir 		aReturn = uno::Any( ::cppu::queryInterface( rType,
108*cdf0e10cSrcweir 									   	static_cast< io::XStream* >( this ),
109*cdf0e10cSrcweir 									   	static_cast< io::XInputStream* >( this ) ) );
110*cdf0e10cSrcweir 
111*cdf0e10cSrcweir 	if ( aReturn.hasValue() == sal_True )
112*cdf0e10cSrcweir 		return aReturn ;
113*cdf0e10cSrcweir 
114*cdf0e10cSrcweir 	return ::cppu::OWeakObject::queryInterface( rType ) ;
115*cdf0e10cSrcweir }
116*cdf0e10cSrcweir 
117*cdf0e10cSrcweir //-----------------------------------------------
118*cdf0e10cSrcweir void SAL_CALL OFSInputStreamContainer::acquire()
119*cdf0e10cSrcweir 		throw()
120*cdf0e10cSrcweir {
121*cdf0e10cSrcweir 	::cppu::OWeakObject::acquire();
122*cdf0e10cSrcweir }
123*cdf0e10cSrcweir 
124*cdf0e10cSrcweir //-----------------------------------------------
125*cdf0e10cSrcweir void SAL_CALL OFSInputStreamContainer::release()
126*cdf0e10cSrcweir 		throw()
127*cdf0e10cSrcweir {
128*cdf0e10cSrcweir 	::cppu::OWeakObject::release();
129*cdf0e10cSrcweir }
130*cdf0e10cSrcweir 
131*cdf0e10cSrcweir //-----------------------------------------------
132*cdf0e10cSrcweir sal_Int32 SAL_CALL OFSInputStreamContainer::readBytes( uno::Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead )
133*cdf0e10cSrcweir 		throw ( io::NotConnectedException,
134*cdf0e10cSrcweir 				io::BufferSizeExceededException,
135*cdf0e10cSrcweir 				io::IOException,
136*cdf0e10cSrcweir 				uno::RuntimeException )
137*cdf0e10cSrcweir {
138*cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
139*cdf0e10cSrcweir 
140*cdf0e10cSrcweir 	if ( m_bDisposed )
141*cdf0e10cSrcweir 		throw lang::DisposedException();
142*cdf0e10cSrcweir 
143*cdf0e10cSrcweir 	if ( !m_xInputStream.is() )
144*cdf0e10cSrcweir 		throw uno::RuntimeException();
145*cdf0e10cSrcweir 
146*cdf0e10cSrcweir 	return m_xInputStream->readBytes( aData, nBytesToRead );
147*cdf0e10cSrcweir }
148*cdf0e10cSrcweir 
149*cdf0e10cSrcweir //-----------------------------------------------
150*cdf0e10cSrcweir sal_Int32 SAL_CALL OFSInputStreamContainer::readSomeBytes( uno::Sequence< sal_Int8 >& aData, sal_Int32 nMaxBytesToRead )
151*cdf0e10cSrcweir 		throw ( io::NotConnectedException,
152*cdf0e10cSrcweir 				io::BufferSizeExceededException,
153*cdf0e10cSrcweir 				io::IOException,
154*cdf0e10cSrcweir 				uno::RuntimeException )
155*cdf0e10cSrcweir {
156*cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
157*cdf0e10cSrcweir 
158*cdf0e10cSrcweir 	if ( m_bDisposed )
159*cdf0e10cSrcweir 		throw lang::DisposedException();
160*cdf0e10cSrcweir 
161*cdf0e10cSrcweir 	if ( !m_xInputStream.is() )
162*cdf0e10cSrcweir 		throw uno::RuntimeException();
163*cdf0e10cSrcweir 
164*cdf0e10cSrcweir 	return m_xInputStream->readSomeBytes( aData, nMaxBytesToRead );
165*cdf0e10cSrcweir }
166*cdf0e10cSrcweir 
167*cdf0e10cSrcweir //-----------------------------------------------
168*cdf0e10cSrcweir void SAL_CALL OFSInputStreamContainer::skipBytes( sal_Int32 nBytesToSkip )
169*cdf0e10cSrcweir 		throw ( io::NotConnectedException,
170*cdf0e10cSrcweir 				io::BufferSizeExceededException,
171*cdf0e10cSrcweir 				io::IOException,
172*cdf0e10cSrcweir 				uno::RuntimeException )
173*cdf0e10cSrcweir {
174*cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
175*cdf0e10cSrcweir 
176*cdf0e10cSrcweir 	if ( m_bDisposed )
177*cdf0e10cSrcweir 		throw lang::DisposedException();
178*cdf0e10cSrcweir 
179*cdf0e10cSrcweir 	if ( !m_xInputStream.is() )
180*cdf0e10cSrcweir 		throw uno::RuntimeException();
181*cdf0e10cSrcweir 
182*cdf0e10cSrcweir 	m_xInputStream->skipBytes( nBytesToSkip );
183*cdf0e10cSrcweir }
184*cdf0e10cSrcweir 
185*cdf0e10cSrcweir //-----------------------------------------------
186*cdf0e10cSrcweir sal_Int32 SAL_CALL OFSInputStreamContainer::available(  )
187*cdf0e10cSrcweir 		throw ( io::NotConnectedException,
188*cdf0e10cSrcweir 				io::IOException,
189*cdf0e10cSrcweir 				uno::RuntimeException )
190*cdf0e10cSrcweir {
191*cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
192*cdf0e10cSrcweir 
193*cdf0e10cSrcweir 	if ( m_bDisposed )
194*cdf0e10cSrcweir 		throw lang::DisposedException();
195*cdf0e10cSrcweir 
196*cdf0e10cSrcweir 	if ( !m_xInputStream.is() )
197*cdf0e10cSrcweir 		throw uno::RuntimeException();
198*cdf0e10cSrcweir 
199*cdf0e10cSrcweir 	return m_xInputStream->available();
200*cdf0e10cSrcweir }
201*cdf0e10cSrcweir 
202*cdf0e10cSrcweir //-----------------------------------------------
203*cdf0e10cSrcweir void SAL_CALL OFSInputStreamContainer::closeInput(  )
204*cdf0e10cSrcweir 		throw ( io::NotConnectedException,
205*cdf0e10cSrcweir 				io::IOException,
206*cdf0e10cSrcweir 				uno::RuntimeException )
207*cdf0e10cSrcweir {
208*cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
209*cdf0e10cSrcweir 
210*cdf0e10cSrcweir 	if ( m_bDisposed )
211*cdf0e10cSrcweir 		throw lang::DisposedException();
212*cdf0e10cSrcweir 
213*cdf0e10cSrcweir 	if ( !m_xInputStream.is() )
214*cdf0e10cSrcweir 		throw uno::RuntimeException();
215*cdf0e10cSrcweir 
216*cdf0e10cSrcweir 	dispose();
217*cdf0e10cSrcweir }
218*cdf0e10cSrcweir 
219*cdf0e10cSrcweir //-----------------------------------------------
220*cdf0e10cSrcweir uno::Reference< io::XInputStream > SAL_CALL OFSInputStreamContainer::getInputStream()
221*cdf0e10cSrcweir 		throw ( uno::RuntimeException )
222*cdf0e10cSrcweir {
223*cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
224*cdf0e10cSrcweir 
225*cdf0e10cSrcweir 	if ( m_bDisposed )
226*cdf0e10cSrcweir 		throw lang::DisposedException();
227*cdf0e10cSrcweir 
228*cdf0e10cSrcweir 	if ( !m_xInputStream.is() )
229*cdf0e10cSrcweir 		return uno::Reference< io::XInputStream >();
230*cdf0e10cSrcweir 
231*cdf0e10cSrcweir 	return uno::Reference< io::XInputStream >( static_cast< io::XInputStream* >( this ), uno::UNO_QUERY );
232*cdf0e10cSrcweir }
233*cdf0e10cSrcweir 
234*cdf0e10cSrcweir //-----------------------------------------------
235*cdf0e10cSrcweir uno::Reference< io::XOutputStream > SAL_CALL OFSInputStreamContainer::getOutputStream()
236*cdf0e10cSrcweir 		throw ( uno::RuntimeException )
237*cdf0e10cSrcweir {
238*cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
239*cdf0e10cSrcweir 
240*cdf0e10cSrcweir 	if ( m_bDisposed )
241*cdf0e10cSrcweir 		throw lang::DisposedException();
242*cdf0e10cSrcweir 
243*cdf0e10cSrcweir 	return uno::Reference< io::XOutputStream >();
244*cdf0e10cSrcweir }
245*cdf0e10cSrcweir 
246*cdf0e10cSrcweir //-----------------------------------------------
247*cdf0e10cSrcweir void SAL_CALL OFSInputStreamContainer::seek( sal_Int64 location )
248*cdf0e10cSrcweir 		throw ( lang::IllegalArgumentException,
249*cdf0e10cSrcweir 				io::IOException,
250*cdf0e10cSrcweir 				uno::RuntimeException )
251*cdf0e10cSrcweir {
252*cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
253*cdf0e10cSrcweir 
254*cdf0e10cSrcweir 	if ( m_bDisposed )
255*cdf0e10cSrcweir 		throw lang::DisposedException();
256*cdf0e10cSrcweir 
257*cdf0e10cSrcweir 	if ( !m_xSeekable.is() )
258*cdf0e10cSrcweir 		throw uno::RuntimeException();
259*cdf0e10cSrcweir 
260*cdf0e10cSrcweir 	m_xSeekable->seek( location );
261*cdf0e10cSrcweir }
262*cdf0e10cSrcweir 
263*cdf0e10cSrcweir //-----------------------------------------------
264*cdf0e10cSrcweir sal_Int64 SAL_CALL OFSInputStreamContainer::getPosition()
265*cdf0e10cSrcweir 		throw ( io::IOException,
266*cdf0e10cSrcweir 				uno::RuntimeException)
267*cdf0e10cSrcweir {
268*cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
269*cdf0e10cSrcweir 
270*cdf0e10cSrcweir 	if ( m_bDisposed )
271*cdf0e10cSrcweir 		throw lang::DisposedException();
272*cdf0e10cSrcweir 
273*cdf0e10cSrcweir 	if ( !m_xSeekable.is() )
274*cdf0e10cSrcweir 		throw uno::RuntimeException();
275*cdf0e10cSrcweir 
276*cdf0e10cSrcweir 	return m_xSeekable->getPosition();
277*cdf0e10cSrcweir }
278*cdf0e10cSrcweir 
279*cdf0e10cSrcweir //-----------------------------------------------
280*cdf0e10cSrcweir sal_Int64 SAL_CALL OFSInputStreamContainer::getLength()
281*cdf0e10cSrcweir 		throw ( io::IOException,
282*cdf0e10cSrcweir 				uno::RuntimeException )
283*cdf0e10cSrcweir {
284*cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
285*cdf0e10cSrcweir 
286*cdf0e10cSrcweir 	if ( m_bDisposed )
287*cdf0e10cSrcweir 		throw lang::DisposedException();
288*cdf0e10cSrcweir 
289*cdf0e10cSrcweir 	if ( !m_xSeekable.is() )
290*cdf0e10cSrcweir 		throw uno::RuntimeException();
291*cdf0e10cSrcweir 
292*cdf0e10cSrcweir 	return m_xSeekable->getLength();
293*cdf0e10cSrcweir }
294*cdf0e10cSrcweir 
295*cdf0e10cSrcweir //-----------------------------------------------
296*cdf0e10cSrcweir void SAL_CALL OFSInputStreamContainer::dispose(  )
297*cdf0e10cSrcweir 		throw ( uno::RuntimeException )
298*cdf0e10cSrcweir {
299*cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
300*cdf0e10cSrcweir 
301*cdf0e10cSrcweir 	if ( m_bDisposed )
302*cdf0e10cSrcweir 		throw lang::DisposedException();
303*cdf0e10cSrcweir 
304*cdf0e10cSrcweir 	if ( !m_xInputStream.is() )
305*cdf0e10cSrcweir 		throw uno::RuntimeException();
306*cdf0e10cSrcweir 
307*cdf0e10cSrcweir 	m_xInputStream->closeInput();
308*cdf0e10cSrcweir 
309*cdf0e10cSrcweir 	if ( m_pListenersContainer )
310*cdf0e10cSrcweir 	{
311*cdf0e10cSrcweir     	lang::EventObject aSource( static_cast< ::cppu::OWeakObject*>( this ) );
312*cdf0e10cSrcweir 		m_pListenersContainer->disposeAndClear( aSource );
313*cdf0e10cSrcweir 	}
314*cdf0e10cSrcweir 
315*cdf0e10cSrcweir 	m_bDisposed = sal_True;
316*cdf0e10cSrcweir }
317*cdf0e10cSrcweir 
318*cdf0e10cSrcweir //-----------------------------------------------
319*cdf0e10cSrcweir void SAL_CALL OFSInputStreamContainer::addEventListener( const uno::Reference< lang::XEventListener >& xListener )
320*cdf0e10cSrcweir 		throw ( uno::RuntimeException )
321*cdf0e10cSrcweir {
322*cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
323*cdf0e10cSrcweir 
324*cdf0e10cSrcweir 	if ( m_bDisposed )
325*cdf0e10cSrcweir 		throw lang::DisposedException();
326*cdf0e10cSrcweir 
327*cdf0e10cSrcweir 	if ( !m_pListenersContainer )
328*cdf0e10cSrcweir 		m_pListenersContainer = new ::cppu::OInterfaceContainerHelper( m_aMutex );
329*cdf0e10cSrcweir 
330*cdf0e10cSrcweir 	m_pListenersContainer->addInterface( xListener );
331*cdf0e10cSrcweir }
332*cdf0e10cSrcweir 
333*cdf0e10cSrcweir //-----------------------------------------------
334*cdf0e10cSrcweir void SAL_CALL OFSInputStreamContainer::removeEventListener( const uno::Reference< lang::XEventListener >& xListener )
335*cdf0e10cSrcweir 		throw ( uno::RuntimeException )
336*cdf0e10cSrcweir {
337*cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
338*cdf0e10cSrcweir 
339*cdf0e10cSrcweir 	if ( m_bDisposed )
340*cdf0e10cSrcweir 		throw lang::DisposedException();
341*cdf0e10cSrcweir 
342*cdf0e10cSrcweir 	if ( m_pListenersContainer )
343*cdf0e10cSrcweir 		m_pListenersContainer->removeInterface( xListener );
344*cdf0e10cSrcweir }
345*cdf0e10cSrcweir 
346*cdf0e10cSrcweir 
347*cdf0e10cSrcweir 
348