xref: /trunk/main/svl/source/fsstor/oinputstreamcontainer.cxx (revision 91144cd0085a7583d2099b982122deb2184ab956)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_svl.hxx"
26 
27 #include "oinputstreamcontainer.hxx"
28 #include <cppuhelper/typeprovider.hxx>
29 
30 using namespace ::com::sun::star;
31 
32 //-----------------------------------------------
OFSInputStreamContainer(const uno::Reference<io::XInputStream> & xStream)33 OFSInputStreamContainer::OFSInputStreamContainer( const uno::Reference< io::XInputStream >& xStream )
34 : m_xInputStream( xStream )
35 , m_xSeekable( xStream, uno::UNO_QUERY )
36 , m_bSeekable( sal_False )
37 , m_bDisposed( sal_False )
38 , m_pListenersContainer( NULL )
39 {
40     m_bSeekable = m_xSeekable.is();
41 }
42 
43 //-----------------------------------------------
~OFSInputStreamContainer()44 OFSInputStreamContainer::~OFSInputStreamContainer()
45 {
46     if ( m_pListenersContainer )
47     {
48         delete m_pListenersContainer;
49         m_pListenersContainer = NULL;
50     }
51 }
52 
53 //-----------------------------------------------
getTypes()54 uno::Sequence< uno::Type > SAL_CALL OFSInputStreamContainer::getTypes()
55 {
56     static ::cppu::OTypeCollection* pTypeCollection = NULL ;
57 
58     if ( pTypeCollection == NULL )
59     {
60         ::osl::MutexGuard aGuard( m_aMutex ) ;
61 
62         if ( pTypeCollection == NULL )
63         {
64             if ( m_bSeekable )
65             {
66                 static ::cppu::OTypeCollection aTypeCollection(
67                         ::getCppuType(( const uno::Reference< io::XStream >* )NULL ),
68                         ::getCppuType(( const uno::Reference< io::XInputStream >* )NULL ),
69                         ::getCppuType(( const uno::Reference< io::XSeekable >* )NULL ) );
70 
71                 pTypeCollection = &aTypeCollection ;
72             }
73             else
74             {
75                 static ::cppu::OTypeCollection aTypeCollection(
76                         ::getCppuType(( const uno::Reference< io::XStream >* )NULL ),
77                         ::getCppuType(( const uno::Reference< io::XInputStream >* )NULL ) );
78 
79                 pTypeCollection = &aTypeCollection ;
80             }
81         }
82     }
83 
84     return pTypeCollection->getTypes() ;
85 
86 }
87 
88 //-----------------------------------------------
queryInterface(const uno::Type & rType)89 uno::Any SAL_CALL OFSInputStreamContainer::queryInterface( const uno::Type& rType )
90 {
91     // Attention:
92     //  Don't use mutex or guard in this method!!! Is a method of XInterface.
93 
94     uno::Any aReturn;
95     if ( m_bSeekable )
96         aReturn = uno::Any( ::cppu::queryInterface( rType,
97                                         static_cast< io::XStream* >( this ),
98                                         static_cast< io::XInputStream* >( this ),
99                                         static_cast< io::XSeekable* >( this ) ) );
100     else
101         aReturn = uno::Any( ::cppu::queryInterface( rType,
102                                         static_cast< io::XStream* >( this ),
103                                         static_cast< io::XInputStream* >( this ) ) );
104 
105     if ( aReturn.hasValue() == sal_True )
106         return aReturn ;
107 
108     return ::cppu::OWeakObject::queryInterface( rType ) ;
109 }
110 
111 //-----------------------------------------------
acquire()112 void SAL_CALL OFSInputStreamContainer::acquire()
113         throw()
114 {
115     ::cppu::OWeakObject::acquire();
116 }
117 
118 //-----------------------------------------------
release()119 void SAL_CALL OFSInputStreamContainer::release()
120         throw()
121 {
122     ::cppu::OWeakObject::release();
123 }
124 
125 //-----------------------------------------------
readBytes(uno::Sequence<sal_Int8> & aData,sal_Int32 nBytesToRead)126 sal_Int32 SAL_CALL OFSInputStreamContainer::readBytes( uno::Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead )
127 {
128     ::osl::MutexGuard aGuard( m_aMutex );
129 
130     if ( m_bDisposed )
131         throw lang::DisposedException();
132 
133     if ( !m_xInputStream.is() )
134         throw uno::RuntimeException();
135 
136     return m_xInputStream->readBytes( aData, nBytesToRead );
137 }
138 
139 //-----------------------------------------------
readSomeBytes(uno::Sequence<sal_Int8> & aData,sal_Int32 nMaxBytesToRead)140 sal_Int32 SAL_CALL OFSInputStreamContainer::readSomeBytes( uno::Sequence< sal_Int8 >& aData, sal_Int32 nMaxBytesToRead )
141 {
142     ::osl::MutexGuard aGuard( m_aMutex );
143 
144     if ( m_bDisposed )
145         throw lang::DisposedException();
146 
147     if ( !m_xInputStream.is() )
148         throw uno::RuntimeException();
149 
150     return m_xInputStream->readSomeBytes( aData, nMaxBytesToRead );
151 }
152 
153 //-----------------------------------------------
skipBytes(sal_Int32 nBytesToSkip)154 void SAL_CALL OFSInputStreamContainer::skipBytes( sal_Int32 nBytesToSkip )
155 {
156     ::osl::MutexGuard aGuard( m_aMutex );
157 
158     if ( m_bDisposed )
159         throw lang::DisposedException();
160 
161     if ( !m_xInputStream.is() )
162         throw uno::RuntimeException();
163 
164     m_xInputStream->skipBytes( nBytesToSkip );
165 }
166 
167 //-----------------------------------------------
available()168 sal_Int32 SAL_CALL OFSInputStreamContainer::available(  )
169 {
170     ::osl::MutexGuard aGuard( m_aMutex );
171 
172     if ( m_bDisposed )
173         throw lang::DisposedException();
174 
175     if ( !m_xInputStream.is() )
176         throw uno::RuntimeException();
177 
178     return m_xInputStream->available();
179 }
180 
181 //-----------------------------------------------
closeInput()182 void SAL_CALL OFSInputStreamContainer::closeInput(  )
183 {
184     ::osl::MutexGuard aGuard( m_aMutex );
185 
186     if ( m_bDisposed )
187         throw lang::DisposedException();
188 
189     if ( !m_xInputStream.is() )
190         throw uno::RuntimeException();
191 
192     dispose();
193 }
194 
195 //-----------------------------------------------
getInputStream()196 uno::Reference< io::XInputStream > SAL_CALL OFSInputStreamContainer::getInputStream()
197 {
198     ::osl::MutexGuard aGuard( m_aMutex );
199 
200     if ( m_bDisposed )
201         throw lang::DisposedException();
202 
203     if ( !m_xInputStream.is() )
204         return uno::Reference< io::XInputStream >();
205 
206     return uno::Reference< io::XInputStream >( static_cast< io::XInputStream* >( this ), uno::UNO_QUERY );
207 }
208 
209 //-----------------------------------------------
getOutputStream()210 uno::Reference< io::XOutputStream > SAL_CALL OFSInputStreamContainer::getOutputStream()
211 {
212     ::osl::MutexGuard aGuard( m_aMutex );
213 
214     if ( m_bDisposed )
215         throw lang::DisposedException();
216 
217     return uno::Reference< io::XOutputStream >();
218 }
219 
220 //-----------------------------------------------
seek(sal_Int64 location)221 void SAL_CALL OFSInputStreamContainer::seek( sal_Int64 location )
222 {
223     ::osl::MutexGuard aGuard( m_aMutex );
224 
225     if ( m_bDisposed )
226         throw lang::DisposedException();
227 
228     if ( !m_xSeekable.is() )
229         throw uno::RuntimeException();
230 
231     m_xSeekable->seek( location );
232 }
233 
234 //-----------------------------------------------
getPosition()235 sal_Int64 SAL_CALL OFSInputStreamContainer::getPosition()
236 {
237     ::osl::MutexGuard aGuard( m_aMutex );
238 
239     if ( m_bDisposed )
240         throw lang::DisposedException();
241 
242     if ( !m_xSeekable.is() )
243         throw uno::RuntimeException();
244 
245     return m_xSeekable->getPosition();
246 }
247 
248 //-----------------------------------------------
getLength()249 sal_Int64 SAL_CALL OFSInputStreamContainer::getLength()
250 {
251     ::osl::MutexGuard aGuard( m_aMutex );
252 
253     if ( m_bDisposed )
254         throw lang::DisposedException();
255 
256     if ( !m_xSeekable.is() )
257         throw uno::RuntimeException();
258 
259     return m_xSeekable->getLength();
260 }
261 
262 //-----------------------------------------------
dispose()263 void SAL_CALL OFSInputStreamContainer::dispose(  )
264 {
265     ::osl::MutexGuard aGuard( m_aMutex );
266 
267     if ( m_bDisposed )
268         throw lang::DisposedException();
269 
270     if ( !m_xInputStream.is() )
271         throw uno::RuntimeException();
272 
273     m_xInputStream->closeInput();
274 
275     if ( m_pListenersContainer )
276     {
277         lang::EventObject aSource( static_cast< ::cppu::OWeakObject*>( this ) );
278         m_pListenersContainer->disposeAndClear( aSource );
279     }
280 
281     m_bDisposed = sal_True;
282 }
283 
284 //-----------------------------------------------
addEventListener(const uno::Reference<lang::XEventListener> & xListener)285 void SAL_CALL OFSInputStreamContainer::addEventListener( const uno::Reference< lang::XEventListener >& xListener )
286 {
287     ::osl::MutexGuard aGuard( m_aMutex );
288 
289     if ( m_bDisposed )
290         throw lang::DisposedException();
291 
292     if ( !m_pListenersContainer )
293         m_pListenersContainer = new ::cppu::OInterfaceContainerHelper( m_aMutex );
294 
295     m_pListenersContainer->addInterface( xListener );
296 }
297 
298 //-----------------------------------------------
removeEventListener(const uno::Reference<lang::XEventListener> & xListener)299 void SAL_CALL OFSInputStreamContainer::removeEventListener( const uno::Reference< lang::XEventListener >& xListener )
300 {
301     ::osl::MutexGuard aGuard( m_aMutex );
302 
303     if ( m_bDisposed )
304         throw lang::DisposedException();
305 
306     if ( m_pListenersContainer )
307         m_pListenersContainer->removeInterface( xListener );
308 }
309