1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
21 #include "ostreamcontainer.hxx"
24 using namespace ::com::sun::star
;
26 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 , m_pTypeCollection( NULL
)
36 if ( !m_xStream
.is() )
37 throw uno::RuntimeException();
39 m_xSeekable
= uno::Reference
< io::XSeekable
>( xStream
, uno::UNO_QUERY
);
40 m_xInputStream
= xStream
->getInputStream();
41 m_xOutputStream
= xStream
->getOutputStream();
42 m_xTruncate
= uno::Reference
< io::XTruncate
>( m_xOutputStream
, uno::UNO_QUERY
);
43 m_xAsyncOutputMonitor
= uno::Reference
< io::XAsyncOutputMonitor
>( m_xOutputStream
, uno::UNO_QUERY
);
45 catch( uno::Exception
& )
47 m_xStream
= uno::Reference
< io::XStream
>();
48 m_xSeekable
= uno::Reference
< io::XSeekable
>();
49 m_xInputStream
= uno::Reference
< io::XInputStream
>();
50 m_xOutputStream
= uno::Reference
< io::XOutputStream
>();
51 m_xTruncate
= uno::Reference
< io::XTruncate
>();
52 m_xAsyncOutputMonitor
= uno::Reference
< io::XAsyncOutputMonitor
>();
56 OFSStreamContainer::~OFSStreamContainer()
58 if ( m_pListenersContainer
)
60 delete m_pListenersContainer
;
61 m_pListenersContainer
= NULL
;
66 uno::Any SAL_CALL
OFSStreamContainer::queryInterface( const uno::Type
& rType
)
67 throw( uno::RuntimeException
)
71 aReturn
<<= ::cppu::queryInterface
73 , static_cast<lang::XTypeProvider
*> ( this )
74 , static_cast<io::XStream
*> ( this )
75 , static_cast<embed::XExtendedStorageStream
*> ( this )
76 , static_cast<lang::XComponent
*> ( this ) );
78 if ( aReturn
.hasValue() == sal_True
)
81 if ( m_xSeekable
.is() )
83 aReturn
<<= ::cppu::queryInterface
85 , static_cast<io::XSeekable
*> ( this ) );
87 if ( aReturn
.hasValue() == sal_True
)
91 if ( m_xInputStream
.is() )
93 aReturn
<<= ::cppu::queryInterface
95 , static_cast<io::XInputStream
*> ( this ) );
97 if ( aReturn
.hasValue() == sal_True
)
100 if ( m_xOutputStream
.is() )
102 aReturn
<<= ::cppu::queryInterface
104 , static_cast<io::XOutputStream
*> ( this ) );
106 if ( aReturn
.hasValue() == sal_True
)
109 if ( m_xTruncate
.is() )
111 aReturn
<<= ::cppu::queryInterface
113 , static_cast<io::XTruncate
*> ( this ) );
115 if ( aReturn
.hasValue() == sal_True
)
118 if ( m_xAsyncOutputMonitor
.is() )
120 aReturn
<<= ::cppu::queryInterface
122 , static_cast<io::XAsyncOutputMonitor
*> ( this ) );
124 if ( aReturn
.hasValue() == sal_True
)
128 return OWeakObject::queryInterface( rType
);
131 void SAL_CALL
OFSStreamContainer::acquire()
134 OWeakObject::acquire();
137 void SAL_CALL
OFSStreamContainer::release()
140 OWeakObject::release();
144 uno::Sequence
< uno::Type
> SAL_CALL
OFSStreamContainer::getTypes()
145 throw( uno::RuntimeException
)
147 if ( m_pTypeCollection
== NULL
)
149 ::osl::MutexGuard
aGuard( m_aMutex
);
151 if ( m_pTypeCollection
== NULL
)
153 ::cppu::OTypeCollection aTypeCollection
154 ( ::getCppuType( ( const uno::Reference
< lang::XTypeProvider
>* )NULL
)
155 , ::getCppuType( ( const uno::Reference
< embed::XExtendedStorageStream
>* )NULL
) );
157 if ( m_xSeekable
.is() )
158 aTypeCollection
= ::cppu::OTypeCollection
159 ( ::getCppuType( ( const uno::Reference
< io::XSeekable
>* )NULL
),
160 aTypeCollection
.getTypes() );
161 if ( m_xInputStream
.is() )
162 aTypeCollection
= ::cppu::OTypeCollection
163 ( ::getCppuType( ( const uno::Reference
< io::XInputStream
>* )NULL
),
164 aTypeCollection
.getTypes() );
166 if ( m_xOutputStream
.is() )
167 aTypeCollection
= ::cppu::OTypeCollection
168 ( ::getCppuType( ( const uno::Reference
< io::XOutputStream
>* )NULL
),
169 aTypeCollection
.getTypes() );
170 if ( m_xTruncate
.is() )
171 aTypeCollection
= ::cppu::OTypeCollection
172 ( ::getCppuType( ( const uno::Reference
< io::XTruncate
>* )NULL
),
173 aTypeCollection
.getTypes() );
174 if ( m_xAsyncOutputMonitor
.is() )
175 aTypeCollection
= ::cppu::OTypeCollection
176 ( ::getCppuType( ( const uno::Reference
< io::XAsyncOutputMonitor
>* )NULL
),
177 aTypeCollection
.getTypes() );
179 m_pTypeCollection
= new ::cppu::OTypeCollection( aTypeCollection
);
182 return m_pTypeCollection
->getTypes() ;
185 uno::Sequence
< sal_Int8
> SAL_CALL
OFSStreamContainer::getImplementationId()
186 throw( uno::RuntimeException
)
188 static ::cppu::OImplementationId
* pID
= NULL
;
192 ::osl::MutexGuard
aGuard( ::osl::Mutex::getGlobalMutex() ) ;
196 static ::cppu::OImplementationId
aID( sal_False
) ;
201 return pID
->getImplementationId() ;
205 uno::Reference
< io::XInputStream
> SAL_CALL
OFSStreamContainer::getInputStream()
206 throw ( uno::RuntimeException
)
208 ::osl::MutexGuard
aGuard( m_aMutex
);
211 throw lang::DisposedException();
213 if ( !m_xStream
.is() )
214 throw uno::RuntimeException();
216 if ( m_xInputStream
.is() )
217 return uno::Reference
< io::XInputStream
>( static_cast< io::XInputStream
* >( this ) );
219 return uno::Reference
< io::XInputStream
>();
222 uno::Reference
< io::XOutputStream
> SAL_CALL
OFSStreamContainer::getOutputStream()
223 throw ( uno::RuntimeException
)
225 ::osl::MutexGuard
aGuard( m_aMutex
);
228 throw lang::DisposedException();
230 if ( !m_xStream
.is() )
231 throw uno::RuntimeException();
233 if ( m_xOutputStream
.is() )
234 return uno::Reference
< io::XOutputStream
>( static_cast< io::XOutputStream
* >( this ) );
236 return uno::Reference
< io::XOutputStream
>();
240 void SAL_CALL
OFSStreamContainer::dispose()
241 throw ( uno::RuntimeException
)
243 ::osl::MutexGuard
aGuard( m_aMutex
);
246 throw lang::DisposedException();
248 if ( !m_xStream
.is() )
249 throw uno::RuntimeException();
251 if ( m_xInputStream
.is() && !m_bInputClosed
)
253 m_xInputStream
->closeInput();
254 m_bInputClosed
= true;
257 if ( m_xOutputStream
.is() && !m_bOutputClosed
)
259 m_xOutputStream
->closeOutput();
260 m_bOutputClosed
= true;
263 if ( m_pListenersContainer
)
265 lang::EventObject
aSource( static_cast< ::cppu::OWeakObject
*>( this ) );
266 m_pListenersContainer
->disposeAndClear( aSource
);
272 void SAL_CALL
OFSStreamContainer::addEventListener( const uno::Reference
< lang::XEventListener
>& xListener
)
273 throw ( uno::RuntimeException
)
275 ::osl::MutexGuard
aGuard( m_aMutex
);
278 throw lang::DisposedException();
280 if ( !m_pListenersContainer
)
281 m_pListenersContainer
= new ::cppu::OInterfaceContainerHelper( m_aMutex
);
283 m_pListenersContainer
->addInterface( xListener
);
286 void SAL_CALL
OFSStreamContainer::removeEventListener( const uno::Reference
< lang::XEventListener
>& xListener
)
287 throw ( uno::RuntimeException
)
289 ::osl::MutexGuard
aGuard( m_aMutex
);
292 throw lang::DisposedException();
294 if ( m_pListenersContainer
)
295 m_pListenersContainer
->removeInterface( xListener
);
300 void SAL_CALL
OFSStreamContainer::seek( sal_Int64 location
)
301 throw ( lang::IllegalArgumentException
,
303 uno::RuntimeException
)
305 ::osl::MutexGuard
aGuard( m_aMutex
);
308 throw lang::DisposedException();
310 if ( !m_xStream
.is() || !m_xSeekable
.is() )
311 throw uno::RuntimeException();
313 m_xSeekable
->seek( location
);
316 sal_Int64 SAL_CALL
OFSStreamContainer::getPosition()
317 throw ( io::IOException
,
318 uno::RuntimeException
)
320 ::osl::MutexGuard
aGuard( m_aMutex
);
323 throw lang::DisposedException();
325 if ( !m_xStream
.is() || !m_xSeekable
.is() )
326 throw uno::RuntimeException();
328 return m_xSeekable
->getPosition();
331 sal_Int64 SAL_CALL
OFSStreamContainer::getLength()
332 throw ( io::IOException
,
333 uno::RuntimeException
)
335 ::osl::MutexGuard
aGuard( m_aMutex
);
338 throw lang::DisposedException();
340 if ( !m_xStream
.is() || !m_xSeekable
.is() )
341 throw uno::RuntimeException();
343 return m_xSeekable
->getLength();
348 sal_Int32 SAL_CALL
OFSStreamContainer::readBytes( uno::Sequence
< sal_Int8
>& aData
, sal_Int32 nBytesToRead
)
349 throw( io::NotConnectedException
,
350 io::BufferSizeExceededException
,
352 uno::RuntimeException
)
354 ::osl::MutexGuard
aGuard( m_aMutex
);
357 throw lang::DisposedException();
359 if ( !m_xStream
.is() || !m_xInputStream
.is() )
360 throw uno::RuntimeException();
362 return m_xInputStream
->readBytes( aData
, nBytesToRead
);
365 sal_Int32 SAL_CALL
OFSStreamContainer::readSomeBytes( uno::Sequence
< sal_Int8
>& aData
, sal_Int32 nMaxBytesToRead
)
366 throw( io::NotConnectedException
,
367 io::BufferSizeExceededException
,
369 uno::RuntimeException
)
371 ::osl::MutexGuard
aGuard( m_aMutex
);
374 throw lang::DisposedException();
376 if ( !m_xStream
.is() || !m_xInputStream
.is() )
377 throw uno::RuntimeException();
379 return m_xInputStream
->readSomeBytes( aData
, nMaxBytesToRead
);
382 void SAL_CALL
OFSStreamContainer::skipBytes( sal_Int32 nBytesToSkip
)
383 throw( io::NotConnectedException
,
384 io::BufferSizeExceededException
,
386 uno::RuntimeException
)
388 ::osl::MutexGuard
aGuard( m_aMutex
);
391 throw lang::DisposedException();
393 if ( !m_xStream
.is() || !m_xInputStream
.is() )
394 throw uno::RuntimeException();
396 m_xInputStream
->skipBytes( nBytesToSkip
);
399 sal_Int32 SAL_CALL
OFSStreamContainer::available()
400 throw( io::NotConnectedException
,
402 uno::RuntimeException
)
404 ::osl::MutexGuard
aGuard( m_aMutex
);
407 throw lang::DisposedException();
409 if ( !m_xStream
.is() || !m_xInputStream
.is() )
410 throw uno::RuntimeException();
412 return m_xInputStream
->available();
415 void SAL_CALL
OFSStreamContainer::closeInput()
416 throw( io::NotConnectedException
,
418 uno::RuntimeException
)
420 ::osl::MutexGuard
aGuard( m_aMutex
);
423 throw lang::DisposedException();
425 if ( !m_xStream
.is() || !m_xInputStream
.is() )
426 throw uno::RuntimeException();
428 if ( m_xInputStream
.is() )
430 m_xInputStream
->closeInput();
431 m_bInputClosed
= true;
434 if ( m_bOutputClosed
)
439 void SAL_CALL
OFSStreamContainer::writeBytes( const uno::Sequence
< sal_Int8
>& aData
)
440 throw ( io::NotConnectedException
,
441 io::BufferSizeExceededException
,
443 uno::RuntimeException
)
445 ::osl::MutexGuard
aGuard( m_aMutex
);
448 throw lang::DisposedException();
450 if ( !m_xStream
.is() || !m_xOutputStream
.is() )
451 throw uno::RuntimeException();
453 return m_xOutputStream
->writeBytes( aData
);
456 void SAL_CALL
OFSStreamContainer::flush()
457 throw ( io::NotConnectedException
,
458 io::BufferSizeExceededException
,
460 uno::RuntimeException
)
462 ::osl::MutexGuard
aGuard( m_aMutex
);
465 throw lang::DisposedException();
467 if ( !m_xStream
.is() || !m_xOutputStream
.is() )
468 throw uno::RuntimeException();
470 return m_xOutputStream
->flush();
473 void SAL_CALL
OFSStreamContainer::closeOutput()
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 if ( m_xOutputStream
.is() )
489 m_xOutputStream
->closeOutput();
490 m_bOutputClosed
= true;
493 if ( m_bInputClosed
)
499 void SAL_CALL
OFSStreamContainer::truncate()
500 throw ( io::IOException
,
501 uno::RuntimeException
)
503 ::osl::MutexGuard
aGuard( m_aMutex
);
506 throw lang::DisposedException();
508 if ( !m_xStream
.is() || !m_xTruncate
.is() )
509 throw uno::RuntimeException();
511 m_xTruncate
->truncate();
515 // XAsyncOutputMonitor
516 void SAL_CALL
OFSStreamContainer::waitForCompletion()
517 throw ( io::IOException
,
518 uno::RuntimeException
)
520 ::osl::MutexGuard
aGuard( m_aMutex
);
523 throw lang::DisposedException();
525 if ( !m_xStream
.is() || !m_xAsyncOutputMonitor
.is() )
526 throw uno::RuntimeException();
528 m_xAsyncOutputMonitor
->waitForCompletion();
533 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */