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