1dde7d3faSAndrew Rist /**************************************************************
2*d75c37aeSmseidel  *
3dde7d3faSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4dde7d3faSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5dde7d3faSAndrew Rist  * distributed with this work for additional information
6dde7d3faSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7dde7d3faSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8dde7d3faSAndrew Rist  * "License"); you may not use this file except in compliance
9dde7d3faSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*d75c37aeSmseidel  *
11dde7d3faSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*d75c37aeSmseidel  *
13dde7d3faSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14dde7d3faSAndrew Rist  * software distributed under the License is distributed on an
15dde7d3faSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16dde7d3faSAndrew Rist  * KIND, either express or implied.  See the License for the
17dde7d3faSAndrew Rist  * specific language governing permissions and limitations
18dde7d3faSAndrew Rist  * under the License.
19*d75c37aeSmseidel  *
20dde7d3faSAndrew Rist  *************************************************************/
21dde7d3faSAndrew Rist 
22dde7d3faSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER( update_precomp.py ): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_comphelper.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include "comphelper_module.hxx"
28cdf0e10cSrcweir 
29cdf0e10cSrcweir #include <sal/config.h>
30cdf0e10cSrcweir #include <osl/mutex.hxx>
31cdf0e10cSrcweir #include <cppuhelper/factory.hxx>
32cdf0e10cSrcweir #include <cppuhelper/implementationentry.hxx>
33cdf0e10cSrcweir #include <cppuhelper/implbase3.hxx>
34cdf0e10cSrcweir #include <comphelper/seqstream.hxx>
35cdf0e10cSrcweir #include <com/sun/star/lang/XServiceInfo.hpp>
36cdf0e10cSrcweir #include <com/sun/star/io/XSeekableInputStream.hpp>
37cdf0e10cSrcweir #include <com/sun/star/lang/XInitialization.hpp>
38cdf0e10cSrcweir #include <com/sun/star/frame/DoubleInitializationException.hpp>
39cdf0e10cSrcweir #include <com/sun/star/uno/XComponentContext.hpp>
40cdf0e10cSrcweir 
41cdf0e10cSrcweir 
42cdf0e10cSrcweir using namespace ::com::sun::star;
43cdf0e10cSrcweir 
44cdf0e10cSrcweir namespace {
45cdf0e10cSrcweir 
46cdf0e10cSrcweir class SequenceInputStreamService:
47*d75c37aeSmseidel 	public ::cppu::WeakImplHelper3<
48*d75c37aeSmseidel 		lang::XServiceInfo,
49*d75c37aeSmseidel 		io::XSeekableInputStream,
50*d75c37aeSmseidel 		lang::XInitialization>
51cdf0e10cSrcweir {
52cdf0e10cSrcweir public:
53*d75c37aeSmseidel 	explicit SequenceInputStreamService();
54cdf0e10cSrcweir 
55*d75c37aeSmseidel 	// ::com::sun::star::lang::XServiceInfo:
56*d75c37aeSmseidel 	virtual ::rtl::OUString SAL_CALL getImplementationName() throw ( uno::RuntimeException );
57*d75c37aeSmseidel 	virtual ::sal_Bool SAL_CALL supportsService( const ::rtl::OUString & ServiceName ) throw ( uno::RuntimeException );
58*d75c37aeSmseidel 	virtual uno::Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames() throw ( uno::RuntimeException );
59cdf0e10cSrcweir 
60*d75c37aeSmseidel 	// XServiceInfo - static versions (used for component registration)
61*d75c37aeSmseidel 	static ::rtl::OUString SAL_CALL getImplementationName_static();
62*d75c37aeSmseidel 	static uno::Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames_static();
63*d75c37aeSmseidel 	static uno::Reference< uno::XInterface > SAL_CALL Create( const uno::Reference< uno::XComponentContext >& );
64cdf0e10cSrcweir 
65*d75c37aeSmseidel 	// ::com::sun::star::io::XInputStream:
66*d75c37aeSmseidel 	virtual ::sal_Int32 SAL_CALL readBytes( uno::Sequence< ::sal_Int8 > & aData, ::sal_Int32 nBytesToRead ) throw ( uno::RuntimeException, io::NotConnectedException, io::BufferSizeExceededException, io::IOException );
67*d75c37aeSmseidel 	virtual ::sal_Int32 SAL_CALL readSomeBytes( uno::Sequence< ::sal_Int8 > & aData, ::sal_Int32 nMaxBytesToRead ) throw ( uno::RuntimeException, io::NotConnectedException, io::BufferSizeExceededException, io::IOException );
68*d75c37aeSmseidel 	virtual void SAL_CALL skipBytes( ::sal_Int32 nBytesToSkip ) throw ( uno::RuntimeException, io::NotConnectedException, io::BufferSizeExceededException, io::IOException );
69*d75c37aeSmseidel 	virtual ::sal_Int32 SAL_CALL available() throw ( uno::RuntimeException, io::NotConnectedException, io::IOException );
70*d75c37aeSmseidel 	virtual void SAL_CALL closeInput() throw ( uno::RuntimeException, io::NotConnectedException, io::IOException );
71cdf0e10cSrcweir 
72*d75c37aeSmseidel 	// ::com::sun::star::io::XSeekable:
73*d75c37aeSmseidel 	virtual void SAL_CALL seek( ::sal_Int64 location ) throw ( uno::RuntimeException, lang::IllegalArgumentException, io::IOException );
74*d75c37aeSmseidel 	virtual ::sal_Int64 SAL_CALL getPosition() throw ( uno::RuntimeException, io::IOException );
75*d75c37aeSmseidel 	virtual ::sal_Int64 SAL_CALL getLength() throw ( uno::RuntimeException, io::IOException );
76cdf0e10cSrcweir 
77*d75c37aeSmseidel 	// ::com::sun::star::lang::XInitialization:
78*d75c37aeSmseidel 	virtual void SAL_CALL initialize( const uno::Sequence< ::com::sun::star::uno::Any > & aArguments ) throw ( uno::RuntimeException, uno::Exception );
79cdf0e10cSrcweir 
80cdf0e10cSrcweir private:
81*d75c37aeSmseidel 	SequenceInputStreamService( SequenceInputStreamService & ); // not defined
82*d75c37aeSmseidel 	void operator =( SequenceInputStreamService & ); // not defined
83cdf0e10cSrcweir 
~SequenceInputStreamService()84*d75c37aeSmseidel 	virtual ~SequenceInputStreamService() {}
85cdf0e10cSrcweir 
86cdf0e10cSrcweir 
87*d75c37aeSmseidel 	::osl::Mutex m_aMutex;
88*d75c37aeSmseidel 	sal_Bool m_bInitialized;
89*d75c37aeSmseidel 	uno::Reference< io::XInputStream > m_xInputStream;
90*d75c37aeSmseidel 	uno::Reference< io::XSeekable > m_xSeekable;
91cdf0e10cSrcweir };
92cdf0e10cSrcweir 
SequenceInputStreamService()93cdf0e10cSrcweir SequenceInputStreamService::SequenceInputStreamService()
94cdf0e10cSrcweir : m_bInitialized( sal_False )
95cdf0e10cSrcweir {}
96cdf0e10cSrcweir 
97cdf0e10cSrcweir // com.sun.star.uno.XServiceInfo:
getImplementationName()98cdf0e10cSrcweir ::rtl::OUString SAL_CALL SequenceInputStreamService::getImplementationName() throw ( uno::RuntimeException )
99cdf0e10cSrcweir {
100*d75c37aeSmseidel 	return getImplementationName_static();
101cdf0e10cSrcweir }
102cdf0e10cSrcweir 
getImplementationName_static()103cdf0e10cSrcweir ::rtl::OUString SAL_CALL SequenceInputStreamService::getImplementationName_static()
104cdf0e10cSrcweir {
105*d75c37aeSmseidel 	return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.comp.SequenceInputStreamService" ) );
106cdf0e10cSrcweir }
107cdf0e10cSrcweir 
supportsService(::rtl::OUString const & serviceName)108cdf0e10cSrcweir ::sal_Bool SAL_CALL SequenceInputStreamService::supportsService( ::rtl::OUString const & serviceName ) throw ( uno::RuntimeException )
109cdf0e10cSrcweir {
110*d75c37aeSmseidel 	uno::Sequence< ::rtl::OUString > serviceNames = getSupportedServiceNames();
111*d75c37aeSmseidel 	for ( ::sal_Int32 i = 0; i < serviceNames.getLength(); ++i ) {
112*d75c37aeSmseidel 		if ( serviceNames[i] == serviceName )
113*d75c37aeSmseidel 			return sal_True;
114*d75c37aeSmseidel 	}
115*d75c37aeSmseidel 	return sal_False;
116cdf0e10cSrcweir }
117cdf0e10cSrcweir 
getSupportedServiceNames()118cdf0e10cSrcweir uno::Sequence< ::rtl::OUString > SAL_CALL SequenceInputStreamService::getSupportedServiceNames() throw ( uno::RuntimeException )
119cdf0e10cSrcweir {
120*d75c37aeSmseidel 	return getSupportedServiceNames_static();
121cdf0e10cSrcweir }
122cdf0e10cSrcweir 
getSupportedServiceNames_static()123cdf0e10cSrcweir uno::Sequence< ::rtl::OUString > SAL_CALL SequenceInputStreamService::getSupportedServiceNames_static()
124cdf0e10cSrcweir {
125*d75c37aeSmseidel 	uno::Sequence< ::rtl::OUString > s( 1 );
126*d75c37aeSmseidel 	s[0] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(
127*d75c37aeSmseidel 		"com.sun.star.io.SequenceInputStream" ) );
128*d75c37aeSmseidel 	return s;
129cdf0e10cSrcweir }
130cdf0e10cSrcweir 
Create(const uno::Reference<uno::XComponentContext> &)131cdf0e10cSrcweir uno::Reference< uno::XInterface > SAL_CALL SequenceInputStreamService::Create(
132*d75c37aeSmseidel 	const uno::Reference< uno::XComponentContext >& )
133cdf0e10cSrcweir {
134*d75c37aeSmseidel 	return static_cast< ::cppu::OWeakObject * >( new SequenceInputStreamService() );
135cdf0e10cSrcweir }
136cdf0e10cSrcweir 
137cdf0e10cSrcweir // ::com::sun::star::io::XInputStream:
readBytes(uno::Sequence<::sal_Int8> & aData,::sal_Int32 nBytesToRead)138cdf0e10cSrcweir ::sal_Int32 SAL_CALL SequenceInputStreamService::readBytes( uno::Sequence< ::sal_Int8 > & aData, ::sal_Int32 nBytesToRead ) throw ( uno::RuntimeException, io::NotConnectedException, io::BufferSizeExceededException, io::IOException )
139cdf0e10cSrcweir {
140*d75c37aeSmseidel 	::osl::MutexGuard aGuard( m_aMutex );
141*d75c37aeSmseidel 	if ( !m_xInputStream.is() )
142*d75c37aeSmseidel 		throw io::NotConnectedException();
143cdf0e10cSrcweir 
144*d75c37aeSmseidel 	return m_xInputStream->readBytes( aData, nBytesToRead );
145cdf0e10cSrcweir }
146cdf0e10cSrcweir 
readSomeBytes(uno::Sequence<::sal_Int8> & aData,::sal_Int32 nMaxBytesToRead)147cdf0e10cSrcweir ::sal_Int32 SAL_CALL SequenceInputStreamService::readSomeBytes( uno::Sequence< ::sal_Int8 > & aData, ::sal_Int32 nMaxBytesToRead ) throw ( uno::RuntimeException, io::NotConnectedException, io::BufferSizeExceededException, io::IOException )
148cdf0e10cSrcweir {
149*d75c37aeSmseidel 	::osl::MutexGuard aGuard( m_aMutex );
150*d75c37aeSmseidel 	if ( !m_xInputStream.is() )
151*d75c37aeSmseidel 		throw io::NotConnectedException();
152cdf0e10cSrcweir 
153*d75c37aeSmseidel 	return m_xInputStream->readSomeBytes( aData, nMaxBytesToRead );
154cdf0e10cSrcweir }
155cdf0e10cSrcweir 
skipBytes(::sal_Int32 nBytesToSkip)156cdf0e10cSrcweir void SAL_CALL SequenceInputStreamService::skipBytes( ::sal_Int32 nBytesToSkip ) throw ( uno::RuntimeException, io::NotConnectedException, io::BufferSizeExceededException, io::IOException )
157cdf0e10cSrcweir {
158*d75c37aeSmseidel 	::osl::MutexGuard aGuard( m_aMutex );
159*d75c37aeSmseidel 	if ( !m_xInputStream.is() )
160*d75c37aeSmseidel 		throw io::NotConnectedException();
161cdf0e10cSrcweir 
162*d75c37aeSmseidel 	return m_xInputStream->skipBytes( nBytesToSkip );
163cdf0e10cSrcweir }
164cdf0e10cSrcweir 
available()165cdf0e10cSrcweir ::sal_Int32 SAL_CALL SequenceInputStreamService::available() throw ( uno::RuntimeException, io::NotConnectedException, io::IOException )
166cdf0e10cSrcweir {
167*d75c37aeSmseidel 	::osl::MutexGuard aGuard( m_aMutex );
168*d75c37aeSmseidel 	if ( !m_xInputStream.is() )
169*d75c37aeSmseidel 		throw io::NotConnectedException();
170cdf0e10cSrcweir 
171*d75c37aeSmseidel 	return m_xInputStream->available();
172cdf0e10cSrcweir }
173cdf0e10cSrcweir 
closeInput()174cdf0e10cSrcweir void SAL_CALL SequenceInputStreamService::closeInput() throw ( uno::RuntimeException, io::NotConnectedException, io::IOException )
175cdf0e10cSrcweir {
176*d75c37aeSmseidel 	::osl::MutexGuard aGuard( m_aMutex );
177*d75c37aeSmseidel 	if ( !m_xInputStream.is() )
178*d75c37aeSmseidel 		throw io::NotConnectedException();
179cdf0e10cSrcweir 
180*d75c37aeSmseidel 	m_xInputStream->closeInput();
181*d75c37aeSmseidel 	m_xInputStream = uno::Reference< io::XInputStream >();
182*d75c37aeSmseidel 	m_xSeekable = uno::Reference< io::XSeekable >();
183cdf0e10cSrcweir }
184cdf0e10cSrcweir 
185cdf0e10cSrcweir // ::com::sun::star::io::XSeekable:
seek(::sal_Int64 location)186cdf0e10cSrcweir void SAL_CALL SequenceInputStreamService::seek( ::sal_Int64 location ) throw ( uno::RuntimeException, lang::IllegalArgumentException, io::IOException )
187cdf0e10cSrcweir {
188*d75c37aeSmseidel 	::osl::MutexGuard aGuard( m_aMutex );
189*d75c37aeSmseidel 	if ( !m_xSeekable.is() )
190*d75c37aeSmseidel 		throw io::NotConnectedException();
191cdf0e10cSrcweir 
192*d75c37aeSmseidel 	m_xSeekable->seek( location );
193cdf0e10cSrcweir }
194cdf0e10cSrcweir 
getPosition()195cdf0e10cSrcweir ::sal_Int64 SAL_CALL SequenceInputStreamService::getPosition() throw ( uno::RuntimeException, io::IOException )
196cdf0e10cSrcweir {
197*d75c37aeSmseidel 	::osl::MutexGuard aGuard( m_aMutex );
198*d75c37aeSmseidel 	if ( !m_xSeekable.is() )
199*d75c37aeSmseidel 		throw io::NotConnectedException();
200cdf0e10cSrcweir 
201*d75c37aeSmseidel 	return m_xSeekable->getPosition();
202cdf0e10cSrcweir }
203cdf0e10cSrcweir 
getLength()204cdf0e10cSrcweir ::sal_Int64 SAL_CALL SequenceInputStreamService::getLength() throw ( uno::RuntimeException, io::IOException )
205cdf0e10cSrcweir {
206*d75c37aeSmseidel 	::osl::MutexGuard aGuard( m_aMutex );
207*d75c37aeSmseidel 	if ( !m_xSeekable.is() )
208*d75c37aeSmseidel 		throw io::NotConnectedException();
209cdf0e10cSrcweir 
210*d75c37aeSmseidel 	return m_xSeekable->getLength();
211cdf0e10cSrcweir }
212cdf0e10cSrcweir 
213cdf0e10cSrcweir // ::com::sun::star::lang::XInitialization:
initialize(const uno::Sequence<::com::sun::star::uno::Any> & aArguments)214cdf0e10cSrcweir void SAL_CALL SequenceInputStreamService::initialize( const uno::Sequence< ::com::sun::star::uno::Any > & aArguments ) throw ( uno::RuntimeException, uno::Exception )
215cdf0e10cSrcweir {
216*d75c37aeSmseidel 	::osl::MutexGuard aGuard( m_aMutex );
217*d75c37aeSmseidel 	if ( m_bInitialized )
218*d75c37aeSmseidel 		throw frame::DoubleInitializationException();
219cdf0e10cSrcweir 
220cdf0e10cSrcweir 	if ( aArguments.getLength() != 1 )
221cdf0e10cSrcweir 		throw lang::IllegalArgumentException( ::rtl::OUString::createFromAscii( "Wrong number of arguments!\n" ),
222cdf0e10cSrcweir 											uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ),
223cdf0e10cSrcweir 											1 );
224cdf0e10cSrcweir 
225*d75c37aeSmseidel 	uno::Sequence< sal_Int8 > aSeq;
226*d75c37aeSmseidel 	if ( aArguments[0] >>= aSeq )
227*d75c37aeSmseidel 	{
228*d75c37aeSmseidel 		uno::Reference< io::XInputStream > xInputStream(
229*d75c37aeSmseidel 						static_cast< ::cppu::OWeakObject* >( new ::comphelper::SequenceInputStream( aSeq ) ),
230*d75c37aeSmseidel 						uno::UNO_QUERY_THROW );
231*d75c37aeSmseidel 		uno::Reference< io::XSeekable > xSeekable( xInputStream, uno::UNO_QUERY_THROW );
232*d75c37aeSmseidel 		m_xInputStream = xInputStream;
233*d75c37aeSmseidel 		m_xSeekable = xSeekable;
234*d75c37aeSmseidel 		m_bInitialized = sal_True;
235*d75c37aeSmseidel 	}
236*d75c37aeSmseidel 	else
237cdf0e10cSrcweir 		throw lang::IllegalArgumentException( ::rtl::OUString::createFromAscii( "Unexpected type of argument!\n" ),
238cdf0e10cSrcweir 											uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ),
239cdf0e10cSrcweir 											1 );
240cdf0e10cSrcweir }
241cdf0e10cSrcweir 
242cdf0e10cSrcweir } // anonymous namespace
243cdf0e10cSrcweir 
createRegistryInfo_SequenceInputStream()244cdf0e10cSrcweir void createRegistryInfo_SequenceInputStream()
245cdf0e10cSrcweir {
246*d75c37aeSmseidel 	static ::comphelper::module::OAutoRegistration< SequenceInputStreamService > aAutoRegistration;
247cdf0e10cSrcweir }
248