1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_svl.hxx"
26 
27 #include "oinputstreamcontainer.hxx"
28 #include <cppuhelper/typeprovider.hxx>
29 
30 using namespace ::com::sun::star;
31 
32 //-----------------------------------------------
OFSInputStreamContainer(const uno::Reference<io::XInputStream> & xStream)33 OFSInputStreamContainer::OFSInputStreamContainer( const uno::Reference< io::XInputStream >& xStream )
34 : m_xInputStream( xStream )
35 , m_xSeekable( xStream, uno::UNO_QUERY )
36 , m_bSeekable( sal_False )
37 , m_bDisposed( sal_False )
38 , m_pListenersContainer( NULL )
39 {
40 	m_bSeekable = m_xSeekable.is();
41 }
42 
43 //-----------------------------------------------
~OFSInputStreamContainer()44 OFSInputStreamContainer::~OFSInputStreamContainer()
45 {
46 	if ( m_pListenersContainer )
47 	{
48 		delete m_pListenersContainer;
49 		m_pListenersContainer = NULL;
50 	}
51 }
52 
53 //-----------------------------------------------
getTypes()54 uno::Sequence< uno::Type > SAL_CALL OFSInputStreamContainer::getTypes()
55 		throw ( uno::RuntimeException )
56 {
57 	static ::cppu::OTypeCollection* pTypeCollection = NULL ;
58 
59 	if ( pTypeCollection == NULL )
60 	{
61 		::osl::MutexGuard aGuard( m_aMutex ) ;
62 
63 		if ( pTypeCollection == NULL )
64 		{
65 			if ( m_bSeekable )
66 			{
67 				static ::cppu::OTypeCollection aTypeCollection(
68             			::getCppuType(( const uno::Reference< io::XStream >* )NULL ),
69             			::getCppuType(( const uno::Reference< io::XInputStream >* )NULL ),
70             			::getCppuType(( const uno::Reference< io::XSeekable >* )NULL ) );
71 
72 				pTypeCollection = &aTypeCollection ;
73 			}
74 			else
75 			{
76 				static ::cppu::OTypeCollection aTypeCollection(
77             			::getCppuType(( const uno::Reference< io::XStream >* )NULL ),
78             			::getCppuType(( const uno::Reference< io::XInputStream >* )NULL ) );
79 
80 				pTypeCollection = &aTypeCollection ;
81 			}
82 		}
83 	}
84 
85 	return pTypeCollection->getTypes() ;
86 
87 }
88 
89 //-----------------------------------------------
queryInterface(const uno::Type & rType)90 uno::Any SAL_CALL OFSInputStreamContainer::queryInterface( const uno::Type& rType )
91 		throw( uno::RuntimeException )
92 {
93 	// Attention:
94 	//	Don't use mutex or guard in this method!!! Is a method of XInterface.
95 
96 	uno::Any aReturn;
97 	if ( m_bSeekable )
98 		aReturn = uno::Any( ::cppu::queryInterface( rType,
99 									   	static_cast< io::XStream* >( this ),
100 									   	static_cast< io::XInputStream* >( this ),
101 									   	static_cast< io::XSeekable* >( this ) ) );
102 	else
103 		aReturn = uno::Any( ::cppu::queryInterface( rType,
104 									   	static_cast< io::XStream* >( this ),
105 									   	static_cast< io::XInputStream* >( this ) ) );
106 
107 	if ( aReturn.hasValue() == sal_True )
108 		return aReturn ;
109 
110 	return ::cppu::OWeakObject::queryInterface( rType ) ;
111 }
112 
113 //-----------------------------------------------
acquire()114 void SAL_CALL OFSInputStreamContainer::acquire()
115 		throw()
116 {
117 	::cppu::OWeakObject::acquire();
118 }
119 
120 //-----------------------------------------------
release()121 void SAL_CALL OFSInputStreamContainer::release()
122 		throw()
123 {
124 	::cppu::OWeakObject::release();
125 }
126 
127 //-----------------------------------------------
readBytes(uno::Sequence<sal_Int8> & aData,sal_Int32 nBytesToRead)128 sal_Int32 SAL_CALL OFSInputStreamContainer::readBytes( uno::Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead )
129 		throw ( io::NotConnectedException,
130 				io::BufferSizeExceededException,
131 				io::IOException,
132 				uno::RuntimeException )
133 {
134 	::osl::MutexGuard aGuard( m_aMutex );
135 
136 	if ( m_bDisposed )
137 		throw lang::DisposedException();
138 
139 	if ( !m_xInputStream.is() )
140 		throw uno::RuntimeException();
141 
142 	return m_xInputStream->readBytes( aData, nBytesToRead );
143 }
144 
145 //-----------------------------------------------
readSomeBytes(uno::Sequence<sal_Int8> & aData,sal_Int32 nMaxBytesToRead)146 sal_Int32 SAL_CALL OFSInputStreamContainer::readSomeBytes( uno::Sequence< sal_Int8 >& aData, sal_Int32 nMaxBytesToRead )
147 		throw ( io::NotConnectedException,
148 				io::BufferSizeExceededException,
149 				io::IOException,
150 				uno::RuntimeException )
151 {
152 	::osl::MutexGuard aGuard( m_aMutex );
153 
154 	if ( m_bDisposed )
155 		throw lang::DisposedException();
156 
157 	if ( !m_xInputStream.is() )
158 		throw uno::RuntimeException();
159 
160 	return m_xInputStream->readSomeBytes( aData, nMaxBytesToRead );
161 }
162 
163 //-----------------------------------------------
skipBytes(sal_Int32 nBytesToSkip)164 void SAL_CALL OFSInputStreamContainer::skipBytes( sal_Int32 nBytesToSkip )
165 		throw ( io::NotConnectedException,
166 				io::BufferSizeExceededException,
167 				io::IOException,
168 				uno::RuntimeException )
169 {
170 	::osl::MutexGuard aGuard( m_aMutex );
171 
172 	if ( m_bDisposed )
173 		throw lang::DisposedException();
174 
175 	if ( !m_xInputStream.is() )
176 		throw uno::RuntimeException();
177 
178 	m_xInputStream->skipBytes( nBytesToSkip );
179 }
180 
181 //-----------------------------------------------
available()182 sal_Int32 SAL_CALL OFSInputStreamContainer::available(  )
183 		throw ( io::NotConnectedException,
184 				io::IOException,
185 				uno::RuntimeException )
186 {
187 	::osl::MutexGuard aGuard( m_aMutex );
188 
189 	if ( m_bDisposed )
190 		throw lang::DisposedException();
191 
192 	if ( !m_xInputStream.is() )
193 		throw uno::RuntimeException();
194 
195 	return m_xInputStream->available();
196 }
197 
198 //-----------------------------------------------
closeInput()199 void SAL_CALL OFSInputStreamContainer::closeInput(  )
200 		throw ( io::NotConnectedException,
201 				io::IOException,
202 				uno::RuntimeException )
203 {
204 	::osl::MutexGuard aGuard( m_aMutex );
205 
206 	if ( m_bDisposed )
207 		throw lang::DisposedException();
208 
209 	if ( !m_xInputStream.is() )
210 		throw uno::RuntimeException();
211 
212 	dispose();
213 }
214 
215 //-----------------------------------------------
getInputStream()216 uno::Reference< io::XInputStream > SAL_CALL OFSInputStreamContainer::getInputStream()
217 		throw ( uno::RuntimeException )
218 {
219 	::osl::MutexGuard aGuard( m_aMutex );
220 
221 	if ( m_bDisposed )
222 		throw lang::DisposedException();
223 
224 	if ( !m_xInputStream.is() )
225 		return uno::Reference< io::XInputStream >();
226 
227 	return uno::Reference< io::XInputStream >( static_cast< io::XInputStream* >( this ), uno::UNO_QUERY );
228 }
229 
230 //-----------------------------------------------
getOutputStream()231 uno::Reference< io::XOutputStream > SAL_CALL OFSInputStreamContainer::getOutputStream()
232 		throw ( uno::RuntimeException )
233 {
234 	::osl::MutexGuard aGuard( m_aMutex );
235 
236 	if ( m_bDisposed )
237 		throw lang::DisposedException();
238 
239 	return uno::Reference< io::XOutputStream >();
240 }
241 
242 //-----------------------------------------------
seek(sal_Int64 location)243 void SAL_CALL OFSInputStreamContainer::seek( sal_Int64 location )
244 		throw ( lang::IllegalArgumentException,
245 				io::IOException,
246 				uno::RuntimeException )
247 {
248 	::osl::MutexGuard aGuard( m_aMutex );
249 
250 	if ( m_bDisposed )
251 		throw lang::DisposedException();
252 
253 	if ( !m_xSeekable.is() )
254 		throw uno::RuntimeException();
255 
256 	m_xSeekable->seek( location );
257 }
258 
259 //-----------------------------------------------
getPosition()260 sal_Int64 SAL_CALL OFSInputStreamContainer::getPosition()
261 		throw ( io::IOException,
262 				uno::RuntimeException)
263 {
264 	::osl::MutexGuard aGuard( m_aMutex );
265 
266 	if ( m_bDisposed )
267 		throw lang::DisposedException();
268 
269 	if ( !m_xSeekable.is() )
270 		throw uno::RuntimeException();
271 
272 	return m_xSeekable->getPosition();
273 }
274 
275 //-----------------------------------------------
getLength()276 sal_Int64 SAL_CALL OFSInputStreamContainer::getLength()
277 		throw ( io::IOException,
278 				uno::RuntimeException )
279 {
280 	::osl::MutexGuard aGuard( m_aMutex );
281 
282 	if ( m_bDisposed )
283 		throw lang::DisposedException();
284 
285 	if ( !m_xSeekable.is() )
286 		throw uno::RuntimeException();
287 
288 	return m_xSeekable->getLength();
289 }
290 
291 //-----------------------------------------------
dispose()292 void SAL_CALL OFSInputStreamContainer::dispose(  )
293 		throw ( uno::RuntimeException )
294 {
295 	::osl::MutexGuard aGuard( m_aMutex );
296 
297 	if ( m_bDisposed )
298 		throw lang::DisposedException();
299 
300 	if ( !m_xInputStream.is() )
301 		throw uno::RuntimeException();
302 
303 	m_xInputStream->closeInput();
304 
305 	if ( m_pListenersContainer )
306 	{
307     	lang::EventObject aSource( static_cast< ::cppu::OWeakObject*>( this ) );
308 		m_pListenersContainer->disposeAndClear( aSource );
309 	}
310 
311 	m_bDisposed = sal_True;
312 }
313 
314 //-----------------------------------------------
addEventListener(const uno::Reference<lang::XEventListener> & xListener)315 void SAL_CALL OFSInputStreamContainer::addEventListener( const uno::Reference< lang::XEventListener >& xListener )
316 		throw ( uno::RuntimeException )
317 {
318 	::osl::MutexGuard aGuard( m_aMutex );
319 
320 	if ( m_bDisposed )
321 		throw lang::DisposedException();
322 
323 	if ( !m_pListenersContainer )
324 		m_pListenersContainer = new ::cppu::OInterfaceContainerHelper( m_aMutex );
325 
326 	m_pListenersContainer->addInterface( xListener );
327 }
328 
329 //-----------------------------------------------
removeEventListener(const uno::Reference<lang::XEventListener> & xListener)330 void SAL_CALL OFSInputStreamContainer::removeEventListener( const uno::Reference< lang::XEventListener >& xListener )
331 		throw ( uno::RuntimeException )
332 {
333 	::osl::MutexGuard aGuard( m_aMutex );
334 
335 	if ( m_bDisposed )
336 		throw lang::DisposedException();
337 
338 	if ( m_pListenersContainer )
339 		m_pListenersContainer->removeInterface( xListener );
340 }
341 
342 
343 
344