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