Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #include "ostreamcontainer.hxx"
21 :
22 : #include <cppuhelper/queryinterface.hxx>
23 :
24 :
25 : using namespace ::com::sun::star;
26 :
27 0 : 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 0 : , m_pTypeCollection( NULL )
33 : {
34 : try
35 : {
36 0 : m_xStream = xStream;
37 0 : if ( !m_xStream.is() )
38 0 : throw uno::RuntimeException();
39 :
40 0 : m_xSeekable = uno::Reference< io::XSeekable >( xStream, uno::UNO_QUERY );
41 0 : m_xInputStream = xStream->getInputStream();
42 0 : m_xOutputStream = xStream->getOutputStream();
43 0 : m_xTruncate = uno::Reference< io::XTruncate >( m_xOutputStream, uno::UNO_QUERY );
44 0 : m_xAsyncOutputMonitor = uno::Reference< io::XAsyncOutputMonitor >( m_xOutputStream, uno::UNO_QUERY );
45 : }
46 0 : catch( uno::Exception& )
47 : {
48 0 : m_xStream = uno::Reference< io::XStream >();
49 0 : m_xSeekable = uno::Reference< io::XSeekable >();
50 0 : m_xInputStream = uno::Reference< io::XInputStream >();
51 0 : m_xOutputStream = uno::Reference< io::XOutputStream >();
52 0 : m_xTruncate = uno::Reference< io::XTruncate >();
53 0 : m_xAsyncOutputMonitor = uno::Reference< io::XAsyncOutputMonitor >();
54 : }
55 0 : }
56 :
57 0 : OFSStreamContainer::~OFSStreamContainer()
58 : {
59 0 : if ( m_pListenersContainer )
60 : {
61 0 : delete m_pListenersContainer;
62 0 : m_pListenersContainer = NULL;
63 : }
64 0 : }
65 :
66 : // XInterface
67 0 : uno::Any SAL_CALL OFSStreamContainer::queryInterface( const uno::Type& rType )
68 : throw( uno::RuntimeException, std::exception )
69 : {
70 0 : uno::Any aReturn;
71 :
72 0 : aReturn <<= ::cppu::queryInterface
73 : ( rType
74 : , static_cast<lang::XTypeProvider*> ( this )
75 : , static_cast<io::XStream*> ( this )
76 : , static_cast<embed::XExtendedStorageStream*> ( this )
77 0 : , static_cast<lang::XComponent*> ( this ) );
78 :
79 0 : if ( aReturn.hasValue() )
80 0 : return aReturn ;
81 :
82 0 : if ( m_xSeekable.is() )
83 : {
84 0 : aReturn <<= ::cppu::queryInterface
85 : ( rType
86 0 : , static_cast<io::XSeekable*> ( this ) );
87 :
88 0 : if ( aReturn.hasValue() )
89 0 : return aReturn ;
90 : }
91 :
92 0 : if ( m_xInputStream.is() )
93 : {
94 0 : aReturn <<= ::cppu::queryInterface
95 : ( rType
96 0 : , static_cast<io::XInputStream*> ( this ) );
97 :
98 0 : if ( aReturn.hasValue() )
99 0 : return aReturn ;
100 : }
101 0 : if ( m_xOutputStream.is() )
102 : {
103 0 : aReturn <<= ::cppu::queryInterface
104 : ( rType
105 0 : , static_cast<io::XOutputStream*> ( this ) );
106 :
107 0 : if ( aReturn.hasValue() )
108 0 : return aReturn ;
109 : }
110 0 : if ( m_xTruncate.is() )
111 : {
112 0 : aReturn <<= ::cppu::queryInterface
113 : ( rType
114 0 : , static_cast<io::XTruncate*> ( this ) );
115 :
116 0 : if ( aReturn.hasValue() )
117 0 : return aReturn ;
118 : }
119 0 : if ( m_xAsyncOutputMonitor.is() )
120 : {
121 0 : aReturn <<= ::cppu::queryInterface
122 : ( rType
123 0 : , static_cast<io::XAsyncOutputMonitor*> ( this ) );
124 :
125 0 : if ( aReturn.hasValue() )
126 0 : return aReturn ;
127 : }
128 :
129 0 : return OWeakObject::queryInterface( rType );
130 : }
131 :
132 0 : void SAL_CALL OFSStreamContainer::acquire()
133 : throw()
134 : {
135 0 : OWeakObject::acquire();
136 0 : }
137 :
138 0 : void SAL_CALL OFSStreamContainer::release()
139 : throw()
140 : {
141 0 : OWeakObject::release();
142 0 : }
143 :
144 : // XTypeProvider
145 0 : uno::Sequence< uno::Type > SAL_CALL OFSStreamContainer::getTypes()
146 : throw( uno::RuntimeException, std::exception )
147 : {
148 0 : if ( m_pTypeCollection == NULL )
149 : {
150 0 : ::osl::MutexGuard aGuard( m_aMutex );
151 :
152 0 : if ( m_pTypeCollection == NULL )
153 : {
154 : ::cppu::OTypeCollection aTypeCollection
155 0 : ( cppu::UnoType<lang::XTypeProvider>::get()
156 0 : , cppu::UnoType<embed::XExtendedStorageStream>::get());
157 :
158 0 : if ( m_xSeekable.is() )
159 0 : aTypeCollection = ::cppu::OTypeCollection
160 0 : ( cppu::UnoType<io::XSeekable>::get(),
161 0 : aTypeCollection.getTypes() );
162 0 : if ( m_xInputStream.is() )
163 0 : aTypeCollection = ::cppu::OTypeCollection
164 0 : ( cppu::UnoType<io::XInputStream>::get(),
165 0 : aTypeCollection.getTypes() );
166 :
167 0 : if ( m_xOutputStream.is() )
168 0 : aTypeCollection = ::cppu::OTypeCollection
169 0 : ( cppu::UnoType<io::XOutputStream>::get(),
170 0 : aTypeCollection.getTypes() );
171 0 : if ( m_xTruncate.is() )
172 0 : aTypeCollection = ::cppu::OTypeCollection
173 0 : ( cppu::UnoType<io::XTruncate>::get(),
174 0 : aTypeCollection.getTypes() );
175 0 : if ( m_xAsyncOutputMonitor.is() )
176 0 : aTypeCollection = ::cppu::OTypeCollection
177 0 : ( cppu::UnoType<io::XAsyncOutputMonitor>::get(),
178 0 : aTypeCollection.getTypes() );
179 :
180 0 : m_pTypeCollection = new ::cppu::OTypeCollection( aTypeCollection );
181 0 : }
182 : }
183 0 : return m_pTypeCollection->getTypes() ;
184 : }
185 :
186 0 : uno::Sequence< sal_Int8 > SAL_CALL OFSStreamContainer::getImplementationId()
187 : throw( uno::RuntimeException, std::exception )
188 : {
189 0 : return css::uno::Sequence<sal_Int8>();
190 : }
191 :
192 : // XStream
193 0 : uno::Reference< io::XInputStream > SAL_CALL OFSStreamContainer::getInputStream()
194 : throw ( uno::RuntimeException, std::exception )
195 : {
196 0 : ::osl::MutexGuard aGuard( m_aMutex );
197 :
198 0 : if ( m_bDisposed )
199 0 : throw lang::DisposedException();
200 :
201 0 : if ( !m_xStream.is() )
202 0 : throw uno::RuntimeException();
203 :
204 0 : if ( m_xInputStream.is() )
205 0 : return uno::Reference< io::XInputStream >( static_cast< io::XInputStream* >( this ) );
206 :
207 0 : return uno::Reference< io::XInputStream >();
208 : }
209 :
210 0 : uno::Reference< io::XOutputStream > SAL_CALL OFSStreamContainer::getOutputStream()
211 : throw ( uno::RuntimeException, std::exception )
212 : {
213 0 : ::osl::MutexGuard aGuard( m_aMutex );
214 :
215 0 : if ( m_bDisposed )
216 0 : throw lang::DisposedException();
217 :
218 0 : if ( !m_xStream.is() )
219 0 : throw uno::RuntimeException();
220 :
221 0 : if ( m_xOutputStream.is() )
222 0 : return uno::Reference< io::XOutputStream >( static_cast< io::XOutputStream* >( this ) );
223 :
224 0 : return uno::Reference< io::XOutputStream >();
225 : }
226 :
227 : // XComponent
228 0 : void SAL_CALL OFSStreamContainer::dispose()
229 : throw ( uno::RuntimeException, std::exception )
230 : {
231 0 : ::osl::MutexGuard aGuard( m_aMutex );
232 :
233 0 : if ( m_bDisposed )
234 0 : throw lang::DisposedException();
235 :
236 0 : if ( !m_xStream.is() )
237 0 : throw uno::RuntimeException();
238 :
239 0 : if ( m_xInputStream.is() && !m_bInputClosed )
240 : {
241 0 : m_xInputStream->closeInput();
242 0 : m_bInputClosed = true;
243 : }
244 :
245 0 : if ( m_xOutputStream.is() && !m_bOutputClosed )
246 : {
247 0 : m_xOutputStream->closeOutput();
248 0 : m_bOutputClosed = true;
249 : }
250 :
251 0 : if ( m_pListenersContainer )
252 : {
253 0 : lang::EventObject aSource( static_cast< ::cppu::OWeakObject*>( this ) );
254 0 : m_pListenersContainer->disposeAndClear( aSource );
255 : }
256 :
257 0 : m_bDisposed = true;
258 0 : }
259 :
260 0 : void SAL_CALL OFSStreamContainer::addEventListener( const uno::Reference< lang::XEventListener >& xListener )
261 : throw ( uno::RuntimeException, std::exception )
262 : {
263 0 : ::osl::MutexGuard aGuard( m_aMutex );
264 :
265 0 : if ( m_bDisposed )
266 0 : throw lang::DisposedException();
267 :
268 0 : if ( !m_pListenersContainer )
269 0 : m_pListenersContainer = new ::cppu::OInterfaceContainerHelper( m_aMutex );
270 :
271 0 : m_pListenersContainer->addInterface( xListener );
272 0 : }
273 :
274 0 : void SAL_CALL OFSStreamContainer::removeEventListener( const uno::Reference< lang::XEventListener >& xListener )
275 : throw ( uno::RuntimeException, std::exception )
276 : {
277 0 : ::osl::MutexGuard aGuard( m_aMutex );
278 :
279 0 : if ( m_bDisposed )
280 0 : throw lang::DisposedException();
281 :
282 0 : if ( m_pListenersContainer )
283 0 : m_pListenersContainer->removeInterface( xListener );
284 0 : }
285 :
286 :
287 : // XSeekable
288 0 : void SAL_CALL OFSStreamContainer::seek( sal_Int64 location )
289 : throw ( lang::IllegalArgumentException,
290 : io::IOException,
291 : uno::RuntimeException, std::exception )
292 : {
293 0 : ::osl::MutexGuard aGuard( m_aMutex );
294 :
295 0 : if ( m_bDisposed )
296 0 : throw lang::DisposedException();
297 :
298 0 : if ( !m_xStream.is() || !m_xSeekable.is() )
299 0 : throw uno::RuntimeException();
300 :
301 0 : m_xSeekable->seek( location );
302 0 : }
303 :
304 0 : sal_Int64 SAL_CALL OFSStreamContainer::getPosition()
305 : throw ( io::IOException,
306 : uno::RuntimeException, std::exception )
307 : {
308 0 : ::osl::MutexGuard aGuard( m_aMutex );
309 :
310 0 : if ( m_bDisposed )
311 0 : throw lang::DisposedException();
312 :
313 0 : if ( !m_xStream.is() || !m_xSeekable.is() )
314 0 : throw uno::RuntimeException();
315 :
316 0 : return m_xSeekable->getPosition();
317 : }
318 :
319 0 : sal_Int64 SAL_CALL OFSStreamContainer::getLength()
320 : throw ( io::IOException,
321 : uno::RuntimeException, std::exception )
322 : {
323 0 : ::osl::MutexGuard aGuard( m_aMutex );
324 :
325 0 : if ( m_bDisposed )
326 0 : throw lang::DisposedException();
327 :
328 0 : if ( !m_xStream.is() || !m_xSeekable.is() )
329 0 : throw uno::RuntimeException();
330 :
331 0 : return m_xSeekable->getLength();
332 : }
333 :
334 :
335 : // XInputStream
336 0 : sal_Int32 SAL_CALL OFSStreamContainer::readBytes( uno::Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead )
337 : throw( io::NotConnectedException,
338 : io::BufferSizeExceededException,
339 : io::IOException,
340 : uno::RuntimeException, std::exception )
341 : {
342 0 : ::osl::MutexGuard aGuard( m_aMutex );
343 :
344 0 : if ( m_bDisposed )
345 0 : throw lang::DisposedException();
346 :
347 0 : if ( !m_xStream.is() || !m_xInputStream.is() )
348 0 : throw uno::RuntimeException();
349 :
350 0 : return m_xInputStream->readBytes( aData, nBytesToRead );
351 : }
352 :
353 0 : sal_Int32 SAL_CALL OFSStreamContainer::readSomeBytes( uno::Sequence< sal_Int8 >& aData, sal_Int32 nMaxBytesToRead )
354 : throw( io::NotConnectedException,
355 : io::BufferSizeExceededException,
356 : io::IOException,
357 : uno::RuntimeException, std::exception )
358 : {
359 0 : ::osl::MutexGuard aGuard( m_aMutex );
360 :
361 0 : if ( m_bDisposed )
362 0 : throw lang::DisposedException();
363 :
364 0 : if ( !m_xStream.is() || !m_xInputStream.is() )
365 0 : throw uno::RuntimeException();
366 :
367 0 : return m_xInputStream->readSomeBytes( aData, nMaxBytesToRead );
368 : }
369 :
370 0 : void SAL_CALL OFSStreamContainer::skipBytes( sal_Int32 nBytesToSkip )
371 : throw( io::NotConnectedException,
372 : io::BufferSizeExceededException,
373 : io::IOException,
374 : uno::RuntimeException, std::exception )
375 : {
376 0 : ::osl::MutexGuard aGuard( m_aMutex );
377 :
378 0 : if ( m_bDisposed )
379 0 : throw lang::DisposedException();
380 :
381 0 : if ( !m_xStream.is() || !m_xInputStream.is() )
382 0 : throw uno::RuntimeException();
383 :
384 0 : m_xInputStream->skipBytes( nBytesToSkip );
385 0 : }
386 :
387 0 : sal_Int32 SAL_CALL OFSStreamContainer::available()
388 : throw( io::NotConnectedException,
389 : io::IOException,
390 : uno::RuntimeException, std::exception )
391 : {
392 0 : ::osl::MutexGuard aGuard( m_aMutex );
393 :
394 0 : if ( m_bDisposed )
395 0 : throw lang::DisposedException();
396 :
397 0 : if ( !m_xStream.is() || !m_xInputStream.is() )
398 0 : throw uno::RuntimeException();
399 :
400 0 : return m_xInputStream->available();
401 : }
402 :
403 0 : void SAL_CALL OFSStreamContainer::closeInput()
404 : throw( io::NotConnectedException,
405 : io::IOException,
406 : uno::RuntimeException, std::exception )
407 : {
408 0 : ::osl::MutexGuard aGuard( m_aMutex );
409 :
410 0 : if ( m_bDisposed )
411 0 : throw lang::DisposedException();
412 :
413 0 : if ( !m_xStream.is() || !m_xInputStream.is() )
414 0 : throw uno::RuntimeException();
415 :
416 0 : if ( m_xInputStream.is() )
417 : {
418 0 : m_xInputStream->closeInput();
419 0 : m_bInputClosed = true;
420 : }
421 :
422 0 : if ( m_bOutputClosed )
423 0 : dispose();
424 0 : }
425 :
426 : // XOutputStream
427 0 : void SAL_CALL OFSStreamContainer::writeBytes( const uno::Sequence< sal_Int8 >& aData )
428 : throw ( io::NotConnectedException,
429 : io::BufferSizeExceededException,
430 : io::IOException,
431 : uno::RuntimeException, std::exception )
432 : {
433 0 : ::osl::MutexGuard aGuard( m_aMutex );
434 :
435 0 : if ( m_bDisposed )
436 0 : throw lang::DisposedException();
437 :
438 0 : if ( !m_xStream.is() || !m_xOutputStream.is() )
439 0 : throw uno::RuntimeException();
440 :
441 0 : return m_xOutputStream->writeBytes( aData );
442 : }
443 :
444 0 : void SAL_CALL OFSStreamContainer::flush()
445 : throw ( io::NotConnectedException,
446 : io::BufferSizeExceededException,
447 : io::IOException,
448 : uno::RuntimeException, std::exception )
449 : {
450 0 : ::osl::MutexGuard aGuard( m_aMutex );
451 :
452 0 : if ( m_bDisposed )
453 0 : throw lang::DisposedException();
454 :
455 0 : if ( !m_xStream.is() || !m_xOutputStream.is() )
456 0 : throw uno::RuntimeException();
457 :
458 0 : return m_xOutputStream->flush();
459 : }
460 :
461 0 : void SAL_CALL OFSStreamContainer::closeOutput()
462 : throw ( io::NotConnectedException,
463 : io::BufferSizeExceededException,
464 : io::IOException,
465 : uno::RuntimeException, std::exception )
466 : {
467 0 : ::osl::MutexGuard aGuard( m_aMutex );
468 :
469 0 : if ( m_bDisposed )
470 0 : throw lang::DisposedException();
471 :
472 0 : if ( !m_xStream.is() || !m_xOutputStream.is() )
473 0 : throw uno::RuntimeException();
474 :
475 0 : if ( m_xOutputStream.is() )
476 : {
477 0 : m_xOutputStream->closeOutput();
478 0 : m_bOutputClosed = true;
479 : }
480 :
481 0 : if ( m_bInputClosed )
482 0 : dispose();
483 0 : }
484 :
485 :
486 : // XTruncate
487 0 : void SAL_CALL OFSStreamContainer::truncate()
488 : throw ( io::IOException,
489 : uno::RuntimeException, std::exception )
490 : {
491 0 : ::osl::MutexGuard aGuard( m_aMutex );
492 :
493 0 : if ( m_bDisposed )
494 0 : throw lang::DisposedException();
495 :
496 0 : if ( !m_xStream.is() || !m_xTruncate.is() )
497 0 : throw uno::RuntimeException();
498 :
499 0 : m_xTruncate->truncate();
500 0 : }
501 :
502 :
503 : // XAsyncOutputMonitor
504 0 : void SAL_CALL OFSStreamContainer::waitForCompletion()
505 : throw ( io::IOException,
506 : uno::RuntimeException, std::exception )
507 : {
508 0 : ::osl::MutexGuard aGuard( m_aMutex );
509 :
510 0 : if ( m_bDisposed )
511 0 : throw lang::DisposedException();
512 :
513 0 : if ( !m_xStream.is() || !m_xAsyncOutputMonitor.is() )
514 0 : throw uno::RuntimeException();
515 :
516 0 : m_xAsyncOutputMonitor->waitForCompletion();
517 0 : }
518 :
519 :
520 :
521 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|