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 :
21 : #include "oinputstreamcontainer.hxx"
22 : #include <cppuhelper/typeprovider.hxx>
23 :
24 : using namespace ::com::sun::star;
25 :
26 428 : OFSInputStreamContainer::OFSInputStreamContainer( const uno::Reference< io::XInputStream >& xStream )
27 : : m_xInputStream( xStream )
28 : , m_xSeekable( xStream, uno::UNO_QUERY )
29 : , m_bSeekable( false )
30 : , m_bDisposed( false )
31 428 : , m_pListenersContainer( NULL )
32 : {
33 428 : m_bSeekable = m_xSeekable.is();
34 428 : }
35 :
36 1284 : OFSInputStreamContainer::~OFSInputStreamContainer()
37 : {
38 428 : if ( m_pListenersContainer )
39 : {
40 0 : delete m_pListenersContainer;
41 0 : m_pListenersContainer = NULL;
42 : }
43 856 : }
44 :
45 0 : uno::Sequence< uno::Type > SAL_CALL OFSInputStreamContainer::getTypes()
46 : throw ( uno::RuntimeException, std::exception )
47 : {
48 : static ::cppu::OTypeCollection* pTypeCollection = NULL ;
49 :
50 0 : if ( pTypeCollection == NULL )
51 : {
52 0 : ::osl::MutexGuard aGuard( m_aMutex ) ;
53 :
54 0 : if ( pTypeCollection == NULL )
55 : {
56 0 : if ( m_bSeekable )
57 : {
58 : static ::cppu::OTypeCollection aTypeCollection(
59 0 : cppu::UnoType<io::XStream>::get(),
60 0 : cppu::UnoType<io::XInputStream>::get(),
61 0 : cppu::UnoType<io::XSeekable>::get());
62 :
63 0 : pTypeCollection = &aTypeCollection ;
64 : }
65 : else
66 : {
67 : static ::cppu::OTypeCollection aTypeCollection(
68 0 : cppu::UnoType<io::XStream>::get(),
69 0 : cppu::UnoType<io::XInputStream>::get());
70 :
71 0 : pTypeCollection = &aTypeCollection ;
72 : }
73 0 : }
74 : }
75 :
76 0 : return pTypeCollection->getTypes() ;
77 :
78 : }
79 :
80 428 : uno::Any SAL_CALL OFSInputStreamContainer::queryInterface( const uno::Type& rType )
81 : throw( uno::RuntimeException, std::exception )
82 : {
83 : // Attention:
84 : // Don't use mutex or guard in this method!!! Is a method of XInterface.
85 :
86 428 : uno::Any aReturn;
87 428 : if ( m_bSeekable )
88 856 : aReturn = uno::Any( ::cppu::queryInterface( rType,
89 : static_cast< io::XStream* >( this ),
90 : static_cast< io::XInputStream* >( this ),
91 428 : static_cast< io::XSeekable* >( this ) ) );
92 : else
93 0 : aReturn = uno::Any( ::cppu::queryInterface( rType,
94 : static_cast< io::XStream* >( this ),
95 0 : static_cast< io::XInputStream* >( this ) ) );
96 :
97 428 : if ( aReturn.hasValue() )
98 428 : return aReturn ;
99 :
100 0 : return ::cppu::OWeakObject::queryInterface( rType ) ;
101 : }
102 :
103 3852 : void SAL_CALL OFSInputStreamContainer::acquire()
104 : throw()
105 : {
106 3852 : ::cppu::OWeakObject::acquire();
107 3852 : }
108 :
109 3852 : void SAL_CALL OFSInputStreamContainer::release()
110 : throw()
111 : {
112 3852 : ::cppu::OWeakObject::release();
113 3852 : }
114 :
115 0 : sal_Int32 SAL_CALL OFSInputStreamContainer::readBytes( uno::Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead )
116 : throw ( io::NotConnectedException,
117 : io::BufferSizeExceededException,
118 : io::IOException,
119 : uno::RuntimeException, std::exception )
120 : {
121 0 : ::osl::MutexGuard aGuard( m_aMutex );
122 :
123 0 : if ( m_bDisposed )
124 0 : throw lang::DisposedException();
125 :
126 0 : if ( !m_xInputStream.is() )
127 0 : throw uno::RuntimeException();
128 :
129 0 : return m_xInputStream->readBytes( aData, nBytesToRead );
130 : }
131 :
132 930 : sal_Int32 SAL_CALL OFSInputStreamContainer::readSomeBytes( uno::Sequence< sal_Int8 >& aData, sal_Int32 nMaxBytesToRead )
133 : throw ( io::NotConnectedException,
134 : io::BufferSizeExceededException,
135 : io::IOException,
136 : uno::RuntimeException, std::exception )
137 : {
138 930 : ::osl::MutexGuard aGuard( m_aMutex );
139 :
140 930 : if ( m_bDisposed )
141 0 : throw lang::DisposedException();
142 :
143 930 : if ( !m_xInputStream.is() )
144 0 : throw uno::RuntimeException();
145 :
146 930 : return m_xInputStream->readSomeBytes( aData, nMaxBytesToRead );
147 : }
148 :
149 0 : void SAL_CALL OFSInputStreamContainer::skipBytes( sal_Int32 nBytesToSkip )
150 : throw ( io::NotConnectedException,
151 : io::BufferSizeExceededException,
152 : io::IOException,
153 : uno::RuntimeException, std::exception )
154 : {
155 0 : ::osl::MutexGuard aGuard( m_aMutex );
156 :
157 0 : if ( m_bDisposed )
158 0 : throw lang::DisposedException();
159 :
160 0 : if ( !m_xInputStream.is() )
161 0 : throw uno::RuntimeException();
162 :
163 0 : m_xInputStream->skipBytes( nBytesToSkip );
164 0 : }
165 :
166 0 : sal_Int32 SAL_CALL OFSInputStreamContainer::available( )
167 : throw ( io::NotConnectedException,
168 : io::IOException,
169 : uno::RuntimeException, std::exception )
170 : {
171 0 : ::osl::MutexGuard aGuard( m_aMutex );
172 :
173 0 : if ( m_bDisposed )
174 0 : throw lang::DisposedException();
175 :
176 0 : if ( !m_xInputStream.is() )
177 0 : throw uno::RuntimeException();
178 :
179 0 : return m_xInputStream->available();
180 : }
181 :
182 0 : void SAL_CALL OFSInputStreamContainer::closeInput( )
183 : throw ( io::NotConnectedException,
184 : io::IOException,
185 : uno::RuntimeException, std::exception )
186 : {
187 0 : ::osl::MutexGuard aGuard( m_aMutex );
188 :
189 0 : if ( m_bDisposed )
190 0 : throw lang::DisposedException();
191 :
192 0 : if ( !m_xInputStream.is() )
193 0 : throw uno::RuntimeException();
194 :
195 0 : dispose();
196 0 : }
197 :
198 428 : uno::Reference< io::XInputStream > SAL_CALL OFSInputStreamContainer::getInputStream()
199 : throw ( uno::RuntimeException, std::exception )
200 : {
201 428 : ::osl::MutexGuard aGuard( m_aMutex );
202 :
203 428 : if ( m_bDisposed )
204 0 : throw lang::DisposedException();
205 :
206 428 : if ( !m_xInputStream.is() )
207 0 : return uno::Reference< io::XInputStream >();
208 :
209 428 : return uno::Reference< io::XInputStream >( static_cast< io::XInputStream* >( this ), uno::UNO_QUERY );
210 : }
211 :
212 0 : uno::Reference< io::XOutputStream > SAL_CALL OFSInputStreamContainer::getOutputStream()
213 : throw ( uno::RuntimeException, std::exception )
214 : {
215 0 : ::osl::MutexGuard aGuard( m_aMutex );
216 :
217 0 : if ( m_bDisposed )
218 0 : throw lang::DisposedException();
219 :
220 0 : return uno::Reference< io::XOutputStream >();
221 : }
222 :
223 0 : void SAL_CALL OFSInputStreamContainer::seek( sal_Int64 location )
224 : throw ( lang::IllegalArgumentException,
225 : io::IOException,
226 : uno::RuntimeException, std::exception )
227 : {
228 0 : ::osl::MutexGuard aGuard( m_aMutex );
229 :
230 0 : if ( m_bDisposed )
231 0 : throw lang::DisposedException();
232 :
233 0 : if ( !m_xSeekable.is() )
234 0 : throw uno::RuntimeException();
235 :
236 0 : m_xSeekable->seek( location );
237 0 : }
238 :
239 0 : sal_Int64 SAL_CALL OFSInputStreamContainer::getPosition()
240 : throw ( io::IOException,
241 : uno::RuntimeException, std::exception)
242 : {
243 0 : ::osl::MutexGuard aGuard( m_aMutex );
244 :
245 0 : if ( m_bDisposed )
246 0 : throw lang::DisposedException();
247 :
248 0 : if ( !m_xSeekable.is() )
249 0 : throw uno::RuntimeException();
250 :
251 0 : return m_xSeekable->getPosition();
252 : }
253 :
254 0 : sal_Int64 SAL_CALL OFSInputStreamContainer::getLength()
255 : throw ( io::IOException,
256 : uno::RuntimeException, std::exception )
257 : {
258 0 : ::osl::MutexGuard aGuard( m_aMutex );
259 :
260 0 : if ( m_bDisposed )
261 0 : throw lang::DisposedException();
262 :
263 0 : if ( !m_xSeekable.is() )
264 0 : throw uno::RuntimeException();
265 :
266 0 : return m_xSeekable->getLength();
267 : }
268 :
269 0 : void SAL_CALL OFSInputStreamContainer::dispose( )
270 : throw ( uno::RuntimeException, std::exception )
271 : {
272 0 : ::osl::MutexGuard aGuard( m_aMutex );
273 :
274 0 : if ( m_bDisposed )
275 0 : throw lang::DisposedException();
276 :
277 0 : if ( !m_xInputStream.is() )
278 0 : throw uno::RuntimeException();
279 :
280 0 : m_xInputStream->closeInput();
281 :
282 0 : if ( m_pListenersContainer )
283 : {
284 0 : lang::EventObject aSource( static_cast< ::cppu::OWeakObject*>( this ) );
285 0 : m_pListenersContainer->disposeAndClear( aSource );
286 : }
287 :
288 0 : m_bDisposed = true;
289 0 : }
290 :
291 0 : void SAL_CALL OFSInputStreamContainer::addEventListener( const uno::Reference< lang::XEventListener >& xListener )
292 : throw ( uno::RuntimeException, std::exception )
293 : {
294 0 : ::osl::MutexGuard aGuard( m_aMutex );
295 :
296 0 : if ( m_bDisposed )
297 0 : throw lang::DisposedException();
298 :
299 0 : if ( !m_pListenersContainer )
300 0 : m_pListenersContainer = new ::cppu::OInterfaceContainerHelper( m_aMutex );
301 :
302 0 : m_pListenersContainer->addInterface( xListener );
303 0 : }
304 :
305 0 : void SAL_CALL OFSInputStreamContainer::removeEventListener( const uno::Reference< lang::XEventListener >& xListener )
306 : throw ( 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_pListenersContainer )
314 0 : m_pListenersContainer->removeInterface( xListener );
315 0 : }
316 :
317 :
318 :
319 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|