1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: oinputstreamcontainer.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_svtools.hxx"
34 #include "oinputstreamcontainer.hxx"
35 #include <cppuhelper/typeprovider.hxx>
37 using namespace ::com::sun::star
;
39 //-----------------------------------------------
40 OFSInputStreamContainer::OFSInputStreamContainer( const uno::Reference
< io::XInputStream
>& xStream
)
41 : m_xInputStream( xStream
)
42 , m_xSeekable( xStream
, uno::UNO_QUERY
)
43 , m_bSeekable( sal_False
)
44 , m_bDisposed( sal_False
)
45 , m_pListenersContainer( NULL
)
47 m_bSeekable
= m_xSeekable
.is();
50 //-----------------------------------------------
51 OFSInputStreamContainer::~OFSInputStreamContainer()
53 if ( m_pListenersContainer
)
55 delete m_pListenersContainer
;
56 m_pListenersContainer
= NULL
;
60 //-----------------------------------------------
61 uno::Sequence
< uno::Type
> SAL_CALL
OFSInputStreamContainer::getTypes()
62 throw ( uno::RuntimeException
)
64 static ::cppu::OTypeCollection
* pTypeCollection
= NULL
;
66 if ( pTypeCollection
== NULL
)
68 ::osl::MutexGuard
aGuard( m_aMutex
) ;
70 if ( pTypeCollection
== NULL
)
74 static ::cppu::OTypeCollection
aTypeCollection(
75 ::getCppuType(( const uno::Reference
< io::XStream
>* )NULL
),
76 ::getCppuType(( const uno::Reference
< io::XInputStream
>* )NULL
),
77 ::getCppuType(( const uno::Reference
< io::XSeekable
>* )NULL
) );
79 pTypeCollection
= &aTypeCollection
;
83 static ::cppu::OTypeCollection
aTypeCollection(
84 ::getCppuType(( const uno::Reference
< io::XStream
>* )NULL
),
85 ::getCppuType(( const uno::Reference
< io::XInputStream
>* )NULL
) );
87 pTypeCollection
= &aTypeCollection
;
92 return pTypeCollection
->getTypes() ;
96 //-----------------------------------------------
97 uno::Any SAL_CALL
OFSInputStreamContainer::queryInterface( const uno::Type
& rType
)
98 throw( uno::RuntimeException
)
101 // Don't use mutex or guard in this method!!! Is a method of XInterface.
105 aReturn
= uno::Any( ::cppu::queryInterface( rType
,
106 static_cast< io::XStream
* >( this ),
107 static_cast< io::XInputStream
* >( this ),
108 static_cast< io::XSeekable
* >( this ) ) );
110 aReturn
= uno::Any( ::cppu::queryInterface( rType
,
111 static_cast< io::XStream
* >( this ),
112 static_cast< io::XInputStream
* >( this ) ) );
114 if ( aReturn
.hasValue() == sal_True
)
117 return ::cppu::OWeakObject::queryInterface( rType
) ;
120 //-----------------------------------------------
121 void SAL_CALL
OFSInputStreamContainer::acquire()
124 ::cppu::OWeakObject::acquire();
127 //-----------------------------------------------
128 void SAL_CALL
OFSInputStreamContainer::release()
131 ::cppu::OWeakObject::release();
134 //-----------------------------------------------
135 sal_Int32 SAL_CALL
OFSInputStreamContainer::readBytes( uno::Sequence
< sal_Int8
>& aData
, sal_Int32 nBytesToRead
)
136 throw ( io::NotConnectedException
,
137 io::BufferSizeExceededException
,
139 uno::RuntimeException
)
141 ::osl::MutexGuard
aGuard( m_aMutex
);
144 throw lang::DisposedException();
146 if ( !m_xInputStream
.is() )
147 throw uno::RuntimeException();
149 return m_xInputStream
->readBytes( aData
, nBytesToRead
);
152 //-----------------------------------------------
153 sal_Int32 SAL_CALL
OFSInputStreamContainer::readSomeBytes( uno::Sequence
< sal_Int8
>& aData
, sal_Int32 nMaxBytesToRead
)
154 throw ( io::NotConnectedException
,
155 io::BufferSizeExceededException
,
157 uno::RuntimeException
)
159 ::osl::MutexGuard
aGuard( m_aMutex
);
162 throw lang::DisposedException();
164 if ( !m_xInputStream
.is() )
165 throw uno::RuntimeException();
167 return m_xInputStream
->readSomeBytes( aData
, nMaxBytesToRead
);
170 //-----------------------------------------------
171 void SAL_CALL
OFSInputStreamContainer::skipBytes( sal_Int32 nBytesToSkip
)
172 throw ( io::NotConnectedException
,
173 io::BufferSizeExceededException
,
175 uno::RuntimeException
)
177 ::osl::MutexGuard
aGuard( m_aMutex
);
180 throw lang::DisposedException();
182 if ( !m_xInputStream
.is() )
183 throw uno::RuntimeException();
185 m_xInputStream
->skipBytes( nBytesToSkip
);
188 //-----------------------------------------------
189 sal_Int32 SAL_CALL
OFSInputStreamContainer::available( )
190 throw ( io::NotConnectedException
,
192 uno::RuntimeException
)
194 ::osl::MutexGuard
aGuard( m_aMutex
);
197 throw lang::DisposedException();
199 if ( !m_xInputStream
.is() )
200 throw uno::RuntimeException();
202 return m_xInputStream
->available();
205 //-----------------------------------------------
206 void SAL_CALL
OFSInputStreamContainer::closeInput( )
207 throw ( io::NotConnectedException
,
209 uno::RuntimeException
)
211 ::osl::MutexGuard
aGuard( m_aMutex
);
214 throw lang::DisposedException();
216 if ( !m_xInputStream
.is() )
217 throw uno::RuntimeException();
222 //-----------------------------------------------
223 uno::Reference
< io::XInputStream
> SAL_CALL
OFSInputStreamContainer::getInputStream()
224 throw ( uno::RuntimeException
)
226 ::osl::MutexGuard
aGuard( m_aMutex
);
229 throw lang::DisposedException();
231 if ( !m_xInputStream
.is() )
232 return uno::Reference
< io::XInputStream
>();
234 return uno::Reference
< io::XInputStream
>( static_cast< io::XInputStream
* >( this ), uno::UNO_QUERY
);
237 //-----------------------------------------------
238 uno::Reference
< io::XOutputStream
> SAL_CALL
OFSInputStreamContainer::getOutputStream()
239 throw ( uno::RuntimeException
)
241 ::osl::MutexGuard
aGuard( m_aMutex
);
244 throw lang::DisposedException();
246 return uno::Reference
< io::XOutputStream
>();
249 //-----------------------------------------------
250 void SAL_CALL
OFSInputStreamContainer::seek( sal_Int64 location
)
251 throw ( lang::IllegalArgumentException
,
253 uno::RuntimeException
)
255 ::osl::MutexGuard
aGuard( m_aMutex
);
258 throw lang::DisposedException();
260 if ( !m_xSeekable
.is() )
261 throw uno::RuntimeException();
263 m_xSeekable
->seek( location
);
266 //-----------------------------------------------
267 sal_Int64 SAL_CALL
OFSInputStreamContainer::getPosition()
268 throw ( io::IOException
,
269 uno::RuntimeException
)
271 ::osl::MutexGuard
aGuard( m_aMutex
);
274 throw lang::DisposedException();
276 if ( !m_xSeekable
.is() )
277 throw uno::RuntimeException();
279 return m_xSeekable
->getPosition();
282 //-----------------------------------------------
283 sal_Int64 SAL_CALL
OFSInputStreamContainer::getLength()
284 throw ( io::IOException
,
285 uno::RuntimeException
)
287 ::osl::MutexGuard
aGuard( m_aMutex
);
290 throw lang::DisposedException();
292 if ( !m_xSeekable
.is() )
293 throw uno::RuntimeException();
295 return m_xSeekable
->getLength();
298 //-----------------------------------------------
299 void SAL_CALL
OFSInputStreamContainer::dispose( )
300 throw ( uno::RuntimeException
)
302 ::osl::MutexGuard
aGuard( m_aMutex
);
305 throw lang::DisposedException();
307 if ( !m_xInputStream
.is() )
308 throw uno::RuntimeException();
310 m_xInputStream
->closeInput();
312 if ( m_pListenersContainer
)
314 lang::EventObject
aSource( static_cast< ::cppu::OWeakObject
*>( this ) );
315 m_pListenersContainer
->disposeAndClear( aSource
);
318 m_bDisposed
= sal_True
;
321 //-----------------------------------------------
322 void SAL_CALL
OFSInputStreamContainer::addEventListener( const uno::Reference
< lang::XEventListener
>& xListener
)
323 throw ( uno::RuntimeException
)
325 ::osl::MutexGuard
aGuard( m_aMutex
);
328 throw lang::DisposedException();
330 if ( !m_pListenersContainer
)
331 m_pListenersContainer
= new ::cppu::OInterfaceContainerHelper( m_aMutex
);
333 m_pListenersContainer
->addInterface( xListener
);
336 //-----------------------------------------------
337 void SAL_CALL
OFSInputStreamContainer::removeEventListener( const uno::Reference
< lang::XEventListener
>& xListener
)
338 throw ( uno::RuntimeException
)
340 ::osl::MutexGuard
aGuard( m_aMutex
);
343 throw lang::DisposedException();
345 if ( m_pListenersContainer
)
346 m_pListenersContainer
->removeInterface( xListener
);