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>
23 #include <comphelper/sequence.hxx>
26 using namespace ::com::sun::star
;
28 OFSStreamContainer::OFSStreamContainer( const uno::Reference
< io::XStream
>& xStream
)
29 : m_bDisposed( false )
30 , m_bInputClosed( false )
31 , m_bOutputClosed( false )
36 if ( !m_xStream
.is() )
37 throw uno::RuntimeException();
39 m_xSeekable
.set( xStream
, uno::UNO_QUERY
);
40 m_xInputStream
= xStream
->getInputStream();
41 m_xOutputStream
= xStream
->getOutputStream();
42 m_xTruncate
.set( m_xOutputStream
, uno::UNO_QUERY
);
43 m_xAsyncOutputMonitor
.set( m_xOutputStream
, uno::UNO_QUERY
);
45 catch( uno::Exception
& )
49 m_xInputStream
.clear();
50 m_xOutputStream
.clear();
52 m_xAsyncOutputMonitor
.clear();
56 OFSStreamContainer::~OFSStreamContainer()
61 uno::Any SAL_CALL
OFSStreamContainer::queryInterface( const uno::Type
& rType
)
63 uno::Any aReturn
= ::cppu::queryInterface
65 , static_cast<lang::XTypeProvider
*> ( this )
66 , static_cast<io::XStream
*> ( this )
67 , static_cast<embed::XExtendedStorageStream
*> ( this )
68 , static_cast<lang::XComponent
*> ( this ) );
70 if ( aReturn
.hasValue() )
73 if ( m_xSeekable
.is() )
75 aReturn
= ::cppu::queryInterface
77 , static_cast<io::XSeekable
*> ( this ) );
79 if ( aReturn
.hasValue() )
83 if ( m_xInputStream
.is() )
85 aReturn
= ::cppu::queryInterface
87 , static_cast<io::XInputStream
*> ( this ) );
89 if ( aReturn
.hasValue() )
92 if ( m_xOutputStream
.is() )
94 aReturn
= ::cppu::queryInterface
96 , static_cast<io::XOutputStream
*> ( this ) );
98 if ( aReturn
.hasValue() )
101 if ( m_xTruncate
.is() )
103 aReturn
= ::cppu::queryInterface
105 , static_cast<io::XTruncate
*> ( this ) );
107 if ( aReturn
.hasValue() )
110 if ( m_xAsyncOutputMonitor
.is() )
112 aReturn
= ::cppu::queryInterface
114 , static_cast<io::XAsyncOutputMonitor
*> ( this ) );
116 if ( aReturn
.hasValue() )
120 return OWeakObject::queryInterface( rType
);
123 void SAL_CALL
OFSStreamContainer::acquire()
126 OWeakObject::acquire();
129 void SAL_CALL
OFSStreamContainer::release()
132 OWeakObject::release();
136 uno::Sequence
< uno::Type
> SAL_CALL
OFSStreamContainer::getTypes()
138 if ( !m_aTypes
.hasElements() )
140 ::osl::MutexGuard
aGuard( m_aMutex
);
142 if ( !m_aTypes
.hasElements() )
144 std::vector
<uno::Type
> tmp
;
145 tmp
.push_back(cppu::UnoType
<lang::XTypeProvider
>::get());
146 tmp
.push_back(cppu::UnoType
<embed::XExtendedStorageStream
>::get());
148 if ( m_xSeekable
.is() )
149 tmp
.push_back(cppu::UnoType
<io::XSeekable
>::get());
150 if ( m_xInputStream
.is() )
151 tmp
.push_back(cppu::UnoType
<io::XInputStream
>::get());
152 if ( m_xOutputStream
.is() )
153 tmp
.push_back(cppu::UnoType
<io::XOutputStream
>::get());
154 if ( m_xTruncate
.is() )
155 tmp
.push_back(cppu::UnoType
<io::XTruncate
>::get());
156 if ( m_xAsyncOutputMonitor
.is() )
157 tmp
.push_back(cppu::UnoType
<io::XAsyncOutputMonitor
>::get());
159 m_aTypes
= comphelper::containerToSequence(tmp
);
165 uno::Sequence
< sal_Int8
> SAL_CALL
OFSStreamContainer::getImplementationId()
167 return css::uno::Sequence
<sal_Int8
>();
171 uno::Reference
< io::XInputStream
> SAL_CALL
OFSStreamContainer::getInputStream()
173 ::osl::MutexGuard
aGuard( m_aMutex
);
176 throw lang::DisposedException();
178 if ( !m_xStream
.is() )
179 throw uno::RuntimeException();
181 if ( m_xInputStream
.is() )
182 return uno::Reference
< io::XInputStream
>( static_cast< io::XInputStream
* >( this ) );
184 return uno::Reference
< io::XInputStream
>();
187 uno::Reference
< io::XOutputStream
> SAL_CALL
OFSStreamContainer::getOutputStream()
189 ::osl::MutexGuard
aGuard( m_aMutex
);
192 throw lang::DisposedException();
194 if ( !m_xStream
.is() )
195 throw uno::RuntimeException();
197 if ( m_xOutputStream
.is() )
198 return uno::Reference
< io::XOutputStream
>( static_cast< io::XOutputStream
* >( this ) );
200 return uno::Reference
< io::XOutputStream
>();
204 void SAL_CALL
OFSStreamContainer::dispose()
206 ::osl::MutexGuard
aGuard( m_aMutex
);
209 throw lang::DisposedException();
211 if ( !m_xStream
.is() )
212 throw uno::RuntimeException();
214 if ( m_xInputStream
.is() && !m_bInputClosed
)
216 m_xInputStream
->closeInput();
217 m_bInputClosed
= true;
220 if ( m_xOutputStream
.is() && !m_bOutputClosed
)
222 m_xOutputStream
->closeOutput();
223 m_bOutputClosed
= true;
226 if ( m_pListenersContainer
)
228 lang::EventObject
aSource( static_cast< ::cppu::OWeakObject
*>( this ) );
229 m_pListenersContainer
->disposeAndClear( aSource
);
235 void SAL_CALL
OFSStreamContainer::addEventListener( const uno::Reference
< lang::XEventListener
>& xListener
)
237 ::osl::MutexGuard
aGuard( m_aMutex
);
240 throw lang::DisposedException();
242 if ( !m_pListenersContainer
)
243 m_pListenersContainer
.reset(new ::comphelper::OInterfaceContainerHelper2( m_aMutex
));
245 m_pListenersContainer
->addInterface( xListener
);
248 void SAL_CALL
OFSStreamContainer::removeEventListener( const uno::Reference
< lang::XEventListener
>& xListener
)
250 ::osl::MutexGuard
aGuard( m_aMutex
);
253 throw lang::DisposedException();
255 if ( m_pListenersContainer
)
256 m_pListenersContainer
->removeInterface( xListener
);
261 void SAL_CALL
OFSStreamContainer::seek( sal_Int64 location
)
263 ::osl::MutexGuard
aGuard( m_aMutex
);
266 throw lang::DisposedException();
268 if ( !m_xStream
.is() || !m_xSeekable
.is() )
269 throw uno::RuntimeException();
271 m_xSeekable
->seek( location
);
274 sal_Int64 SAL_CALL
OFSStreamContainer::getPosition()
276 ::osl::MutexGuard
aGuard( m_aMutex
);
279 throw lang::DisposedException();
281 if ( !m_xStream
.is() || !m_xSeekable
.is() )
282 throw uno::RuntimeException();
284 return m_xSeekable
->getPosition();
287 sal_Int64 SAL_CALL
OFSStreamContainer::getLength()
289 ::osl::MutexGuard
aGuard( m_aMutex
);
292 throw lang::DisposedException();
294 if ( !m_xStream
.is() || !m_xSeekable
.is() )
295 throw uno::RuntimeException();
297 return m_xSeekable
->getLength();
302 sal_Int32 SAL_CALL
OFSStreamContainer::readBytes( uno::Sequence
< sal_Int8
>& aData
, sal_Int32 nBytesToRead
)
304 ::osl::MutexGuard
aGuard( m_aMutex
);
307 throw lang::DisposedException();
309 if ( !m_xStream
.is() || !m_xInputStream
.is() )
310 throw uno::RuntimeException();
312 return m_xInputStream
->readBytes( aData
, nBytesToRead
);
315 sal_Int32 SAL_CALL
OFSStreamContainer::readSomeBytes( uno::Sequence
< sal_Int8
>& aData
, sal_Int32 nMaxBytesToRead
)
317 ::osl::MutexGuard
aGuard( m_aMutex
);
320 throw lang::DisposedException();
322 if ( !m_xStream
.is() || !m_xInputStream
.is() )
323 throw uno::RuntimeException();
325 return m_xInputStream
->readSomeBytes( aData
, nMaxBytesToRead
);
328 void SAL_CALL
OFSStreamContainer::skipBytes( sal_Int32 nBytesToSkip
)
330 ::osl::MutexGuard
aGuard( m_aMutex
);
333 throw lang::DisposedException();
335 if ( !m_xStream
.is() || !m_xInputStream
.is() )
336 throw uno::RuntimeException();
338 m_xInputStream
->skipBytes( nBytesToSkip
);
341 sal_Int32 SAL_CALL
OFSStreamContainer::available()
343 ::osl::MutexGuard
aGuard( m_aMutex
);
346 throw lang::DisposedException();
348 if ( !m_xStream
.is() || !m_xInputStream
.is() )
349 throw uno::RuntimeException();
351 return m_xInputStream
->available();
354 void SAL_CALL
OFSStreamContainer::closeInput()
356 ::osl::MutexGuard
aGuard( m_aMutex
);
359 throw lang::DisposedException();
361 if ( !m_xStream
.is() || !m_xInputStream
.is() )
362 throw uno::RuntimeException();
364 if ( m_xInputStream
.is() )
366 m_xInputStream
->closeInput();
367 m_bInputClosed
= true;
370 if ( m_bOutputClosed
)
375 void SAL_CALL
OFSStreamContainer::writeBytes( const uno::Sequence
< sal_Int8
>& aData
)
377 ::osl::MutexGuard
aGuard( m_aMutex
);
380 throw lang::DisposedException();
382 if ( !m_xStream
.is() || !m_xOutputStream
.is() )
383 throw uno::RuntimeException();
385 return m_xOutputStream
->writeBytes( aData
);
388 void SAL_CALL
OFSStreamContainer::flush()
390 ::osl::MutexGuard
aGuard( m_aMutex
);
393 throw lang::DisposedException();
395 if ( !m_xStream
.is() || !m_xOutputStream
.is() )
396 throw uno::RuntimeException();
398 return m_xOutputStream
->flush();
401 void SAL_CALL
OFSStreamContainer::closeOutput()
403 ::osl::MutexGuard
aGuard( m_aMutex
);
406 throw lang::DisposedException();
408 if ( !m_xStream
.is() || !m_xOutputStream
.is() )
409 throw uno::RuntimeException();
411 if ( m_xOutputStream
.is() )
413 m_xOutputStream
->closeOutput();
414 m_bOutputClosed
= true;
417 if ( m_bInputClosed
)
423 void SAL_CALL
OFSStreamContainer::truncate()
425 ::osl::MutexGuard
aGuard( m_aMutex
);
428 throw lang::DisposedException();
430 if ( !m_xStream
.is() || !m_xTruncate
.is() )
431 throw uno::RuntimeException();
433 m_xTruncate
->truncate();
437 // XAsyncOutputMonitor
438 void SAL_CALL
OFSStreamContainer::waitForCompletion()
440 ::osl::MutexGuard
aGuard( m_aMutex
);
443 throw lang::DisposedException();
445 if ( !m_xStream
.is() || !m_xAsyncOutputMonitor
.is() )
446 throw uno::RuntimeException();
448 m_xAsyncOutputMonitor
->waitForCompletion();
452 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */