xref: /trunk/main/comphelper/source/streaming/seqinputstreamserv.cxx (revision b9a4ae0fa8e1446f2d3d30226e5244245afa6a63)
1*dde7d3faSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*dde7d3faSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*dde7d3faSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*dde7d3faSAndrew Rist  * distributed with this work for additional information
6*dde7d3faSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*dde7d3faSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*dde7d3faSAndrew Rist  * "License"); you may not use this file except in compliance
9*dde7d3faSAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11*dde7d3faSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13*dde7d3faSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*dde7d3faSAndrew Rist  * software distributed under the License is distributed on an
15*dde7d3faSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*dde7d3faSAndrew Rist  * KIND, either express or implied.  See the License for the
17*dde7d3faSAndrew Rist  * specific language governing permissions and limitations
18*dde7d3faSAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20*dde7d3faSAndrew Rist  *************************************************************/
21*dde7d3faSAndrew Rist 
22*dde7d3faSAndrew 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:
47cdf0e10cSrcweir     public ::cppu::WeakImplHelper3<
48cdf0e10cSrcweir         lang::XServiceInfo,
49cdf0e10cSrcweir         io::XSeekableInputStream,
50cdf0e10cSrcweir         lang::XInitialization>
51cdf0e10cSrcweir {
52cdf0e10cSrcweir public:
53cdf0e10cSrcweir     explicit SequenceInputStreamService();
54cdf0e10cSrcweir 
55cdf0e10cSrcweir     // ::com::sun::star::lang::XServiceInfo:
56cdf0e10cSrcweir     virtual ::rtl::OUString SAL_CALL getImplementationName() throw ( uno::RuntimeException );
57cdf0e10cSrcweir     virtual ::sal_Bool SAL_CALL supportsService( const ::rtl::OUString & ServiceName ) throw ( uno::RuntimeException );
58cdf0e10cSrcweir     virtual uno::Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames() throw ( uno::RuntimeException );
59cdf0e10cSrcweir 
60cdf0e10cSrcweir     // XServiceInfo - static versions (used for component registration)
61cdf0e10cSrcweir     static ::rtl::OUString SAL_CALL getImplementationName_static();
62cdf0e10cSrcweir     static uno::Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames_static();
63cdf0e10cSrcweir     static uno::Reference< uno::XInterface > SAL_CALL Create( const uno::Reference< uno::XComponentContext >& );
64cdf0e10cSrcweir 
65cdf0e10cSrcweir     // ::com::sun::star::io::XInputStream:
66cdf0e10cSrcweir     virtual ::sal_Int32 SAL_CALL readBytes( uno::Sequence< ::sal_Int8 > & aData, ::sal_Int32 nBytesToRead ) throw ( uno::RuntimeException, io::NotConnectedException, io::BufferSizeExceededException, io::IOException );
67cdf0e10cSrcweir     virtual ::sal_Int32 SAL_CALL readSomeBytes( uno::Sequence< ::sal_Int8 > & aData, ::sal_Int32 nMaxBytesToRead ) throw ( uno::RuntimeException, io::NotConnectedException, io::BufferSizeExceededException, io::IOException );
68cdf0e10cSrcweir     virtual void SAL_CALL skipBytes( ::sal_Int32 nBytesToSkip ) throw ( uno::RuntimeException, io::NotConnectedException, io::BufferSizeExceededException, io::IOException );
69cdf0e10cSrcweir     virtual ::sal_Int32 SAL_CALL available() throw ( uno::RuntimeException, io::NotConnectedException, io::IOException );
70cdf0e10cSrcweir     virtual void SAL_CALL closeInput() throw ( uno::RuntimeException, io::NotConnectedException, io::IOException );
71cdf0e10cSrcweir 
72cdf0e10cSrcweir     // ::com::sun::star::io::XSeekable:
73cdf0e10cSrcweir     virtual void SAL_CALL seek( ::sal_Int64 location ) throw ( uno::RuntimeException, lang::IllegalArgumentException, io::IOException );
74cdf0e10cSrcweir     virtual ::sal_Int64 SAL_CALL getPosition() throw ( uno::RuntimeException, io::IOException );
75cdf0e10cSrcweir     virtual ::sal_Int64 SAL_CALL getLength() throw ( uno::RuntimeException, io::IOException );
76cdf0e10cSrcweir 
77cdf0e10cSrcweir     // ::com::sun::star::lang::XInitialization:
78cdf0e10cSrcweir     virtual void SAL_CALL initialize( const uno::Sequence< ::com::sun::star::uno::Any > & aArguments ) throw ( uno::RuntimeException, uno::Exception );
79cdf0e10cSrcweir 
80cdf0e10cSrcweir private:
81cdf0e10cSrcweir     SequenceInputStreamService( SequenceInputStreamService & ); // not defined
82cdf0e10cSrcweir     void operator =( SequenceInputStreamService & ); // not defined
83cdf0e10cSrcweir 
~SequenceInputStreamService()84cdf0e10cSrcweir     virtual ~SequenceInputStreamService() {}
85cdf0e10cSrcweir 
86cdf0e10cSrcweir 
87cdf0e10cSrcweir     ::osl::Mutex m_aMutex;
88cdf0e10cSrcweir     sal_Bool m_bInitialized;
89cdf0e10cSrcweir     uno::Reference< io::XInputStream > m_xInputStream;
90cdf0e10cSrcweir     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 {
100cdf0e10cSrcweir     return getImplementationName_static();
101cdf0e10cSrcweir }
102cdf0e10cSrcweir 
getImplementationName_static()103cdf0e10cSrcweir ::rtl::OUString SAL_CALL SequenceInputStreamService::getImplementationName_static()
104cdf0e10cSrcweir {
105cdf0e10cSrcweir     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 {
110cdf0e10cSrcweir     uno::Sequence< ::rtl::OUString > serviceNames = getSupportedServiceNames();
111cdf0e10cSrcweir     for ( ::sal_Int32 i = 0; i < serviceNames.getLength(); ++i ) {
112cdf0e10cSrcweir         if ( serviceNames[i] == serviceName )
113cdf0e10cSrcweir             return sal_True;
114cdf0e10cSrcweir     }
115cdf0e10cSrcweir     return sal_False;
116cdf0e10cSrcweir }
117cdf0e10cSrcweir 
getSupportedServiceNames()118cdf0e10cSrcweir uno::Sequence< ::rtl::OUString > SAL_CALL SequenceInputStreamService::getSupportedServiceNames() throw ( uno::RuntimeException )
119cdf0e10cSrcweir {
120cdf0e10cSrcweir     return getSupportedServiceNames_static();
121cdf0e10cSrcweir }
122cdf0e10cSrcweir 
getSupportedServiceNames_static()123cdf0e10cSrcweir uno::Sequence< ::rtl::OUString > SAL_CALL SequenceInputStreamService::getSupportedServiceNames_static()
124cdf0e10cSrcweir {
125cdf0e10cSrcweir     uno::Sequence< ::rtl::OUString > s( 1 );
126cdf0e10cSrcweir     s[0] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(
127cdf0e10cSrcweir         "com.sun.star.io.SequenceInputStream" ) );
128cdf0e10cSrcweir     return s;
129cdf0e10cSrcweir }
130cdf0e10cSrcweir 
Create(const uno::Reference<uno::XComponentContext> &)131cdf0e10cSrcweir uno::Reference< uno::XInterface > SAL_CALL SequenceInputStreamService::Create(
132cdf0e10cSrcweir     const uno::Reference< uno::XComponentContext >& )
133cdf0e10cSrcweir {
134cdf0e10cSrcweir     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 {
140cdf0e10cSrcweir     ::osl::MutexGuard aGuard( m_aMutex );
141cdf0e10cSrcweir     if ( !m_xInputStream.is() )
142cdf0e10cSrcweir         throw io::NotConnectedException();
143cdf0e10cSrcweir 
144cdf0e10cSrcweir     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 {
149cdf0e10cSrcweir     ::osl::MutexGuard aGuard( m_aMutex );
150cdf0e10cSrcweir     if ( !m_xInputStream.is() )
151cdf0e10cSrcweir         throw io::NotConnectedException();
152cdf0e10cSrcweir 
153cdf0e10cSrcweir     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 {
158cdf0e10cSrcweir     ::osl::MutexGuard aGuard( m_aMutex );
159cdf0e10cSrcweir     if ( !m_xInputStream.is() )
160cdf0e10cSrcweir         throw io::NotConnectedException();
161cdf0e10cSrcweir 
162cdf0e10cSrcweir     return m_xInputStream->skipBytes( nBytesToSkip );
163cdf0e10cSrcweir }
164cdf0e10cSrcweir 
available()165cdf0e10cSrcweir ::sal_Int32 SAL_CALL SequenceInputStreamService::available() throw ( uno::RuntimeException, io::NotConnectedException, io::IOException )
166cdf0e10cSrcweir {
167cdf0e10cSrcweir     ::osl::MutexGuard aGuard( m_aMutex );
168cdf0e10cSrcweir     if ( !m_xInputStream.is() )
169cdf0e10cSrcweir         throw io::NotConnectedException();
170cdf0e10cSrcweir 
171cdf0e10cSrcweir     return m_xInputStream->available();
172cdf0e10cSrcweir }
173cdf0e10cSrcweir 
closeInput()174cdf0e10cSrcweir void SAL_CALL SequenceInputStreamService::closeInput() throw ( uno::RuntimeException, io::NotConnectedException, io::IOException )
175cdf0e10cSrcweir {
176cdf0e10cSrcweir     ::osl::MutexGuard aGuard( m_aMutex );
177cdf0e10cSrcweir     if ( !m_xInputStream.is() )
178cdf0e10cSrcweir         throw io::NotConnectedException();
179cdf0e10cSrcweir 
180cdf0e10cSrcweir     m_xInputStream->closeInput();
181cdf0e10cSrcweir     m_xInputStream = uno::Reference< io::XInputStream >();
182cdf0e10cSrcweir     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 {
188cdf0e10cSrcweir     ::osl::MutexGuard aGuard( m_aMutex );
189cdf0e10cSrcweir     if ( !m_xSeekable.is() )
190cdf0e10cSrcweir         throw io::NotConnectedException();
191cdf0e10cSrcweir 
192cdf0e10cSrcweir     m_xSeekable->seek( location );
193cdf0e10cSrcweir }
194cdf0e10cSrcweir 
getPosition()195cdf0e10cSrcweir ::sal_Int64 SAL_CALL SequenceInputStreamService::getPosition() throw ( uno::RuntimeException, io::IOException )
196cdf0e10cSrcweir {
197cdf0e10cSrcweir     ::osl::MutexGuard aGuard( m_aMutex );
198cdf0e10cSrcweir     if ( !m_xSeekable.is() )
199cdf0e10cSrcweir         throw io::NotConnectedException();
200cdf0e10cSrcweir 
201cdf0e10cSrcweir     return m_xSeekable->getPosition();
202cdf0e10cSrcweir }
203cdf0e10cSrcweir 
getLength()204cdf0e10cSrcweir ::sal_Int64 SAL_CALL SequenceInputStreamService::getLength() throw ( uno::RuntimeException, io::IOException )
205cdf0e10cSrcweir {
206cdf0e10cSrcweir     ::osl::MutexGuard aGuard( m_aMutex );
207cdf0e10cSrcweir     if ( !m_xSeekable.is() )
208cdf0e10cSrcweir         throw io::NotConnectedException();
209cdf0e10cSrcweir 
210cdf0e10cSrcweir     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 {
216cdf0e10cSrcweir     ::osl::MutexGuard aGuard( m_aMutex );
217cdf0e10cSrcweir     if ( m_bInitialized )
218cdf0e10cSrcweir         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 
225cdf0e10cSrcweir     uno::Sequence< sal_Int8 > aSeq;
226cdf0e10cSrcweir     if ( aArguments[0] >>= aSeq )
227cdf0e10cSrcweir     {
228cdf0e10cSrcweir         uno::Reference< io::XInputStream > xInputStream(
229cdf0e10cSrcweir                         static_cast< ::cppu::OWeakObject* >( new ::comphelper::SequenceInputStream( aSeq ) ),
230cdf0e10cSrcweir                         uno::UNO_QUERY_THROW );
231cdf0e10cSrcweir         uno::Reference< io::XSeekable > xSeekable( xInputStream, uno::UNO_QUERY_THROW );
232cdf0e10cSrcweir         m_xInputStream = xInputStream;
233cdf0e10cSrcweir         m_xSeekable = xSeekable;
234cdf0e10cSrcweir         m_bInitialized = sal_True;
235cdf0e10cSrcweir     }
236cdf0e10cSrcweir     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 {
246cdf0e10cSrcweir     static ::comphelper::module::OAutoRegistration< SequenceInputStreamService > aAutoRegistration;
247cdf0e10cSrcweir }
248