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 : //-----------------------------------------------
27 0 : OFSStreamContainer::OFSStreamContainer( const uno::Reference < io::XStream >& xStream )
28 : : m_bDisposed( false )
29 : , m_bInputClosed( false )
30 : , m_bOutputClosed( false )
31 : , m_pListenersContainer( NULL )
32 0 : , m_pTypeCollection( NULL )
33 : {
34 : try
35 : {
36 0 : m_xStream = xStream;
37 0 : if ( !m_xStream.is() )
38 0 : throw uno::RuntimeException();
39 :
40 0 : m_xSeekable = uno::Reference< io::XSeekable >( xStream, uno::UNO_QUERY );
41 0 : m_xInputStream = xStream->getInputStream();
42 0 : m_xOutputStream = xStream->getOutputStream();
43 0 : m_xTruncate = uno::Reference< io::XTruncate >( m_xOutputStream, uno::UNO_QUERY );
44 0 : m_xAsyncOutputMonitor = uno::Reference< io::XAsyncOutputMonitor >( m_xOutputStream, uno::UNO_QUERY );
45 : }
46 0 : catch( uno::Exception& )
47 : {
48 0 : m_xStream = uno::Reference< io::XStream >();
49 0 : m_xSeekable = uno::Reference< io::XSeekable >();
50 0 : m_xInputStream = uno::Reference< io::XInputStream >();
51 0 : m_xOutputStream = uno::Reference< io::XOutputStream >();
52 0 : m_xTruncate = uno::Reference< io::XTruncate >();
53 0 : m_xAsyncOutputMonitor = uno::Reference< io::XAsyncOutputMonitor >();
54 : }
55 0 : }
56 :
57 : //-----------------------------------------------
58 0 : OFSStreamContainer::~OFSStreamContainer()
59 : {
60 0 : if ( m_pListenersContainer )
61 : {
62 0 : delete m_pListenersContainer;
63 0 : m_pListenersContainer = NULL;
64 : }
65 0 : }
66 :
67 : // XInterface
68 : //-----------------------------------------------
69 0 : uno::Any SAL_CALL OFSStreamContainer::queryInterface( const uno::Type& rType )
70 : throw( uno::RuntimeException )
71 : {
72 0 : uno::Any aReturn;
73 :
74 : aReturn <<= ::cppu::queryInterface
75 : ( rType
76 : , static_cast<lang::XTypeProvider*> ( this )
77 : , static_cast<io::XStream*> ( this )
78 : , static_cast<embed::XExtendedStorageStream*> ( this )
79 0 : , static_cast<lang::XComponent*> ( this ) );
80 :
81 0 : if ( aReturn.hasValue() == sal_True )
82 0 : return aReturn ;
83 :
84 0 : if ( m_xSeekable.is() )
85 : {
86 : aReturn <<= ::cppu::queryInterface
87 : ( rType
88 0 : , static_cast<io::XSeekable*> ( this ) );
89 :
90 0 : if ( aReturn.hasValue() == sal_True )
91 0 : return aReturn ;
92 : }
93 :
94 0 : if ( m_xInputStream.is() )
95 : {
96 : aReturn <<= ::cppu::queryInterface
97 : ( rType
98 0 : , static_cast<io::XInputStream*> ( this ) );
99 :
100 0 : if ( aReturn.hasValue() == sal_True )
101 0 : return aReturn ;
102 : }
103 0 : if ( m_xOutputStream.is() )
104 : {
105 : aReturn <<= ::cppu::queryInterface
106 : ( rType
107 0 : , static_cast<io::XOutputStream*> ( this ) );
108 :
109 0 : if ( aReturn.hasValue() == sal_True )
110 0 : return aReturn ;
111 : }
112 0 : if ( m_xTruncate.is() )
113 : {
114 : aReturn <<= ::cppu::queryInterface
115 : ( rType
116 0 : , static_cast<io::XTruncate*> ( this ) );
117 :
118 0 : if ( aReturn.hasValue() == sal_True )
119 0 : return aReturn ;
120 : }
121 0 : if ( m_xAsyncOutputMonitor.is() )
122 : {
123 : aReturn <<= ::cppu::queryInterface
124 : ( rType
125 0 : , static_cast<io::XAsyncOutputMonitor*> ( this ) );
126 :
127 0 : if ( aReturn.hasValue() == sal_True )
128 0 : return aReturn ;
129 : }
130 :
131 0 : return OWeakObject::queryInterface( rType );
132 : }
133 :
134 : //-----------------------------------------------
135 0 : void SAL_CALL OFSStreamContainer::acquire()
136 : throw()
137 : {
138 0 : OWeakObject::acquire();
139 0 : }
140 :
141 : //-----------------------------------------------
142 0 : void SAL_CALL OFSStreamContainer::release()
143 : throw()
144 : {
145 0 : OWeakObject::release();
146 0 : }
147 :
148 : // XTypeProvider
149 : //-----------------------------------------------
150 0 : uno::Sequence< uno::Type > SAL_CALL OFSStreamContainer::getTypes()
151 : throw( uno::RuntimeException )
152 : {
153 0 : if ( m_pTypeCollection == NULL )
154 : {
155 0 : ::osl::MutexGuard aGuard( m_aMutex );
156 :
157 0 : if ( m_pTypeCollection == NULL )
158 : {
159 : ::cppu::OTypeCollection aTypeCollection
160 0 : ( ::getCppuType( ( const uno::Reference< lang::XTypeProvider >* )NULL )
161 0 : , ::getCppuType( ( const uno::Reference< embed::XExtendedStorageStream >* )NULL ) );
162 :
163 0 : if ( m_xSeekable.is() )
164 : aTypeCollection = ::cppu::OTypeCollection
165 0 : ( ::getCppuType( ( const uno::Reference< io::XSeekable >* )NULL ),
166 0 : aTypeCollection.getTypes() );
167 0 : if ( m_xInputStream.is() )
168 : aTypeCollection = ::cppu::OTypeCollection
169 0 : ( ::getCppuType( ( const uno::Reference< io::XInputStream >* )NULL ),
170 0 : aTypeCollection.getTypes() );
171 :
172 0 : if ( m_xOutputStream.is() )
173 : aTypeCollection = ::cppu::OTypeCollection
174 0 : ( ::getCppuType( ( const uno::Reference< io::XOutputStream >* )NULL ),
175 0 : aTypeCollection.getTypes() );
176 0 : if ( m_xTruncate.is() )
177 : aTypeCollection = ::cppu::OTypeCollection
178 0 : ( ::getCppuType( ( const uno::Reference< io::XTruncate >* )NULL ),
179 0 : aTypeCollection.getTypes() );
180 0 : if ( m_xAsyncOutputMonitor.is() )
181 : aTypeCollection = ::cppu::OTypeCollection
182 0 : ( ::getCppuType( ( const uno::Reference< io::XAsyncOutputMonitor >* )NULL ),
183 0 : aTypeCollection.getTypes() );
184 :
185 0 : m_pTypeCollection = new ::cppu::OTypeCollection( aTypeCollection );
186 0 : }
187 : }
188 0 : return m_pTypeCollection->getTypes() ;
189 : }
190 :
191 : //-----------------------------------------------
192 0 : uno::Sequence< sal_Int8 > SAL_CALL OFSStreamContainer::getImplementationId()
193 : throw( uno::RuntimeException )
194 : {
195 : static ::cppu::OImplementationId* pID = NULL ;
196 :
197 0 : if ( pID == NULL )
198 : {
199 0 : ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() ) ;
200 :
201 0 : if ( pID == NULL )
202 : {
203 0 : static ::cppu::OImplementationId aID( sal_False ) ;
204 0 : pID = &aID ;
205 0 : }
206 : }
207 :
208 0 : return pID->getImplementationId() ;
209 : }
210 :
211 : // XStream
212 : //-----------------------------------------------
213 0 : uno::Reference< io::XInputStream > SAL_CALL OFSStreamContainer::getInputStream()
214 : throw ( uno::RuntimeException )
215 : {
216 0 : ::osl::MutexGuard aGuard( m_aMutex );
217 :
218 0 : if ( m_bDisposed )
219 0 : throw lang::DisposedException();
220 :
221 0 : if ( !m_xStream.is() )
222 0 : throw uno::RuntimeException();
223 :
224 0 : if ( m_xInputStream.is() )
225 0 : return uno::Reference< io::XInputStream >( static_cast< io::XInputStream* >( this ) );
226 :
227 0 : return uno::Reference< io::XInputStream >();
228 : }
229 :
230 : //-----------------------------------------------
231 0 : uno::Reference< io::XOutputStream > SAL_CALL OFSStreamContainer::getOutputStream()
232 : throw ( uno::RuntimeException )
233 : {
234 0 : ::osl::MutexGuard aGuard( m_aMutex );
235 :
236 0 : if ( m_bDisposed )
237 0 : throw lang::DisposedException();
238 :
239 0 : if ( !m_xStream.is() )
240 0 : throw uno::RuntimeException();
241 :
242 0 : if ( m_xOutputStream.is() )
243 0 : return uno::Reference< io::XOutputStream >( static_cast< io::XOutputStream* >( this ) );
244 :
245 0 : return uno::Reference< io::XOutputStream >();
246 : }
247 :
248 : // XComponent
249 : //-----------------------------------------------
250 0 : void SAL_CALL OFSStreamContainer::dispose()
251 : throw ( uno::RuntimeException )
252 : {
253 0 : ::osl::MutexGuard aGuard( m_aMutex );
254 :
255 0 : if ( m_bDisposed )
256 0 : throw lang::DisposedException();
257 :
258 0 : if ( !m_xStream.is() )
259 0 : throw uno::RuntimeException();
260 :
261 0 : if ( m_xInputStream.is() && !m_bInputClosed )
262 : {
263 0 : m_xInputStream->closeInput();
264 0 : m_bInputClosed = true;
265 : }
266 :
267 0 : if ( m_xOutputStream.is() && !m_bOutputClosed )
268 : {
269 0 : m_xOutputStream->closeOutput();
270 0 : m_bOutputClosed = true;
271 : }
272 :
273 0 : if ( m_pListenersContainer )
274 : {
275 0 : lang::EventObject aSource( static_cast< ::cppu::OWeakObject*>( this ) );
276 0 : m_pListenersContainer->disposeAndClear( aSource );
277 : }
278 :
279 0 : m_bDisposed = true;
280 0 : }
281 :
282 : //-----------------------------------------------
283 0 : void SAL_CALL OFSStreamContainer::addEventListener( const uno::Reference< lang::XEventListener >& xListener )
284 : throw ( uno::RuntimeException )
285 : {
286 0 : ::osl::MutexGuard aGuard( m_aMutex );
287 :
288 0 : if ( m_bDisposed )
289 0 : throw lang::DisposedException();
290 :
291 0 : if ( !m_pListenersContainer )
292 0 : m_pListenersContainer = new ::cppu::OInterfaceContainerHelper( m_aMutex );
293 :
294 0 : m_pListenersContainer->addInterface( xListener );
295 0 : }
296 :
297 : //-----------------------------------------------
298 0 : void SAL_CALL OFSStreamContainer::removeEventListener( const uno::Reference< lang::XEventListener >& xListener )
299 : throw ( uno::RuntimeException )
300 : {
301 0 : ::osl::MutexGuard aGuard( m_aMutex );
302 :
303 0 : if ( m_bDisposed )
304 0 : throw lang::DisposedException();
305 :
306 0 : if ( m_pListenersContainer )
307 0 : m_pListenersContainer->removeInterface( xListener );
308 0 : }
309 :
310 :
311 : // XSeekable
312 : //-----------------------------------------------
313 0 : void SAL_CALL OFSStreamContainer::seek( sal_Int64 location )
314 : throw ( lang::IllegalArgumentException,
315 : io::IOException,
316 : uno::RuntimeException )
317 : {
318 0 : ::osl::MutexGuard aGuard( m_aMutex );
319 :
320 0 : if ( m_bDisposed )
321 0 : throw lang::DisposedException();
322 :
323 0 : if ( !m_xStream.is() || !m_xSeekable.is() )
324 0 : throw uno::RuntimeException();
325 :
326 0 : m_xSeekable->seek( location );
327 0 : }
328 :
329 : //-----------------------------------------------
330 0 : sal_Int64 SAL_CALL OFSStreamContainer::getPosition()
331 : throw ( io::IOException,
332 : uno::RuntimeException )
333 : {
334 0 : ::osl::MutexGuard aGuard( m_aMutex );
335 :
336 0 : if ( m_bDisposed )
337 0 : throw lang::DisposedException();
338 :
339 0 : if ( !m_xStream.is() || !m_xSeekable.is() )
340 0 : throw uno::RuntimeException();
341 :
342 0 : return m_xSeekable->getPosition();
343 : }
344 :
345 : //-----------------------------------------------
346 0 : sal_Int64 SAL_CALL OFSStreamContainer::getLength()
347 : throw ( io::IOException,
348 : uno::RuntimeException )
349 : {
350 0 : ::osl::MutexGuard aGuard( m_aMutex );
351 :
352 0 : if ( m_bDisposed )
353 0 : throw lang::DisposedException();
354 :
355 0 : if ( !m_xStream.is() || !m_xSeekable.is() )
356 0 : throw uno::RuntimeException();
357 :
358 0 : return m_xSeekable->getLength();
359 : }
360 :
361 :
362 : // XInputStream
363 : //-----------------------------------------------
364 0 : sal_Int32 SAL_CALL OFSStreamContainer::readBytes( uno::Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead )
365 : throw( io::NotConnectedException,
366 : io::BufferSizeExceededException,
367 : io::IOException,
368 : uno::RuntimeException )
369 : {
370 0 : ::osl::MutexGuard aGuard( m_aMutex );
371 :
372 0 : if ( m_bDisposed )
373 0 : throw lang::DisposedException();
374 :
375 0 : if ( !m_xStream.is() || !m_xInputStream.is() )
376 0 : throw uno::RuntimeException();
377 :
378 0 : return m_xInputStream->readBytes( aData, nBytesToRead );
379 : }
380 :
381 : //-----------------------------------------------
382 0 : sal_Int32 SAL_CALL OFSStreamContainer::readSomeBytes( uno::Sequence< sal_Int8 >& aData, sal_Int32 nMaxBytesToRead )
383 : throw( io::NotConnectedException,
384 : io::BufferSizeExceededException,
385 : io::IOException,
386 : uno::RuntimeException )
387 : {
388 0 : ::osl::MutexGuard aGuard( m_aMutex );
389 :
390 0 : if ( m_bDisposed )
391 0 : throw lang::DisposedException();
392 :
393 0 : if ( !m_xStream.is() || !m_xInputStream.is() )
394 0 : throw uno::RuntimeException();
395 :
396 0 : return m_xInputStream->readSomeBytes( aData, nMaxBytesToRead );
397 : }
398 :
399 : //-----------------------------------------------
400 0 : void SAL_CALL OFSStreamContainer::skipBytes( sal_Int32 nBytesToSkip )
401 : throw( io::NotConnectedException,
402 : io::BufferSizeExceededException,
403 : io::IOException,
404 : uno::RuntimeException )
405 : {
406 0 : ::osl::MutexGuard aGuard( m_aMutex );
407 :
408 0 : if ( m_bDisposed )
409 0 : throw lang::DisposedException();
410 :
411 0 : if ( !m_xStream.is() || !m_xInputStream.is() )
412 0 : throw uno::RuntimeException();
413 :
414 0 : m_xInputStream->skipBytes( nBytesToSkip );
415 0 : }
416 :
417 : //-----------------------------------------------
418 0 : sal_Int32 SAL_CALL OFSStreamContainer::available()
419 : throw( io::NotConnectedException,
420 : io::IOException,
421 : uno::RuntimeException )
422 : {
423 0 : ::osl::MutexGuard aGuard( m_aMutex );
424 :
425 0 : if ( m_bDisposed )
426 0 : throw lang::DisposedException();
427 :
428 0 : if ( !m_xStream.is() || !m_xInputStream.is() )
429 0 : throw uno::RuntimeException();
430 :
431 0 : return m_xInputStream->available();
432 : }
433 :
434 : //-----------------------------------------------
435 0 : void SAL_CALL OFSStreamContainer::closeInput()
436 : throw( io::NotConnectedException,
437 : io::IOException,
438 : uno::RuntimeException )
439 : {
440 0 : ::osl::MutexGuard aGuard( m_aMutex );
441 :
442 0 : if ( m_bDisposed )
443 0 : throw lang::DisposedException();
444 :
445 0 : if ( !m_xStream.is() || !m_xInputStream.is() )
446 0 : throw uno::RuntimeException();
447 :
448 0 : if ( m_xInputStream.is() )
449 : {
450 0 : m_xInputStream->closeInput();
451 0 : m_bInputClosed = true;
452 : }
453 :
454 0 : if ( m_bOutputClosed )
455 0 : dispose();
456 0 : }
457 :
458 : // XOutputStream
459 : //-----------------------------------------------
460 0 : void SAL_CALL OFSStreamContainer::writeBytes( const uno::Sequence< sal_Int8 >& aData )
461 : throw ( io::NotConnectedException,
462 : io::BufferSizeExceededException,
463 : io::IOException,
464 : uno::RuntimeException )
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 : return m_xOutputStream->writeBytes( aData );
475 : }
476 :
477 : //-----------------------------------------------
478 0 : void SAL_CALL OFSStreamContainer::flush()
479 : throw ( io::NotConnectedException,
480 : io::BufferSizeExceededException,
481 : io::IOException,
482 : uno::RuntimeException )
483 : {
484 0 : ::osl::MutexGuard aGuard( m_aMutex );
485 :
486 0 : if ( m_bDisposed )
487 0 : throw lang::DisposedException();
488 :
489 0 : if ( !m_xStream.is() || !m_xOutputStream.is() )
490 0 : throw uno::RuntimeException();
491 :
492 0 : return m_xOutputStream->flush();
493 : }
494 :
495 : //-----------------------------------------------
496 0 : void SAL_CALL OFSStreamContainer::closeOutput()
497 : throw ( io::NotConnectedException,
498 : io::BufferSizeExceededException,
499 : io::IOException,
500 : uno::RuntimeException )
501 : {
502 0 : ::osl::MutexGuard aGuard( m_aMutex );
503 :
504 0 : if ( m_bDisposed )
505 0 : throw lang::DisposedException();
506 :
507 0 : if ( !m_xStream.is() || !m_xOutputStream.is() )
508 0 : throw uno::RuntimeException();
509 :
510 0 : if ( m_xOutputStream.is() )
511 : {
512 0 : m_xOutputStream->closeOutput();
513 0 : m_bOutputClosed = true;
514 : }
515 :
516 0 : if ( m_bInputClosed )
517 0 : dispose();
518 0 : }
519 :
520 :
521 : // XTruncate
522 : //-----------------------------------------------
523 0 : void SAL_CALL OFSStreamContainer::truncate()
524 : throw ( io::IOException,
525 : uno::RuntimeException )
526 : {
527 0 : ::osl::MutexGuard aGuard( m_aMutex );
528 :
529 0 : if ( m_bDisposed )
530 0 : throw lang::DisposedException();
531 :
532 0 : if ( !m_xStream.is() || !m_xTruncate.is() )
533 0 : throw uno::RuntimeException();
534 :
535 0 : m_xTruncate->truncate();
536 0 : }
537 :
538 :
539 : // XAsyncOutputMonitor
540 : //-----------------------------------------------
541 0 : void SAL_CALL OFSStreamContainer::waitForCompletion()
542 : throw ( io::IOException,
543 : uno::RuntimeException )
544 : {
545 0 : ::osl::MutexGuard aGuard( m_aMutex );
546 :
547 0 : if ( m_bDisposed )
548 0 : throw lang::DisposedException();
549 :
550 0 : if ( !m_xStream.is() || !m_xAsyncOutputMonitor.is() )
551 0 : throw uno::RuntimeException();
552 :
553 0 : m_xAsyncOutputMonitor->waitForCompletion();
554 0 : }
555 :
556 :
557 :
558 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|