1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
21 #include "oinputstreamcontainer.hxx"
22 #include <cppuhelper/typeprovider.hxx>
23 #include <cppuhelper/queryinterface.hxx>
25 using namespace ::com::sun::star
;
27 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 )
33 m_bSeekable
= m_xSeekable
.is();
36 OFSInputStreamContainer::~OFSInputStreamContainer()
40 uno::Sequence
< uno::Type
> SAL_CALL
OFSInputStreamContainer::getTypes()
44 static cppu::OTypeCollection
aTypeCollection(cppu::UnoType
<io::XStream
>::get(),
45 cppu::UnoType
<io::XInputStream
>::get(),
46 cppu::UnoType
<io::XSeekable
>::get());
48 return aTypeCollection
.getTypes();
52 static cppu::OTypeCollection
aTypeCollection(cppu::UnoType
<io::XStream
>::get(),
53 cppu::UnoType
<io::XInputStream
>::get());
55 return aTypeCollection
.getTypes();
59 uno::Any SAL_CALL
OFSInputStreamContainer::queryInterface( const uno::Type
& rType
)
62 // Don't use mutex or guard in this method!!! Is a method of XInterface.
66 aReturn
= ::cppu::queryInterface( rType
,
67 static_cast< io::XStream
* >( this ),
68 static_cast< io::XInputStream
* >( this ),
69 static_cast< io::XSeekable
* >( this ) );
71 aReturn
= ::cppu::queryInterface( rType
,
72 static_cast< io::XStream
* >( this ),
73 static_cast< io::XInputStream
* >( this ) );
75 if ( aReturn
.hasValue() )
78 return ::cppu::OWeakObject::queryInterface( rType
) ;
81 void SAL_CALL
OFSInputStreamContainer::acquire()
84 ::cppu::OWeakObject::acquire();
87 void SAL_CALL
OFSInputStreamContainer::release()
90 ::cppu::OWeakObject::release();
93 sal_Int32 SAL_CALL
OFSInputStreamContainer::readBytes( uno::Sequence
< sal_Int8
>& aData
, sal_Int32 nBytesToRead
)
95 std::scoped_lock
aGuard( m_aMutex
);
98 throw lang::DisposedException();
100 if ( !m_xInputStream
.is() )
101 throw uno::RuntimeException();
103 return m_xInputStream
->readBytes( aData
, nBytesToRead
);
106 sal_Int32 SAL_CALL
OFSInputStreamContainer::readSomeBytes( uno::Sequence
< sal_Int8
>& aData
, sal_Int32 nMaxBytesToRead
)
108 std::scoped_lock
aGuard( m_aMutex
);
111 throw lang::DisposedException();
113 if ( !m_xInputStream
.is() )
114 throw uno::RuntimeException();
116 return m_xInputStream
->readSomeBytes( aData
, nMaxBytesToRead
);
119 void SAL_CALL
OFSInputStreamContainer::skipBytes( sal_Int32 nBytesToSkip
)
121 std::scoped_lock
aGuard( m_aMutex
);
124 throw lang::DisposedException();
126 if ( !m_xInputStream
.is() )
127 throw uno::RuntimeException();
129 m_xInputStream
->skipBytes( nBytesToSkip
);
132 sal_Int32 SAL_CALL
OFSInputStreamContainer::available( )
134 std::scoped_lock
aGuard( m_aMutex
);
137 throw lang::DisposedException();
139 if ( !m_xInputStream
.is() )
140 throw uno::RuntimeException();
142 return m_xInputStream
->available();
145 void SAL_CALL
OFSInputStreamContainer::closeInput( )
148 std::scoped_lock
aGuard( m_aMutex
);
151 throw lang::DisposedException();
153 if ( !m_xInputStream
.is() )
154 throw uno::RuntimeException();
159 uno::Reference
< io::XInputStream
> SAL_CALL
OFSInputStreamContainer::getInputStream()
161 std::scoped_lock
aGuard( m_aMutex
);
164 throw lang::DisposedException();
166 if ( !m_xInputStream
.is() )
167 return uno::Reference
< io::XInputStream
>();
172 uno::Reference
< io::XOutputStream
> SAL_CALL
OFSInputStreamContainer::getOutputStream()
174 std::scoped_lock
aGuard( m_aMutex
);
177 throw lang::DisposedException();
179 return uno::Reference
< io::XOutputStream
>();
182 void SAL_CALL
OFSInputStreamContainer::seek( sal_Int64 location
)
184 std::scoped_lock
aGuard( m_aMutex
);
187 throw lang::DisposedException();
189 if ( !m_xSeekable
.is() )
190 throw uno::RuntimeException();
192 m_xSeekable
->seek( location
);
195 sal_Int64 SAL_CALL
OFSInputStreamContainer::getPosition()
197 std::scoped_lock
aGuard( m_aMutex
);
200 throw lang::DisposedException();
202 if ( !m_xSeekable
.is() )
203 throw uno::RuntimeException();
205 return m_xSeekable
->getPosition();
208 sal_Int64 SAL_CALL
OFSInputStreamContainer::getLength()
210 std::scoped_lock
aGuard( m_aMutex
);
213 throw lang::DisposedException();
215 if ( !m_xSeekable
.is() )
216 throw uno::RuntimeException();
218 return m_xSeekable
->getLength();
221 void SAL_CALL
OFSInputStreamContainer::dispose( )
223 std::unique_lock
aGuard( m_aMutex
);
228 if ( !m_xInputStream
.is() )
229 throw uno::RuntimeException();
231 m_xInputStream
->closeInput();
233 lang::EventObject
aSource( getXWeak() );
234 m_aListenersContainer
.disposeAndClear( aGuard
, aSource
);
239 void SAL_CALL
OFSInputStreamContainer::addEventListener( const uno::Reference
< lang::XEventListener
>& xListener
)
241 std::unique_lock
aGuard( m_aMutex
);
244 throw lang::DisposedException();
246 m_aListenersContainer
.addInterface( aGuard
, xListener
);
249 void SAL_CALL
OFSInputStreamContainer::removeEventListener( const uno::Reference
< lang::XEventListener
>& xListener
)
251 std::unique_lock
aGuard( m_aMutex
);
254 throw lang::DisposedException();
256 m_aListenersContainer
.removeInterface( aGuard
, xListener
);
260 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */