Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 :
21 : #include "ostreamcontainer.hxx"
22 :
23 :
24 : using namespace ::com::sun::star;
25 :
26 0 : OFSStreamContainer::OFSStreamContainer( const uno::Reference < io::XStream >& xStream )
27 : : m_bDisposed( false )
28 : , m_bInputClosed( false )
29 : , m_bOutputClosed( false )
30 : , m_pListenersContainer( NULL )
31 0 : , m_pTypeCollection( NULL )
32 : {
33 : try
34 : {
35 0 : m_xStream = xStream;
36 0 : if ( !m_xStream.is() )
37 0 : throw uno::RuntimeException();
38 :
39 0 : m_xSeekable = uno::Reference< io::XSeekable >( xStream, uno::UNO_QUERY );
40 0 : m_xInputStream = xStream->getInputStream();
41 0 : m_xOutputStream = xStream->getOutputStream();
42 0 : m_xTruncate = uno::Reference< io::XTruncate >( m_xOutputStream, uno::UNO_QUERY );
43 0 : m_xAsyncOutputMonitor = uno::Reference< io::XAsyncOutputMonitor >( m_xOutputStream, uno::UNO_QUERY );
44 : }
45 0 : catch( uno::Exception& )
46 : {
47 0 : m_xStream = uno::Reference< io::XStream >();
48 0 : m_xSeekable = uno::Reference< io::XSeekable >();
49 0 : m_xInputStream = uno::Reference< io::XInputStream >();
50 0 : m_xOutputStream = uno::Reference< io::XOutputStream >();
51 0 : m_xTruncate = uno::Reference< io::XTruncate >();
52 0 : m_xAsyncOutputMonitor = uno::Reference< io::XAsyncOutputMonitor >();
53 : }
54 0 : }
55 :
56 0 : OFSStreamContainer::~OFSStreamContainer()
57 : {
58 0 : if ( m_pListenersContainer )
59 : {
60 0 : delete m_pListenersContainer;
61 0 : m_pListenersContainer = NULL;
62 : }
63 0 : }
64 :
65 : // XInterface
66 0 : uno::Any SAL_CALL OFSStreamContainer::queryInterface( const uno::Type& rType )
67 : throw( uno::RuntimeException, std::exception )
68 : {
69 0 : uno::Any aReturn;
70 :
71 0 : aReturn <<= ::cppu::queryInterface
72 : ( rType
73 : , static_cast<lang::XTypeProvider*> ( this )
74 : , static_cast<io::XStream*> ( this )
75 : , static_cast<embed::XExtendedStorageStream*> ( this )
76 0 : , static_cast<lang::XComponent*> ( this ) );
77 :
78 0 : if ( aReturn.hasValue() )
79 0 : return aReturn ;
80 :
81 0 : if ( m_xSeekable.is() )
82 : {
83 0 : aReturn <<= ::cppu::queryInterface
84 : ( rType
85 0 : , static_cast<io::XSeekable*> ( this ) );
86 :
87 0 : if ( aReturn.hasValue() )
88 0 : return aReturn ;
89 : }
90 :
91 0 : if ( m_xInputStream.is() )
92 : {
93 0 : aReturn <<= ::cppu::queryInterface
94 : ( rType
95 0 : , static_cast<io::XInputStream*> ( this ) );
96 :
97 0 : if ( aReturn.hasValue() )
98 0 : return aReturn ;
99 : }
100 0 : if ( m_xOutputStream.is() )
101 : {
102 0 : aReturn <<= ::cppu::queryInterface
103 : ( rType
104 0 : , static_cast<io::XOutputStream*> ( this ) );
105 :
106 0 : if ( aReturn.hasValue() )
107 0 : return aReturn ;
108 : }
109 0 : if ( m_xTruncate.is() )
110 : {
111 0 : aReturn <<= ::cppu::queryInterface
112 : ( rType
113 0 : , static_cast<io::XTruncate*> ( this ) );
114 :
115 0 : if ( aReturn.hasValue() )
116 0 : return aReturn ;
117 : }
118 0 : if ( m_xAsyncOutputMonitor.is() )
119 : {
120 0 : aReturn <<= ::cppu::queryInterface
121 : ( rType
122 0 : , static_cast<io::XAsyncOutputMonitor*> ( this ) );
123 :
124 0 : if ( aReturn.hasValue() )
125 0 : return aReturn ;
126 : }
127 :
128 0 : return OWeakObject::queryInterface( rType );
129 : }
130 :
131 0 : void SAL_CALL OFSStreamContainer::acquire()
132 : throw()
133 : {
134 0 : OWeakObject::acquire();
135 0 : }
136 :
137 0 : void SAL_CALL OFSStreamContainer::release()
138 : throw()
139 : {
140 0 : OWeakObject::release();
141 0 : }
142 :
143 : // XTypeProvider
144 0 : uno::Sequence< uno::Type > SAL_CALL OFSStreamContainer::getTypes()
145 : throw( uno::RuntimeException, std::exception )
146 : {
147 0 : if ( m_pTypeCollection == NULL )
148 : {
149 0 : ::osl::MutexGuard aGuard( m_aMutex );
150 :
151 0 : if ( m_pTypeCollection == NULL )
152 : {
153 : ::cppu::OTypeCollection aTypeCollection
154 0 : ( ::getCppuType( ( const uno::Reference< lang::XTypeProvider >* )NULL )
155 0 : , ::getCppuType( ( const uno::Reference< embed::XExtendedStorageStream >* )NULL ) );
156 :
157 0 : if ( m_xSeekable.is() )
158 0 : aTypeCollection = ::cppu::OTypeCollection
159 0 : ( ::getCppuType( ( const uno::Reference< io::XSeekable >* )NULL ),
160 0 : aTypeCollection.getTypes() );
161 0 : if ( m_xInputStream.is() )
162 0 : aTypeCollection = ::cppu::OTypeCollection
163 0 : ( ::getCppuType( ( const uno::Reference< io::XInputStream >* )NULL ),
164 0 : aTypeCollection.getTypes() );
165 :
166 0 : if ( m_xOutputStream.is() )
167 0 : aTypeCollection = ::cppu::OTypeCollection
168 0 : ( ::getCppuType( ( const uno::Reference< io::XOutputStream >* )NULL ),
169 0 : aTypeCollection.getTypes() );
170 0 : if ( m_xTruncate.is() )
171 0 : aTypeCollection = ::cppu::OTypeCollection
172 0 : ( ::getCppuType( ( const uno::Reference< io::XTruncate >* )NULL ),
173 0 : aTypeCollection.getTypes() );
174 0 : if ( m_xAsyncOutputMonitor.is() )
175 0 : aTypeCollection = ::cppu::OTypeCollection
176 0 : ( ::getCppuType( ( const uno::Reference< io::XAsyncOutputMonitor >* )NULL ),
177 0 : aTypeCollection.getTypes() );
178 :
179 0 : m_pTypeCollection = new ::cppu::OTypeCollection( aTypeCollection );
180 0 : }
181 : }
182 0 : return m_pTypeCollection->getTypes() ;
183 : }
184 :
185 0 : uno::Sequence< sal_Int8 > SAL_CALL OFSStreamContainer::getImplementationId()
186 : throw( uno::RuntimeException, std::exception )
187 : {
188 0 : return css::uno::Sequence<sal_Int8>();
189 : }
190 :
191 : // XStream
192 0 : uno::Reference< io::XInputStream > SAL_CALL OFSStreamContainer::getInputStream()
193 : throw ( uno::RuntimeException, std::exception )
194 : {
195 0 : ::osl::MutexGuard aGuard( m_aMutex );
196 :
197 0 : if ( m_bDisposed )
198 0 : throw lang::DisposedException();
199 :
200 0 : if ( !m_xStream.is() )
201 0 : throw uno::RuntimeException();
202 :
203 0 : if ( m_xInputStream.is() )
204 0 : return uno::Reference< io::XInputStream >( static_cast< io::XInputStream* >( this ) );
205 :
206 0 : return uno::Reference< io::XInputStream >();
207 : }
208 :
209 0 : uno::Reference< io::XOutputStream > SAL_CALL OFSStreamContainer::getOutputStream()
210 : throw ( uno::RuntimeException, std::exception )
211 : {
212 0 : ::osl::MutexGuard aGuard( m_aMutex );
213 :
214 0 : if ( m_bDisposed )
215 0 : throw lang::DisposedException();
216 :
217 0 : if ( !m_xStream.is() )
218 0 : throw uno::RuntimeException();
219 :
220 0 : if ( m_xOutputStream.is() )
221 0 : return uno::Reference< io::XOutputStream >( static_cast< io::XOutputStream* >( this ) );
222 :
223 0 : return uno::Reference< io::XOutputStream >();
224 : }
225 :
226 : // XComponent
227 0 : void SAL_CALL OFSStreamContainer::dispose()
228 : throw ( uno::RuntimeException, std::exception )
229 : {
230 0 : ::osl::MutexGuard aGuard( m_aMutex );
231 :
232 0 : if ( m_bDisposed )
233 0 : throw lang::DisposedException();
234 :
235 0 : if ( !m_xStream.is() )
236 0 : throw uno::RuntimeException();
237 :
238 0 : if ( m_xInputStream.is() && !m_bInputClosed )
239 : {
240 0 : m_xInputStream->closeInput();
241 0 : m_bInputClosed = true;
242 : }
243 :
244 0 : if ( m_xOutputStream.is() && !m_bOutputClosed )
245 : {
246 0 : m_xOutputStream->closeOutput();
247 0 : m_bOutputClosed = true;
248 : }
249 :
250 0 : if ( m_pListenersContainer )
251 : {
252 0 : lang::EventObject aSource( static_cast< ::cppu::OWeakObject*>( this ) );
253 0 : m_pListenersContainer->disposeAndClear( aSource );
254 : }
255 :
256 0 : m_bDisposed = true;
257 0 : }
258 :
259 0 : void SAL_CALL OFSStreamContainer::addEventListener( const uno::Reference< lang::XEventListener >& xListener )
260 : throw ( uno::RuntimeException, std::exception )
261 : {
262 0 : ::osl::MutexGuard aGuard( m_aMutex );
263 :
264 0 : if ( m_bDisposed )
265 0 : throw lang::DisposedException();
266 :
267 0 : if ( !m_pListenersContainer )
268 0 : m_pListenersContainer = new ::cppu::OInterfaceContainerHelper( m_aMutex );
269 :
270 0 : m_pListenersContainer->addInterface( xListener );
271 0 : }
272 :
273 0 : void SAL_CALL OFSStreamContainer::removeEventListener( const uno::Reference< lang::XEventListener >& xListener )
274 : throw ( uno::RuntimeException, std::exception )
275 : {
276 0 : ::osl::MutexGuard aGuard( m_aMutex );
277 :
278 0 : if ( m_bDisposed )
279 0 : throw lang::DisposedException();
280 :
281 0 : if ( m_pListenersContainer )
282 0 : m_pListenersContainer->removeInterface( xListener );
283 0 : }
284 :
285 :
286 : // XSeekable
287 0 : void SAL_CALL OFSStreamContainer::seek( sal_Int64 location )
288 : throw ( lang::IllegalArgumentException,
289 : io::IOException,
290 : uno::RuntimeException, std::exception )
291 : {
292 0 : ::osl::MutexGuard aGuard( m_aMutex );
293 :
294 0 : if ( m_bDisposed )
295 0 : throw lang::DisposedException();
296 :
297 0 : if ( !m_xStream.is() || !m_xSeekable.is() )
298 0 : throw uno::RuntimeException();
299 :
300 0 : m_xSeekable->seek( location );
301 0 : }
302 :
303 0 : sal_Int64 SAL_CALL OFSStreamContainer::getPosition()
304 : throw ( io::IOException,
305 : uno::RuntimeException, std::exception )
306 : {
307 0 : ::osl::MutexGuard aGuard( m_aMutex );
308 :
309 0 : if ( m_bDisposed )
310 0 : throw lang::DisposedException();
311 :
312 0 : if ( !m_xStream.is() || !m_xSeekable.is() )
313 0 : throw uno::RuntimeException();
314 :
315 0 : return m_xSeekable->getPosition();
316 : }
317 :
318 0 : sal_Int64 SAL_CALL OFSStreamContainer::getLength()
319 : throw ( io::IOException,
320 : uno::RuntimeException, std::exception )
321 : {
322 0 : ::osl::MutexGuard aGuard( m_aMutex );
323 :
324 0 : if ( m_bDisposed )
325 0 : throw lang::DisposedException();
326 :
327 0 : if ( !m_xStream.is() || !m_xSeekable.is() )
328 0 : throw uno::RuntimeException();
329 :
330 0 : return m_xSeekable->getLength();
331 : }
332 :
333 :
334 : // XInputStream
335 0 : sal_Int32 SAL_CALL OFSStreamContainer::readBytes( uno::Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead )
336 : throw( io::NotConnectedException,
337 : io::BufferSizeExceededException,
338 : io::IOException,
339 : uno::RuntimeException, std::exception )
340 : {
341 0 : ::osl::MutexGuard aGuard( m_aMutex );
342 :
343 0 : if ( m_bDisposed )
344 0 : throw lang::DisposedException();
345 :
346 0 : if ( !m_xStream.is() || !m_xInputStream.is() )
347 0 : throw uno::RuntimeException();
348 :
349 0 : return m_xInputStream->readBytes( aData, nBytesToRead );
350 : }
351 :
352 0 : sal_Int32 SAL_CALL OFSStreamContainer::readSomeBytes( uno::Sequence< sal_Int8 >& aData, sal_Int32 nMaxBytesToRead )
353 : throw( io::NotConnectedException,
354 : io::BufferSizeExceededException,
355 : io::IOException,
356 : uno::RuntimeException, std::exception )
357 : {
358 0 : ::osl::MutexGuard aGuard( m_aMutex );
359 :
360 0 : if ( m_bDisposed )
361 0 : throw lang::DisposedException();
362 :
363 0 : if ( !m_xStream.is() || !m_xInputStream.is() )
364 0 : throw uno::RuntimeException();
365 :
366 0 : return m_xInputStream->readSomeBytes( aData, nMaxBytesToRead );
367 : }
368 :
369 0 : void SAL_CALL OFSStreamContainer::skipBytes( sal_Int32 nBytesToSkip )
370 : throw( io::NotConnectedException,
371 : io::BufferSizeExceededException,
372 : io::IOException,
373 : uno::RuntimeException, std::exception )
374 : {
375 0 : ::osl::MutexGuard aGuard( m_aMutex );
376 :
377 0 : if ( m_bDisposed )
378 0 : throw lang::DisposedException();
379 :
380 0 : if ( !m_xStream.is() || !m_xInputStream.is() )
381 0 : throw uno::RuntimeException();
382 :
383 0 : m_xInputStream->skipBytes( nBytesToSkip );
384 0 : }
385 :
386 0 : sal_Int32 SAL_CALL OFSStreamContainer::available()
387 : throw( io::NotConnectedException,
388 : io::IOException,
389 : uno::RuntimeException, std::exception )
390 : {
391 0 : ::osl::MutexGuard aGuard( m_aMutex );
392 :
393 0 : if ( m_bDisposed )
394 0 : throw lang::DisposedException();
395 :
396 0 : if ( !m_xStream.is() || !m_xInputStream.is() )
397 0 : throw uno::RuntimeException();
398 :
399 0 : return m_xInputStream->available();
400 : }
401 :
402 0 : void SAL_CALL OFSStreamContainer::closeInput()
403 : throw( io::NotConnectedException,
404 : io::IOException,
405 : uno::RuntimeException, std::exception )
406 : {
407 0 : ::osl::MutexGuard aGuard( m_aMutex );
408 :
409 0 : if ( m_bDisposed )
410 0 : throw lang::DisposedException();
411 :
412 0 : if ( !m_xStream.is() || !m_xInputStream.is() )
413 0 : throw uno::RuntimeException();
414 :
415 0 : if ( m_xInputStream.is() )
416 : {
417 0 : m_xInputStream->closeInput();
418 0 : m_bInputClosed = true;
419 : }
420 :
421 0 : if ( m_bOutputClosed )
422 0 : dispose();
423 0 : }
424 :
425 : // XOutputStream
426 0 : void SAL_CALL OFSStreamContainer::writeBytes( const uno::Sequence< sal_Int8 >& aData )
427 : throw ( io::NotConnectedException,
428 : io::BufferSizeExceededException,
429 : io::IOException,
430 : uno::RuntimeException, std::exception )
431 : {
432 0 : ::osl::MutexGuard aGuard( m_aMutex );
433 :
434 0 : if ( m_bDisposed )
435 0 : throw lang::DisposedException();
436 :
437 0 : if ( !m_xStream.is() || !m_xOutputStream.is() )
438 0 : throw uno::RuntimeException();
439 :
440 0 : return m_xOutputStream->writeBytes( aData );
441 : }
442 :
443 0 : void SAL_CALL OFSStreamContainer::flush()
444 : throw ( io::NotConnectedException,
445 : io::BufferSizeExceededException,
446 : io::IOException,
447 : uno::RuntimeException, std::exception )
448 : {
449 0 : ::osl::MutexGuard aGuard( m_aMutex );
450 :
451 0 : if ( m_bDisposed )
452 0 : throw lang::DisposedException();
453 :
454 0 : if ( !m_xStream.is() || !m_xOutputStream.is() )
455 0 : throw uno::RuntimeException();
456 :
457 0 : return m_xOutputStream->flush();
458 : }
459 :
460 0 : void SAL_CALL OFSStreamContainer::closeOutput()
461 : throw ( io::NotConnectedException,
462 : io::BufferSizeExceededException,
463 : io::IOException,
464 : uno::RuntimeException, std::exception )
465 : {
466 0 : ::osl::MutexGuard aGuard( m_aMutex );
467 :
468 0 : if ( m_bDisposed )
469 0 : throw lang::DisposedException();
470 :
471 0 : if ( !m_xStream.is() || !m_xOutputStream.is() )
472 0 : throw uno::RuntimeException();
473 :
474 0 : if ( m_xOutputStream.is() )
475 : {
476 0 : m_xOutputStream->closeOutput();
477 0 : m_bOutputClosed = true;
478 : }
479 :
480 0 : if ( m_bInputClosed )
481 0 : dispose();
482 0 : }
483 :
484 :
485 : // XTruncate
486 0 : void SAL_CALL OFSStreamContainer::truncate()
487 : throw ( io::IOException,
488 : uno::RuntimeException, std::exception )
489 : {
490 0 : ::osl::MutexGuard aGuard( m_aMutex );
491 :
492 0 : if ( m_bDisposed )
493 0 : throw lang::DisposedException();
494 :
495 0 : if ( !m_xStream.is() || !m_xTruncate.is() )
496 0 : throw uno::RuntimeException();
497 :
498 0 : m_xTruncate->truncate();
499 0 : }
500 :
501 :
502 : // XAsyncOutputMonitor
503 0 : void SAL_CALL OFSStreamContainer::waitForCompletion()
504 : throw ( io::IOException,
505 : uno::RuntimeException, std::exception )
506 : {
507 0 : ::osl::MutexGuard aGuard( m_aMutex );
508 :
509 0 : if ( m_bDisposed )
510 0 : throw lang::DisposedException();
511 :
512 0 : if ( !m_xStream.is() || !m_xAsyncOutputMonitor.is() )
513 0 : throw uno::RuntimeException();
514 :
515 0 : m_xAsyncOutputMonitor->waitForCompletion();
516 0 : }
517 :
518 :
519 :
520 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|