xref: /trunk/main/comphelper/source/streaming/seqinputstreamserv.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER( update_precomp.py ): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_comphelper.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include "comphelper_module.hxx"
32*cdf0e10cSrcweir 
33*cdf0e10cSrcweir #include <sal/config.h>
34*cdf0e10cSrcweir #include <osl/mutex.hxx>
35*cdf0e10cSrcweir #include <cppuhelper/factory.hxx>
36*cdf0e10cSrcweir #include <cppuhelper/implementationentry.hxx>
37*cdf0e10cSrcweir #include <cppuhelper/implbase3.hxx>
38*cdf0e10cSrcweir #include <comphelper/seqstream.hxx>
39*cdf0e10cSrcweir #include <com/sun/star/lang/XServiceInfo.hpp>
40*cdf0e10cSrcweir #include <com/sun/star/io/XSeekableInputStream.hpp>
41*cdf0e10cSrcweir #include <com/sun/star/lang/XInitialization.hpp>
42*cdf0e10cSrcweir #include <com/sun/star/frame/DoubleInitializationException.hpp>
43*cdf0e10cSrcweir #include <com/sun/star/uno/XComponentContext.hpp>
44*cdf0e10cSrcweir 
45*cdf0e10cSrcweir 
46*cdf0e10cSrcweir using namespace ::com::sun::star;
47*cdf0e10cSrcweir 
48*cdf0e10cSrcweir namespace {
49*cdf0e10cSrcweir 
50*cdf0e10cSrcweir class SequenceInputStreamService:
51*cdf0e10cSrcweir     public ::cppu::WeakImplHelper3<
52*cdf0e10cSrcweir         lang::XServiceInfo,
53*cdf0e10cSrcweir         io::XSeekableInputStream,
54*cdf0e10cSrcweir         lang::XInitialization>
55*cdf0e10cSrcweir {
56*cdf0e10cSrcweir public:
57*cdf0e10cSrcweir     explicit SequenceInputStreamService();
58*cdf0e10cSrcweir 
59*cdf0e10cSrcweir     // ::com::sun::star::lang::XServiceInfo:
60*cdf0e10cSrcweir     virtual ::rtl::OUString SAL_CALL getImplementationName() throw ( uno::RuntimeException );
61*cdf0e10cSrcweir     virtual ::sal_Bool SAL_CALL supportsService( const ::rtl::OUString & ServiceName ) throw ( uno::RuntimeException );
62*cdf0e10cSrcweir     virtual uno::Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames() throw ( uno::RuntimeException );
63*cdf0e10cSrcweir 
64*cdf0e10cSrcweir     // XServiceInfo - static versions (used for component registration)
65*cdf0e10cSrcweir     static ::rtl::OUString SAL_CALL getImplementationName_static();
66*cdf0e10cSrcweir     static uno::Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames_static();
67*cdf0e10cSrcweir     static uno::Reference< uno::XInterface > SAL_CALL Create( const uno::Reference< uno::XComponentContext >& );
68*cdf0e10cSrcweir 
69*cdf0e10cSrcweir     // ::com::sun::star::io::XInputStream:
70*cdf0e10cSrcweir     virtual ::sal_Int32 SAL_CALL readBytes( uno::Sequence< ::sal_Int8 > & aData, ::sal_Int32 nBytesToRead ) throw ( uno::RuntimeException, io::NotConnectedException, io::BufferSizeExceededException, io::IOException );
71*cdf0e10cSrcweir     virtual ::sal_Int32 SAL_CALL readSomeBytes( uno::Sequence< ::sal_Int8 > & aData, ::sal_Int32 nMaxBytesToRead ) throw ( uno::RuntimeException, io::NotConnectedException, io::BufferSizeExceededException, io::IOException );
72*cdf0e10cSrcweir     virtual void SAL_CALL skipBytes( ::sal_Int32 nBytesToSkip ) throw ( uno::RuntimeException, io::NotConnectedException, io::BufferSizeExceededException, io::IOException );
73*cdf0e10cSrcweir     virtual ::sal_Int32 SAL_CALL available() throw ( uno::RuntimeException, io::NotConnectedException, io::IOException );
74*cdf0e10cSrcweir     virtual void SAL_CALL closeInput() throw ( uno::RuntimeException, io::NotConnectedException, io::IOException );
75*cdf0e10cSrcweir 
76*cdf0e10cSrcweir     // ::com::sun::star::io::XSeekable:
77*cdf0e10cSrcweir     virtual void SAL_CALL seek( ::sal_Int64 location ) throw ( uno::RuntimeException, lang::IllegalArgumentException, io::IOException );
78*cdf0e10cSrcweir     virtual ::sal_Int64 SAL_CALL getPosition() throw ( uno::RuntimeException, io::IOException );
79*cdf0e10cSrcweir     virtual ::sal_Int64 SAL_CALL getLength() throw ( uno::RuntimeException, io::IOException );
80*cdf0e10cSrcweir 
81*cdf0e10cSrcweir     // ::com::sun::star::lang::XInitialization:
82*cdf0e10cSrcweir     virtual void SAL_CALL initialize( const uno::Sequence< ::com::sun::star::uno::Any > & aArguments ) throw ( uno::RuntimeException, uno::Exception );
83*cdf0e10cSrcweir 
84*cdf0e10cSrcweir private:
85*cdf0e10cSrcweir     SequenceInputStreamService( SequenceInputStreamService & ); // not defined
86*cdf0e10cSrcweir     void operator =( SequenceInputStreamService & ); // not defined
87*cdf0e10cSrcweir 
88*cdf0e10cSrcweir     virtual ~SequenceInputStreamService() {}
89*cdf0e10cSrcweir 
90*cdf0e10cSrcweir 
91*cdf0e10cSrcweir     ::osl::Mutex m_aMutex;
92*cdf0e10cSrcweir     sal_Bool m_bInitialized;
93*cdf0e10cSrcweir     uno::Reference< io::XInputStream > m_xInputStream;
94*cdf0e10cSrcweir     uno::Reference< io::XSeekable > m_xSeekable;
95*cdf0e10cSrcweir };
96*cdf0e10cSrcweir 
97*cdf0e10cSrcweir SequenceInputStreamService::SequenceInputStreamService()
98*cdf0e10cSrcweir : m_bInitialized( sal_False )
99*cdf0e10cSrcweir {}
100*cdf0e10cSrcweir 
101*cdf0e10cSrcweir // com.sun.star.uno.XServiceInfo:
102*cdf0e10cSrcweir ::rtl::OUString SAL_CALL SequenceInputStreamService::getImplementationName() throw ( uno::RuntimeException )
103*cdf0e10cSrcweir {
104*cdf0e10cSrcweir     return getImplementationName_static();
105*cdf0e10cSrcweir }
106*cdf0e10cSrcweir 
107*cdf0e10cSrcweir ::rtl::OUString SAL_CALL SequenceInputStreamService::getImplementationName_static()
108*cdf0e10cSrcweir {
109*cdf0e10cSrcweir     return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.comp.SequenceInputStreamService" ) );
110*cdf0e10cSrcweir }
111*cdf0e10cSrcweir 
112*cdf0e10cSrcweir ::sal_Bool SAL_CALL SequenceInputStreamService::supportsService( ::rtl::OUString const & serviceName ) throw ( uno::RuntimeException )
113*cdf0e10cSrcweir {
114*cdf0e10cSrcweir     uno::Sequence< ::rtl::OUString > serviceNames = getSupportedServiceNames();
115*cdf0e10cSrcweir     for ( ::sal_Int32 i = 0; i < serviceNames.getLength(); ++i ) {
116*cdf0e10cSrcweir         if ( serviceNames[i] == serviceName )
117*cdf0e10cSrcweir             return sal_True;
118*cdf0e10cSrcweir     }
119*cdf0e10cSrcweir     return sal_False;
120*cdf0e10cSrcweir }
121*cdf0e10cSrcweir 
122*cdf0e10cSrcweir uno::Sequence< ::rtl::OUString > SAL_CALL SequenceInputStreamService::getSupportedServiceNames() throw ( uno::RuntimeException )
123*cdf0e10cSrcweir {
124*cdf0e10cSrcweir     return getSupportedServiceNames_static();
125*cdf0e10cSrcweir }
126*cdf0e10cSrcweir 
127*cdf0e10cSrcweir uno::Sequence< ::rtl::OUString > SAL_CALL SequenceInputStreamService::getSupportedServiceNames_static()
128*cdf0e10cSrcweir {
129*cdf0e10cSrcweir     uno::Sequence< ::rtl::OUString > s( 1 );
130*cdf0e10cSrcweir     s[0] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(
131*cdf0e10cSrcweir         "com.sun.star.io.SequenceInputStream" ) );
132*cdf0e10cSrcweir     return s;
133*cdf0e10cSrcweir }
134*cdf0e10cSrcweir 
135*cdf0e10cSrcweir uno::Reference< uno::XInterface > SAL_CALL SequenceInputStreamService::Create(
136*cdf0e10cSrcweir     const uno::Reference< uno::XComponentContext >& )
137*cdf0e10cSrcweir {
138*cdf0e10cSrcweir     return static_cast< ::cppu::OWeakObject * >( new SequenceInputStreamService() );
139*cdf0e10cSrcweir }
140*cdf0e10cSrcweir 
141*cdf0e10cSrcweir // ::com::sun::star::io::XInputStream:
142*cdf0e10cSrcweir ::sal_Int32 SAL_CALL SequenceInputStreamService::readBytes( uno::Sequence< ::sal_Int8 > & aData, ::sal_Int32 nBytesToRead ) throw ( uno::RuntimeException, io::NotConnectedException, io::BufferSizeExceededException, io::IOException )
143*cdf0e10cSrcweir {
144*cdf0e10cSrcweir     ::osl::MutexGuard aGuard( m_aMutex );
145*cdf0e10cSrcweir     if ( !m_xInputStream.is() )
146*cdf0e10cSrcweir         throw io::NotConnectedException();
147*cdf0e10cSrcweir 
148*cdf0e10cSrcweir     return m_xInputStream->readBytes( aData, nBytesToRead );
149*cdf0e10cSrcweir }
150*cdf0e10cSrcweir 
151*cdf0e10cSrcweir ::sal_Int32 SAL_CALL SequenceInputStreamService::readSomeBytes( uno::Sequence< ::sal_Int8 > & aData, ::sal_Int32 nMaxBytesToRead ) throw ( uno::RuntimeException, io::NotConnectedException, io::BufferSizeExceededException, io::IOException )
152*cdf0e10cSrcweir {
153*cdf0e10cSrcweir     ::osl::MutexGuard aGuard( m_aMutex );
154*cdf0e10cSrcweir     if ( !m_xInputStream.is() )
155*cdf0e10cSrcweir         throw io::NotConnectedException();
156*cdf0e10cSrcweir 
157*cdf0e10cSrcweir     return m_xInputStream->readSomeBytes( aData, nMaxBytesToRead );
158*cdf0e10cSrcweir }
159*cdf0e10cSrcweir 
160*cdf0e10cSrcweir void SAL_CALL SequenceInputStreamService::skipBytes( ::sal_Int32 nBytesToSkip ) throw ( uno::RuntimeException, io::NotConnectedException, io::BufferSizeExceededException, io::IOException )
161*cdf0e10cSrcweir {
162*cdf0e10cSrcweir     ::osl::MutexGuard aGuard( m_aMutex );
163*cdf0e10cSrcweir     if ( !m_xInputStream.is() )
164*cdf0e10cSrcweir         throw io::NotConnectedException();
165*cdf0e10cSrcweir 
166*cdf0e10cSrcweir     return m_xInputStream->skipBytes( nBytesToSkip );
167*cdf0e10cSrcweir }
168*cdf0e10cSrcweir 
169*cdf0e10cSrcweir ::sal_Int32 SAL_CALL SequenceInputStreamService::available() throw ( uno::RuntimeException, io::NotConnectedException, io::IOException )
170*cdf0e10cSrcweir {
171*cdf0e10cSrcweir     ::osl::MutexGuard aGuard( m_aMutex );
172*cdf0e10cSrcweir     if ( !m_xInputStream.is() )
173*cdf0e10cSrcweir         throw io::NotConnectedException();
174*cdf0e10cSrcweir 
175*cdf0e10cSrcweir     return m_xInputStream->available();
176*cdf0e10cSrcweir }
177*cdf0e10cSrcweir 
178*cdf0e10cSrcweir void SAL_CALL SequenceInputStreamService::closeInput() throw ( uno::RuntimeException, io::NotConnectedException, io::IOException )
179*cdf0e10cSrcweir {
180*cdf0e10cSrcweir     ::osl::MutexGuard aGuard( m_aMutex );
181*cdf0e10cSrcweir     if ( !m_xInputStream.is() )
182*cdf0e10cSrcweir         throw io::NotConnectedException();
183*cdf0e10cSrcweir 
184*cdf0e10cSrcweir     m_xInputStream->closeInput();
185*cdf0e10cSrcweir     m_xInputStream = uno::Reference< io::XInputStream >();
186*cdf0e10cSrcweir     m_xSeekable = uno::Reference< io::XSeekable >();
187*cdf0e10cSrcweir }
188*cdf0e10cSrcweir 
189*cdf0e10cSrcweir // ::com::sun::star::io::XSeekable:
190*cdf0e10cSrcweir void SAL_CALL SequenceInputStreamService::seek( ::sal_Int64 location ) throw ( uno::RuntimeException, lang::IllegalArgumentException, io::IOException )
191*cdf0e10cSrcweir {
192*cdf0e10cSrcweir     ::osl::MutexGuard aGuard( m_aMutex );
193*cdf0e10cSrcweir     if ( !m_xSeekable.is() )
194*cdf0e10cSrcweir         throw io::NotConnectedException();
195*cdf0e10cSrcweir 
196*cdf0e10cSrcweir     m_xSeekable->seek( location );
197*cdf0e10cSrcweir }
198*cdf0e10cSrcweir 
199*cdf0e10cSrcweir ::sal_Int64 SAL_CALL SequenceInputStreamService::getPosition() throw ( uno::RuntimeException, io::IOException )
200*cdf0e10cSrcweir {
201*cdf0e10cSrcweir     ::osl::MutexGuard aGuard( m_aMutex );
202*cdf0e10cSrcweir     if ( !m_xSeekable.is() )
203*cdf0e10cSrcweir         throw io::NotConnectedException();
204*cdf0e10cSrcweir 
205*cdf0e10cSrcweir     return m_xSeekable->getPosition();
206*cdf0e10cSrcweir }
207*cdf0e10cSrcweir 
208*cdf0e10cSrcweir ::sal_Int64 SAL_CALL SequenceInputStreamService::getLength() throw ( uno::RuntimeException, io::IOException )
209*cdf0e10cSrcweir {
210*cdf0e10cSrcweir     ::osl::MutexGuard aGuard( m_aMutex );
211*cdf0e10cSrcweir     if ( !m_xSeekable.is() )
212*cdf0e10cSrcweir         throw io::NotConnectedException();
213*cdf0e10cSrcweir 
214*cdf0e10cSrcweir     return m_xSeekable->getLength();
215*cdf0e10cSrcweir }
216*cdf0e10cSrcweir 
217*cdf0e10cSrcweir // ::com::sun::star::lang::XInitialization:
218*cdf0e10cSrcweir void SAL_CALL SequenceInputStreamService::initialize( const uno::Sequence< ::com::sun::star::uno::Any > & aArguments ) throw ( uno::RuntimeException, uno::Exception )
219*cdf0e10cSrcweir {
220*cdf0e10cSrcweir     ::osl::MutexGuard aGuard( m_aMutex );
221*cdf0e10cSrcweir     if ( m_bInitialized )
222*cdf0e10cSrcweir         throw frame::DoubleInitializationException();
223*cdf0e10cSrcweir 
224*cdf0e10cSrcweir     if ( aArguments.getLength() != 1 )
225*cdf0e10cSrcweir         throw lang::IllegalArgumentException( ::rtl::OUString::createFromAscii( "Wrong number of arguments!\n" ),
226*cdf0e10cSrcweir                                             uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ),
227*cdf0e10cSrcweir                                             1 );
228*cdf0e10cSrcweir 
229*cdf0e10cSrcweir     uno::Sequence< sal_Int8 > aSeq;
230*cdf0e10cSrcweir     if ( aArguments[0] >>= aSeq )
231*cdf0e10cSrcweir     {
232*cdf0e10cSrcweir         uno::Reference< io::XInputStream > xInputStream(
233*cdf0e10cSrcweir                         static_cast< ::cppu::OWeakObject* >( new ::comphelper::SequenceInputStream( aSeq ) ),
234*cdf0e10cSrcweir                         uno::UNO_QUERY_THROW );
235*cdf0e10cSrcweir         uno::Reference< io::XSeekable > xSeekable( xInputStream, uno::UNO_QUERY_THROW );
236*cdf0e10cSrcweir         m_xInputStream = xInputStream;
237*cdf0e10cSrcweir         m_xSeekable = xSeekable;
238*cdf0e10cSrcweir         m_bInitialized = sal_True;
239*cdf0e10cSrcweir     }
240*cdf0e10cSrcweir     else
241*cdf0e10cSrcweir         throw lang::IllegalArgumentException( ::rtl::OUString::createFromAscii( "Unexpected type of argument!\n" ),
242*cdf0e10cSrcweir                                             uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ),
243*cdf0e10cSrcweir                                             1 );
244*cdf0e10cSrcweir }
245*cdf0e10cSrcweir 
246*cdf0e10cSrcweir } // anonymous namespace
247*cdf0e10cSrcweir 
248*cdf0e10cSrcweir void createRegistryInfo_SequenceInputStream()
249*cdf0e10cSrcweir {
250*cdf0e10cSrcweir     static ::comphelper::module::OAutoRegistration< SequenceInputStreamService > aAutoRegistration;
251*cdf0e10cSrcweir }
252