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 .
20 #include "ostreamcontainer.hxx"
22 #include <cppuhelper/queryinterface.hxx>
25 using namespace ::com::sun::star
;
27 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 , m_pTypeCollection( NULL
)
37 if ( !m_xStream
.is() )
38 throw uno::RuntimeException();
40 m_xSeekable
= uno::Reference
< io::XSeekable
>( xStream
, uno::UNO_QUERY
);
41 m_xInputStream
= xStream
->getInputStream();
42 m_xOutputStream
= xStream
->getOutputStream();
43 m_xTruncate
= uno::Reference
< io::XTruncate
>( m_xOutputStream
, uno::UNO_QUERY
);
44 m_xAsyncOutputMonitor
= uno::Reference
< io::XAsyncOutputMonitor
>( m_xOutputStream
, uno::UNO_QUERY
);
46 catch( uno::Exception
& )
48 m_xStream
= uno::Reference
< io::XStream
>();
49 m_xSeekable
= uno::Reference
< io::XSeekable
>();
50 m_xInputStream
= uno::Reference
< io::XInputStream
>();
51 m_xOutputStream
= uno::Reference
< io::XOutputStream
>();
52 m_xTruncate
= uno::Reference
< io::XTruncate
>();
53 m_xAsyncOutputMonitor
= uno::Reference
< io::XAsyncOutputMonitor
>();
57 OFSStreamContainer::~OFSStreamContainer()
59 if ( m_pListenersContainer
)
61 delete m_pListenersContainer
;
62 m_pListenersContainer
= NULL
;
67 uno::Any SAL_CALL
OFSStreamContainer::queryInterface( const uno::Type
& rType
)
68 throw( uno::RuntimeException
, std::exception
)
72 aReturn
<<= ::cppu::queryInterface
74 , static_cast<lang::XTypeProvider
*> ( this )
75 , static_cast<io::XStream
*> ( this )
76 , static_cast<embed::XExtendedStorageStream
*> ( this )
77 , static_cast<lang::XComponent
*> ( this ) );
79 if ( aReturn
.hasValue() )
82 if ( m_xSeekable
.is() )
84 aReturn
<<= ::cppu::queryInterface
86 , static_cast<io::XSeekable
*> ( this ) );
88 if ( aReturn
.hasValue() )
92 if ( m_xInputStream
.is() )
94 aReturn
<<= ::cppu::queryInterface
96 , static_cast<io::XInputStream
*> ( this ) );
98 if ( aReturn
.hasValue() )
101 if ( m_xOutputStream
.is() )
103 aReturn
<<= ::cppu::queryInterface
105 , static_cast<io::XOutputStream
*> ( this ) );
107 if ( aReturn
.hasValue() )
110 if ( m_xTruncate
.is() )
112 aReturn
<<= ::cppu::queryInterface
114 , static_cast<io::XTruncate
*> ( this ) );
116 if ( aReturn
.hasValue() )
119 if ( m_xAsyncOutputMonitor
.is() )
121 aReturn
<<= ::cppu::queryInterface
123 , static_cast<io::XAsyncOutputMonitor
*> ( this ) );
125 if ( aReturn
.hasValue() )
129 return OWeakObject::queryInterface( rType
);
132 void SAL_CALL
OFSStreamContainer::acquire()
135 OWeakObject::acquire();
138 void SAL_CALL
OFSStreamContainer::release()
141 OWeakObject::release();
145 uno::Sequence
< uno::Type
> SAL_CALL
OFSStreamContainer::getTypes()
146 throw( uno::RuntimeException
, std::exception
)
148 if ( m_pTypeCollection
== NULL
)
150 ::osl::MutexGuard
aGuard( m_aMutex
);
152 if ( m_pTypeCollection
== NULL
)
154 ::cppu::OTypeCollection aTypeCollection
155 ( cppu::UnoType
<lang::XTypeProvider
>::get()
156 , cppu::UnoType
<embed::XExtendedStorageStream
>::get());
158 if ( m_xSeekable
.is() )
159 aTypeCollection
= ::cppu::OTypeCollection
160 ( cppu::UnoType
<io::XSeekable
>::get(),
161 aTypeCollection
.getTypes() );
162 if ( m_xInputStream
.is() )
163 aTypeCollection
= ::cppu::OTypeCollection
164 ( cppu::UnoType
<io::XInputStream
>::get(),
165 aTypeCollection
.getTypes() );
167 if ( m_xOutputStream
.is() )
168 aTypeCollection
= ::cppu::OTypeCollection
169 ( cppu::UnoType
<io::XOutputStream
>::get(),
170 aTypeCollection
.getTypes() );
171 if ( m_xTruncate
.is() )
172 aTypeCollection
= ::cppu::OTypeCollection
173 ( cppu::UnoType
<io::XTruncate
>::get(),
174 aTypeCollection
.getTypes() );
175 if ( m_xAsyncOutputMonitor
.is() )
176 aTypeCollection
= ::cppu::OTypeCollection
177 ( cppu::UnoType
<io::XAsyncOutputMonitor
>::get(),
178 aTypeCollection
.getTypes() );
180 m_pTypeCollection
= new ::cppu::OTypeCollection( aTypeCollection
);
183 return m_pTypeCollection
->getTypes() ;
186 uno::Sequence
< sal_Int8
> SAL_CALL
OFSStreamContainer::getImplementationId()
187 throw( uno::RuntimeException
, std::exception
)
189 return css::uno::Sequence
<sal_Int8
>();
193 uno::Reference
< io::XInputStream
> SAL_CALL
OFSStreamContainer::getInputStream()
194 throw ( uno::RuntimeException
, std::exception
)
196 ::osl::MutexGuard
aGuard( m_aMutex
);
199 throw lang::DisposedException();
201 if ( !m_xStream
.is() )
202 throw uno::RuntimeException();
204 if ( m_xInputStream
.is() )
205 return uno::Reference
< io::XInputStream
>( static_cast< io::XInputStream
* >( this ) );
207 return uno::Reference
< io::XInputStream
>();
210 uno::Reference
< io::XOutputStream
> SAL_CALL
OFSStreamContainer::getOutputStream()
211 throw ( uno::RuntimeException
, std::exception
)
213 ::osl::MutexGuard
aGuard( m_aMutex
);
216 throw lang::DisposedException();
218 if ( !m_xStream
.is() )
219 throw uno::RuntimeException();
221 if ( m_xOutputStream
.is() )
222 return uno::Reference
< io::XOutputStream
>( static_cast< io::XOutputStream
* >( this ) );
224 return uno::Reference
< io::XOutputStream
>();
228 void SAL_CALL
OFSStreamContainer::dispose()
229 throw ( uno::RuntimeException
, std::exception
)
231 ::osl::MutexGuard
aGuard( m_aMutex
);
234 throw lang::DisposedException();
236 if ( !m_xStream
.is() )
237 throw uno::RuntimeException();
239 if ( m_xInputStream
.is() && !m_bInputClosed
)
241 m_xInputStream
->closeInput();
242 m_bInputClosed
= true;
245 if ( m_xOutputStream
.is() && !m_bOutputClosed
)
247 m_xOutputStream
->closeOutput();
248 m_bOutputClosed
= true;
251 if ( m_pListenersContainer
)
253 lang::EventObject
aSource( static_cast< ::cppu::OWeakObject
*>( this ) );
254 m_pListenersContainer
->disposeAndClear( aSource
);
260 void SAL_CALL
OFSStreamContainer::addEventListener( const uno::Reference
< lang::XEventListener
>& xListener
)
261 throw ( uno::RuntimeException
, std::exception
)
263 ::osl::MutexGuard
aGuard( m_aMutex
);
266 throw lang::DisposedException();
268 if ( !m_pListenersContainer
)
269 m_pListenersContainer
= new ::cppu::OInterfaceContainerHelper( m_aMutex
);
271 m_pListenersContainer
->addInterface( xListener
);
274 void SAL_CALL
OFSStreamContainer::removeEventListener( const uno::Reference
< lang::XEventListener
>& xListener
)
275 throw ( uno::RuntimeException
, std::exception
)
277 ::osl::MutexGuard
aGuard( m_aMutex
);
280 throw lang::DisposedException();
282 if ( m_pListenersContainer
)
283 m_pListenersContainer
->removeInterface( xListener
);
288 void SAL_CALL
OFSStreamContainer::seek( sal_Int64 location
)
289 throw ( lang::IllegalArgumentException
,
291 uno::RuntimeException
, std::exception
)
293 ::osl::MutexGuard
aGuard( m_aMutex
);
296 throw lang::DisposedException();
298 if ( !m_xStream
.is() || !m_xSeekable
.is() )
299 throw uno::RuntimeException();
301 m_xSeekable
->seek( location
);
304 sal_Int64 SAL_CALL
OFSStreamContainer::getPosition()
305 throw ( io::IOException
,
306 uno::RuntimeException
, std::exception
)
308 ::osl::MutexGuard
aGuard( m_aMutex
);
311 throw lang::DisposedException();
313 if ( !m_xStream
.is() || !m_xSeekable
.is() )
314 throw uno::RuntimeException();
316 return m_xSeekable
->getPosition();
319 sal_Int64 SAL_CALL
OFSStreamContainer::getLength()
320 throw ( io::IOException
,
321 uno::RuntimeException
, std::exception
)
323 ::osl::MutexGuard
aGuard( m_aMutex
);
326 throw lang::DisposedException();
328 if ( !m_xStream
.is() || !m_xSeekable
.is() )
329 throw uno::RuntimeException();
331 return m_xSeekable
->getLength();
336 sal_Int32 SAL_CALL
OFSStreamContainer::readBytes( uno::Sequence
< sal_Int8
>& aData
, sal_Int32 nBytesToRead
)
337 throw( io::NotConnectedException
,
338 io::BufferSizeExceededException
,
340 uno::RuntimeException
, std::exception
)
342 ::osl::MutexGuard
aGuard( m_aMutex
);
345 throw lang::DisposedException();
347 if ( !m_xStream
.is() || !m_xInputStream
.is() )
348 throw uno::RuntimeException();
350 return m_xInputStream
->readBytes( aData
, nBytesToRead
);
353 sal_Int32 SAL_CALL
OFSStreamContainer::readSomeBytes( uno::Sequence
< sal_Int8
>& aData
, sal_Int32 nMaxBytesToRead
)
354 throw( io::NotConnectedException
,
355 io::BufferSizeExceededException
,
357 uno::RuntimeException
, std::exception
)
359 ::osl::MutexGuard
aGuard( m_aMutex
);
362 throw lang::DisposedException();
364 if ( !m_xStream
.is() || !m_xInputStream
.is() )
365 throw uno::RuntimeException();
367 return m_xInputStream
->readSomeBytes( aData
, nMaxBytesToRead
);
370 void SAL_CALL
OFSStreamContainer::skipBytes( sal_Int32 nBytesToSkip
)
371 throw( io::NotConnectedException
,
372 io::BufferSizeExceededException
,
374 uno::RuntimeException
, std::exception
)
376 ::osl::MutexGuard
aGuard( m_aMutex
);
379 throw lang::DisposedException();
381 if ( !m_xStream
.is() || !m_xInputStream
.is() )
382 throw uno::RuntimeException();
384 m_xInputStream
->skipBytes( nBytesToSkip
);
387 sal_Int32 SAL_CALL
OFSStreamContainer::available()
388 throw( io::NotConnectedException
,
390 uno::RuntimeException
, std::exception
)
392 ::osl::MutexGuard
aGuard( m_aMutex
);
395 throw lang::DisposedException();
397 if ( !m_xStream
.is() || !m_xInputStream
.is() )
398 throw uno::RuntimeException();
400 return m_xInputStream
->available();
403 void SAL_CALL
OFSStreamContainer::closeInput()
404 throw( io::NotConnectedException
,
406 uno::RuntimeException
, std::exception
)
408 ::osl::MutexGuard
aGuard( m_aMutex
);
411 throw lang::DisposedException();
413 if ( !m_xStream
.is() || !m_xInputStream
.is() )
414 throw uno::RuntimeException();
416 if ( m_xInputStream
.is() )
418 m_xInputStream
->closeInput();
419 m_bInputClosed
= true;
422 if ( m_bOutputClosed
)
427 void SAL_CALL
OFSStreamContainer::writeBytes( const uno::Sequence
< sal_Int8
>& aData
)
428 throw ( io::NotConnectedException
,
429 io::BufferSizeExceededException
,
431 uno::RuntimeException
, std::exception
)
433 ::osl::MutexGuard
aGuard( m_aMutex
);
436 throw lang::DisposedException();
438 if ( !m_xStream
.is() || !m_xOutputStream
.is() )
439 throw uno::RuntimeException();
441 return m_xOutputStream
->writeBytes( aData
);
444 void SAL_CALL
OFSStreamContainer::flush()
445 throw ( io::NotConnectedException
,
446 io::BufferSizeExceededException
,
448 uno::RuntimeException
, std::exception
)
450 ::osl::MutexGuard
aGuard( m_aMutex
);
453 throw lang::DisposedException();
455 if ( !m_xStream
.is() || !m_xOutputStream
.is() )
456 throw uno::RuntimeException();
458 return m_xOutputStream
->flush();
461 void SAL_CALL
OFSStreamContainer::closeOutput()
462 throw ( io::NotConnectedException
,
463 io::BufferSizeExceededException
,
465 uno::RuntimeException
, std::exception
)
467 ::osl::MutexGuard
aGuard( m_aMutex
);
470 throw lang::DisposedException();
472 if ( !m_xStream
.is() || !m_xOutputStream
.is() )
473 throw uno::RuntimeException();
475 if ( m_xOutputStream
.is() )
477 m_xOutputStream
->closeOutput();
478 m_bOutputClosed
= true;
481 if ( m_bInputClosed
)
487 void SAL_CALL
OFSStreamContainer::truncate()
488 throw ( io::IOException
,
489 uno::RuntimeException
, std::exception
)
491 ::osl::MutexGuard
aGuard( m_aMutex
);
494 throw lang::DisposedException();
496 if ( !m_xStream
.is() || !m_xTruncate
.is() )
497 throw uno::RuntimeException();
499 m_xTruncate
->truncate();
503 // XAsyncOutputMonitor
504 void SAL_CALL
OFSStreamContainer::waitForCompletion()
505 throw ( io::IOException
,
506 uno::RuntimeException
, std::exception
)
508 ::osl::MutexGuard
aGuard( m_aMutex
);
511 throw lang::DisposedException();
513 if ( !m_xStream
.is() || !m_xAsyncOutputMonitor
.is() )
514 throw uno::RuntimeException();
516 m_xAsyncOutputMonitor
->waitForCompletion();
521 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */