bump product version to 6.3.0.0.beta1
[LibreOffice.git] / svl / source / fsstor / ostreamcontainer.cxx
blobef74bacbe7b0f5b49837cd28fd1ee25e75d02079
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 )
33 try
35 m_xStream = xStream;
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& )
47 m_xStream.clear();
48 m_xSeekable.clear();
49 m_xInputStream.clear();
50 m_xOutputStream.clear();
51 m_xTruncate.clear();
52 m_xAsyncOutputMonitor.clear();
56 OFSStreamContainer::~OFSStreamContainer()
60 // XInterface
61 uno::Any SAL_CALL OFSStreamContainer::queryInterface( const uno::Type& rType )
63 uno::Any aReturn = ::cppu::queryInterface
64 ( rType
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() )
71 return aReturn ;
73 if ( m_xSeekable.is() )
75 aReturn = ::cppu::queryInterface
76 ( rType
77 , static_cast<io::XSeekable*> ( this ) );
79 if ( aReturn.hasValue() )
80 return aReturn ;
83 if ( m_xInputStream.is() )
85 aReturn = ::cppu::queryInterface
86 ( rType
87 , static_cast<io::XInputStream*> ( this ) );
89 if ( aReturn.hasValue() )
90 return aReturn ;
92 if ( m_xOutputStream.is() )
94 aReturn = ::cppu::queryInterface
95 ( rType
96 , static_cast<io::XOutputStream*> ( this ) );
98 if ( aReturn.hasValue() )
99 return aReturn ;
101 if ( m_xTruncate.is() )
103 aReturn = ::cppu::queryInterface
104 ( rType
105 , static_cast<io::XTruncate*> ( this ) );
107 if ( aReturn.hasValue() )
108 return aReturn ;
110 if ( m_xAsyncOutputMonitor.is() )
112 aReturn = ::cppu::queryInterface
113 ( rType
114 , static_cast<io::XAsyncOutputMonitor*> ( this ) );
116 if ( aReturn.hasValue() )
117 return aReturn ;
120 return OWeakObject::queryInterface( rType );
123 void SAL_CALL OFSStreamContainer::acquire()
124 throw()
126 OWeakObject::acquire();
129 void SAL_CALL OFSStreamContainer::release()
130 throw()
132 OWeakObject::release();
135 // XTypeProvider
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);
162 return m_aTypes;
165 uno::Sequence< sal_Int8 > SAL_CALL OFSStreamContainer::getImplementationId()
167 return css::uno::Sequence<sal_Int8>();
170 // XStream
171 uno::Reference< io::XInputStream > SAL_CALL OFSStreamContainer::getInputStream()
173 ::osl::MutexGuard aGuard( m_aMutex );
175 if ( m_bDisposed )
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 );
191 if ( m_bDisposed )
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 >();
203 // XComponent
204 void SAL_CALL OFSStreamContainer::dispose()
206 ::osl::MutexGuard aGuard( m_aMutex );
208 if ( m_bDisposed )
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 );
232 m_bDisposed = true;
235 void SAL_CALL OFSStreamContainer::addEventListener( const uno::Reference< lang::XEventListener >& xListener )
237 ::osl::MutexGuard aGuard( m_aMutex );
239 if ( m_bDisposed )
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 );
252 if ( m_bDisposed )
253 throw lang::DisposedException();
255 if ( m_pListenersContainer )
256 m_pListenersContainer->removeInterface( xListener );
260 // XSeekable
261 void SAL_CALL OFSStreamContainer::seek( sal_Int64 location )
263 ::osl::MutexGuard aGuard( m_aMutex );
265 if ( m_bDisposed )
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 );
278 if ( m_bDisposed )
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 );
291 if ( m_bDisposed )
292 throw lang::DisposedException();
294 if ( !m_xStream.is() || !m_xSeekable.is() )
295 throw uno::RuntimeException();
297 return m_xSeekable->getLength();
301 // XInputStream
302 sal_Int32 SAL_CALL OFSStreamContainer::readBytes( uno::Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead )
304 ::osl::MutexGuard aGuard( m_aMutex );
306 if ( m_bDisposed )
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 );
319 if ( m_bDisposed )
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 );
332 if ( m_bDisposed )
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 );
345 if ( m_bDisposed )
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 );
358 if ( m_bDisposed )
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 )
371 dispose();
374 // XOutputStream
375 void SAL_CALL OFSStreamContainer::writeBytes( const uno::Sequence< sal_Int8 >& aData )
377 ::osl::MutexGuard aGuard( m_aMutex );
379 if ( m_bDisposed )
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 );
392 if ( m_bDisposed )
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 );
405 if ( m_bDisposed )
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 )
418 dispose();
422 // XTruncate
423 void SAL_CALL OFSStreamContainer::truncate()
425 ::osl::MutexGuard aGuard( m_aMutex );
427 if ( m_bDisposed )
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 );
442 if ( m_bDisposed )
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: */