1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: ostreamcontainer.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_svtools.hxx"
34 #include "ostreamcontainer.hxx"
37 using namespace ::com::sun::star
;
39 //-----------------------------------------------
40 OFSStreamContainer::OFSStreamContainer( const uno::Reference
< io::XStream
>& xStream
)
41 : m_bDisposed( sal_False
)
42 , m_bInputClosed( sal_False
)
43 , m_bOutputClosed( sal_False
)
44 , m_pListenersContainer( NULL
)
45 , m_pTypeCollection( NULL
)
50 if ( !m_xStream
.is() )
51 throw uno::RuntimeException();
53 m_xSeekable
= uno::Reference
< io::XSeekable
>( xStream
, uno::UNO_QUERY
);
54 m_xInputStream
= xStream
->getInputStream();
55 m_xOutputStream
= xStream
->getOutputStream();
56 m_xTruncate
= uno::Reference
< io::XTruncate
>( m_xOutputStream
, uno::UNO_QUERY
);
57 m_xAsyncOutputMonitor
= uno::Reference
< io::XAsyncOutputMonitor
>( m_xOutputStream
, uno::UNO_QUERY
);
59 catch( uno::Exception
& )
61 m_xStream
= uno::Reference
< io::XStream
>();
62 m_xSeekable
= uno::Reference
< io::XSeekable
>();
63 m_xInputStream
= uno::Reference
< io::XInputStream
>();
64 m_xOutputStream
= uno::Reference
< io::XOutputStream
>();
65 m_xTruncate
= uno::Reference
< io::XTruncate
>();
66 m_xAsyncOutputMonitor
= uno::Reference
< io::XAsyncOutputMonitor
>();
70 //-----------------------------------------------
71 OFSStreamContainer::~OFSStreamContainer()
73 if ( m_pListenersContainer
)
75 delete m_pListenersContainer
;
76 m_pListenersContainer
= NULL
;
81 //-----------------------------------------------
82 uno::Any SAL_CALL
OFSStreamContainer::queryInterface( const uno::Type
& rType
)
83 throw( uno::RuntimeException
)
87 aReturn
<<= ::cppu::queryInterface
89 , static_cast<lang::XTypeProvider
*> ( this )
90 , static_cast<io::XStream
*> ( this )
91 , static_cast<embed::XExtendedStorageStream
*> ( this )
92 , static_cast<lang::XComponent
*> ( this ) );
94 if ( aReturn
.hasValue() == sal_True
)
97 if ( m_xSeekable
.is() )
99 aReturn
<<= ::cppu::queryInterface
101 , static_cast<io::XSeekable
*> ( this ) );
103 if ( aReturn
.hasValue() == sal_True
)
107 if ( m_xInputStream
.is() )
109 aReturn
<<= ::cppu::queryInterface
111 , static_cast<io::XInputStream
*> ( this ) );
113 if ( aReturn
.hasValue() == sal_True
)
116 if ( m_xOutputStream
.is() )
118 aReturn
<<= ::cppu::queryInterface
120 , static_cast<io::XOutputStream
*> ( this ) );
122 if ( aReturn
.hasValue() == sal_True
)
125 if ( m_xTruncate
.is() )
127 aReturn
<<= ::cppu::queryInterface
129 , static_cast<io::XTruncate
*> ( this ) );
131 if ( aReturn
.hasValue() == sal_True
)
134 if ( m_xAsyncOutputMonitor
.is() )
136 aReturn
<<= ::cppu::queryInterface
138 , static_cast<io::XAsyncOutputMonitor
*> ( this ) );
140 if ( aReturn
.hasValue() == sal_True
)
144 return OWeakObject::queryInterface( rType
);
147 //-----------------------------------------------
148 void SAL_CALL
OFSStreamContainer::acquire()
151 OWeakObject::acquire();
154 //-----------------------------------------------
155 void SAL_CALL
OFSStreamContainer::release()
158 OWeakObject::release();
162 //-----------------------------------------------
163 uno::Sequence
< uno::Type
> SAL_CALL
OFSStreamContainer::getTypes()
164 throw( uno::RuntimeException
)
166 if ( m_pTypeCollection
== NULL
)
168 ::osl::MutexGuard
aGuard( m_aMutex
);
170 if ( m_pTypeCollection
== NULL
)
172 ::cppu::OTypeCollection aTypeCollection
173 ( ::getCppuType( ( const uno::Reference
< lang::XTypeProvider
>* )NULL
)
174 , ::getCppuType( ( const uno::Reference
< embed::XExtendedStorageStream
>* )NULL
) );
176 if ( m_xSeekable
.is() )
177 aTypeCollection
= ::cppu::OTypeCollection
178 ( ::getCppuType( ( const uno::Reference
< io::XSeekable
>* )NULL
),
179 aTypeCollection
.getTypes() );
180 if ( m_xInputStream
.is() )
181 aTypeCollection
= ::cppu::OTypeCollection
182 ( ::getCppuType( ( const uno::Reference
< io::XInputStream
>* )NULL
),
183 aTypeCollection
.getTypes() );
185 if ( m_xOutputStream
.is() )
186 aTypeCollection
= ::cppu::OTypeCollection
187 ( ::getCppuType( ( const uno::Reference
< io::XOutputStream
>* )NULL
),
188 aTypeCollection
.getTypes() );
189 if ( m_xTruncate
.is() )
190 aTypeCollection
= ::cppu::OTypeCollection
191 ( ::getCppuType( ( const uno::Reference
< io::XTruncate
>* )NULL
),
192 aTypeCollection
.getTypes() );
193 if ( m_xAsyncOutputMonitor
.is() )
194 aTypeCollection
= ::cppu::OTypeCollection
195 ( ::getCppuType( ( const uno::Reference
< io::XAsyncOutputMonitor
>* )NULL
),
196 aTypeCollection
.getTypes() );
198 m_pTypeCollection
= new ::cppu::OTypeCollection( aTypeCollection
);
201 return m_pTypeCollection
->getTypes() ;
204 //-----------------------------------------------
205 uno::Sequence
< sal_Int8
> SAL_CALL
OFSStreamContainer::getImplementationId()
206 throw( uno::RuntimeException
)
208 static ::cppu::OImplementationId
* pID
= NULL
;
212 ::osl::MutexGuard
aGuard( ::osl::Mutex::getGlobalMutex() ) ;
216 static ::cppu::OImplementationId
aID( sal_False
) ;
221 return pID
->getImplementationId() ;
225 //-----------------------------------------------
226 uno::Reference
< io::XInputStream
> SAL_CALL
OFSStreamContainer::getInputStream()
227 throw ( uno::RuntimeException
)
229 ::osl::MutexGuard
aGuard( m_aMutex
);
232 throw lang::DisposedException();
234 if ( !m_xStream
.is() )
235 throw uno::RuntimeException();
237 if ( m_xInputStream
.is() )
238 return uno::Reference
< io::XInputStream
>( static_cast< io::XInputStream
* >( this ) );
240 return uno::Reference
< io::XInputStream
>();
243 //-----------------------------------------------
244 uno::Reference
< io::XOutputStream
> SAL_CALL
OFSStreamContainer::getOutputStream()
245 throw ( uno::RuntimeException
)
247 ::osl::MutexGuard
aGuard( m_aMutex
);
250 throw lang::DisposedException();
252 if ( !m_xStream
.is() )
253 throw uno::RuntimeException();
255 if ( m_xOutputStream
.is() )
256 return uno::Reference
< io::XOutputStream
>( static_cast< io::XOutputStream
* >( this ) );
258 return uno::Reference
< io::XOutputStream
>();
262 //-----------------------------------------------
263 void SAL_CALL
OFSStreamContainer::dispose()
264 throw ( uno::RuntimeException
)
266 ::osl::MutexGuard
aGuard( m_aMutex
);
269 throw lang::DisposedException();
271 if ( !m_xStream
.is() )
272 throw uno::RuntimeException();
274 if ( m_xInputStream
.is() && !m_bInputClosed
)
276 m_xInputStream
->closeInput();
277 m_bInputClosed
= sal_True
;
280 if ( m_xOutputStream
.is() && !m_bOutputClosed
)
282 m_xOutputStream
->closeOutput();
283 m_bOutputClosed
= sal_True
;
286 if ( m_pListenersContainer
)
288 lang::EventObject
aSource( static_cast< ::cppu::OWeakObject
*>( this ) );
289 m_pListenersContainer
->disposeAndClear( aSource
);
292 m_bDisposed
= sal_True
;
295 //-----------------------------------------------
296 void SAL_CALL
OFSStreamContainer::addEventListener( const uno::Reference
< lang::XEventListener
>& xListener
)
297 throw ( uno::RuntimeException
)
299 ::osl::MutexGuard
aGuard( m_aMutex
);
302 throw lang::DisposedException();
304 if ( !m_pListenersContainer
)
305 m_pListenersContainer
= new ::cppu::OInterfaceContainerHelper( m_aMutex
);
307 m_pListenersContainer
->addInterface( xListener
);
310 //-----------------------------------------------
311 void SAL_CALL
OFSStreamContainer::removeEventListener( const uno::Reference
< lang::XEventListener
>& xListener
)
312 throw ( uno::RuntimeException
)
314 ::osl::MutexGuard
aGuard( m_aMutex
);
317 throw lang::DisposedException();
319 if ( m_pListenersContainer
)
320 m_pListenersContainer
->removeInterface( xListener
);
325 //-----------------------------------------------
326 void SAL_CALL
OFSStreamContainer::seek( sal_Int64 location
)
327 throw ( lang::IllegalArgumentException
,
329 uno::RuntimeException
)
331 ::osl::MutexGuard
aGuard( m_aMutex
);
334 throw lang::DisposedException();
336 if ( !m_xStream
.is() || !m_xSeekable
.is() )
337 throw uno::RuntimeException();
339 m_xSeekable
->seek( location
);
342 //-----------------------------------------------
343 sal_Int64 SAL_CALL
OFSStreamContainer::getPosition()
344 throw ( io::IOException
,
345 uno::RuntimeException
)
347 ::osl::MutexGuard
aGuard( m_aMutex
);
350 throw lang::DisposedException();
352 if ( !m_xStream
.is() || !m_xSeekable
.is() )
353 throw uno::RuntimeException();
355 return m_xSeekable
->getPosition();
358 //-----------------------------------------------
359 sal_Int64 SAL_CALL
OFSStreamContainer::getLength()
360 throw ( io::IOException
,
361 uno::RuntimeException
)
363 ::osl::MutexGuard
aGuard( m_aMutex
);
366 throw lang::DisposedException();
368 if ( !m_xStream
.is() || !m_xSeekable
.is() )
369 throw uno::RuntimeException();
371 return m_xSeekable
->getLength();
376 //-----------------------------------------------
377 sal_Int32 SAL_CALL
OFSStreamContainer::readBytes( uno::Sequence
< sal_Int8
>& aData
, sal_Int32 nBytesToRead
)
378 throw( io::NotConnectedException
,
379 io::BufferSizeExceededException
,
381 uno::RuntimeException
)
383 ::osl::MutexGuard
aGuard( m_aMutex
);
386 throw lang::DisposedException();
388 if ( !m_xStream
.is() || !m_xInputStream
.is() )
389 throw uno::RuntimeException();
391 return m_xInputStream
->readBytes( aData
, nBytesToRead
);
394 //-----------------------------------------------
395 sal_Int32 SAL_CALL
OFSStreamContainer::readSomeBytes( uno::Sequence
< sal_Int8
>& aData
, sal_Int32 nMaxBytesToRead
)
396 throw( io::NotConnectedException
,
397 io::BufferSizeExceededException
,
399 uno::RuntimeException
)
401 ::osl::MutexGuard
aGuard( m_aMutex
);
404 throw lang::DisposedException();
406 if ( !m_xStream
.is() || !m_xInputStream
.is() )
407 throw uno::RuntimeException();
409 return m_xInputStream
->readSomeBytes( aData
, nMaxBytesToRead
);
412 //-----------------------------------------------
413 void SAL_CALL
OFSStreamContainer::skipBytes( sal_Int32 nBytesToSkip
)
414 throw( io::NotConnectedException
,
415 io::BufferSizeExceededException
,
417 uno::RuntimeException
)
419 ::osl::MutexGuard
aGuard( m_aMutex
);
422 throw lang::DisposedException();
424 if ( !m_xStream
.is() || !m_xInputStream
.is() )
425 throw uno::RuntimeException();
427 m_xInputStream
->skipBytes( nBytesToSkip
);
430 //-----------------------------------------------
431 sal_Int32 SAL_CALL
OFSStreamContainer::available()
432 throw( io::NotConnectedException
,
434 uno::RuntimeException
)
436 ::osl::MutexGuard
aGuard( m_aMutex
);
439 throw lang::DisposedException();
441 if ( !m_xStream
.is() || !m_xInputStream
.is() )
442 throw uno::RuntimeException();
444 return m_xInputStream
->available();
447 //-----------------------------------------------
448 void SAL_CALL
OFSStreamContainer::closeInput()
449 throw( io::NotConnectedException
,
451 uno::RuntimeException
)
453 ::osl::MutexGuard
aGuard( m_aMutex
);
456 throw lang::DisposedException();
458 if ( !m_xStream
.is() || !m_xInputStream
.is() )
459 throw uno::RuntimeException();
461 if ( m_xInputStream
.is() )
463 m_xInputStream
->closeInput();
464 m_bInputClosed
= sal_True
;
467 if ( m_bOutputClosed
)
472 //-----------------------------------------------
473 void SAL_CALL
OFSStreamContainer::writeBytes( const uno::Sequence
< sal_Int8
>& aData
)
474 throw ( io::NotConnectedException
,
475 io::BufferSizeExceededException
,
477 uno::RuntimeException
)
479 ::osl::MutexGuard
aGuard( m_aMutex
);
482 throw lang::DisposedException();
484 if ( !m_xStream
.is() || !m_xOutputStream
.is() )
485 throw uno::RuntimeException();
487 return m_xOutputStream
->writeBytes( aData
);
490 //-----------------------------------------------
491 void SAL_CALL
OFSStreamContainer::flush()
492 throw ( io::NotConnectedException
,
493 io::BufferSizeExceededException
,
495 uno::RuntimeException
)
497 ::osl::MutexGuard
aGuard( m_aMutex
);
500 throw lang::DisposedException();
502 if ( !m_xStream
.is() || !m_xOutputStream
.is() )
503 throw uno::RuntimeException();
505 return m_xOutputStream
->flush();
508 //-----------------------------------------------
509 void SAL_CALL
OFSStreamContainer::closeOutput()
510 throw ( io::NotConnectedException
,
511 io::BufferSizeExceededException
,
513 uno::RuntimeException
)
515 ::osl::MutexGuard
aGuard( m_aMutex
);
518 throw lang::DisposedException();
520 if ( !m_xStream
.is() || !m_xOutputStream
.is() )
521 throw uno::RuntimeException();
523 if ( m_xOutputStream
.is() )
525 m_xOutputStream
->closeOutput();
526 m_bOutputClosed
= sal_True
;
529 if ( m_bInputClosed
)
535 //-----------------------------------------------
536 void SAL_CALL
OFSStreamContainer::truncate()
537 throw ( io::IOException
,
538 uno::RuntimeException
)
540 ::osl::MutexGuard
aGuard( m_aMutex
);
543 throw lang::DisposedException();
545 if ( !m_xStream
.is() || !m_xTruncate
.is() )
546 throw uno::RuntimeException();
548 m_xTruncate
->truncate();
552 // XAsyncOutputMonitor
553 //-----------------------------------------------
554 void SAL_CALL
OFSStreamContainer::waitForCompletion()
555 throw ( io::IOException
,
556 uno::RuntimeException
)
558 ::osl::MutexGuard
aGuard( m_aMutex
);
561 throw lang::DisposedException();
563 if ( !m_xStream
.is() || !m_xAsyncOutputMonitor
.is() )
564 throw uno::RuntimeException();
566 m_xAsyncOutputMonitor
->waitForCompletion();