merged tag ooo/OOO330_m14
[LibreOffice.git] / comphelper / source / streaming / seqinputstreamserv.cxx
blob015387b9894d7742382f74a04bdbbba8624d165b
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
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.
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).
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.
26 ************************************************************************/
28 // MARKER( update_precomp.py ): autogen include statement, do not remove
29 #include "precompiled_comphelper.hxx"
31 #include "comphelper_module.hxx"
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>
46 using namespace ::com::sun::star;
48 namespace {
50 class SequenceInputStreamService:
51 public ::cppu::WeakImplHelper3<
52 lang::XServiceInfo,
53 io::XSeekableInputStream,
54 lang::XInitialization>
56 public:
57 explicit SequenceInputStreamService();
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 );
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 >& );
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 );
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 );
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 );
84 private:
85 SequenceInputStreamService( SequenceInputStreamService & ); // not defined
86 void operator =( SequenceInputStreamService & ); // not defined
88 virtual ~SequenceInputStreamService() {}
91 ::osl::Mutex m_aMutex;
92 sal_Bool m_bInitialized;
93 uno::Reference< io::XInputStream > m_xInputStream;
94 uno::Reference< io::XSeekable > m_xSeekable;
97 SequenceInputStreamService::SequenceInputStreamService()
98 : m_bInitialized( sal_False )
101 // com.sun.star.uno.XServiceInfo:
102 ::rtl::OUString SAL_CALL SequenceInputStreamService::getImplementationName() throw ( uno::RuntimeException )
104 return getImplementationName_static();
107 ::rtl::OUString SAL_CALL SequenceInputStreamService::getImplementationName_static()
109 return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.comp.SequenceInputStreamService" ) );
112 ::sal_Bool SAL_CALL SequenceInputStreamService::supportsService( ::rtl::OUString const & serviceName ) throw ( uno::RuntimeException )
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;
119 return sal_False;
122 uno::Sequence< ::rtl::OUString > SAL_CALL SequenceInputStreamService::getSupportedServiceNames() throw ( uno::RuntimeException )
124 return getSupportedServiceNames_static();
127 uno::Sequence< ::rtl::OUString > SAL_CALL SequenceInputStreamService::getSupportedServiceNames_static()
129 uno::Sequence< ::rtl::OUString > s( 1 );
130 s[0] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(
131 "com.sun.star.io.SequenceInputStream" ) );
132 return s;
135 uno::Reference< uno::XInterface > SAL_CALL SequenceInputStreamService::Create(
136 const uno::Reference< uno::XComponentContext >& )
138 return static_cast< ::cppu::OWeakObject * >( new SequenceInputStreamService() );
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 )
144 ::osl::MutexGuard aGuard( m_aMutex );
145 if ( !m_xInputStream.is() )
146 throw io::NotConnectedException();
148 return m_xInputStream->readBytes( aData, nBytesToRead );
151 ::sal_Int32 SAL_CALL SequenceInputStreamService::readSomeBytes( uno::Sequence< ::sal_Int8 > & aData, ::sal_Int32 nMaxBytesToRead ) throw ( uno::RuntimeException, io::NotConnectedException, io::BufferSizeExceededException, io::IOException )
153 ::osl::MutexGuard aGuard( m_aMutex );
154 if ( !m_xInputStream.is() )
155 throw io::NotConnectedException();
157 return m_xInputStream->readSomeBytes( aData, nMaxBytesToRead );
160 void SAL_CALL SequenceInputStreamService::skipBytes( ::sal_Int32 nBytesToSkip ) throw ( uno::RuntimeException, io::NotConnectedException, io::BufferSizeExceededException, io::IOException )
162 ::osl::MutexGuard aGuard( m_aMutex );
163 if ( !m_xInputStream.is() )
164 throw io::NotConnectedException();
166 return m_xInputStream->skipBytes( nBytesToSkip );
169 ::sal_Int32 SAL_CALL SequenceInputStreamService::available() throw ( uno::RuntimeException, io::NotConnectedException, io::IOException )
171 ::osl::MutexGuard aGuard( m_aMutex );
172 if ( !m_xInputStream.is() )
173 throw io::NotConnectedException();
175 return m_xInputStream->available();
178 void SAL_CALL SequenceInputStreamService::closeInput() throw ( uno::RuntimeException, io::NotConnectedException, io::IOException )
180 ::osl::MutexGuard aGuard( m_aMutex );
181 if ( !m_xInputStream.is() )
182 throw io::NotConnectedException();
184 m_xInputStream->closeInput();
185 m_xInputStream = uno::Reference< io::XInputStream >();
186 m_xSeekable = uno::Reference< io::XSeekable >();
189 // ::com::sun::star::io::XSeekable:
190 void SAL_CALL SequenceInputStreamService::seek( ::sal_Int64 location ) throw ( uno::RuntimeException, lang::IllegalArgumentException, io::IOException )
192 ::osl::MutexGuard aGuard( m_aMutex );
193 if ( !m_xSeekable.is() )
194 throw io::NotConnectedException();
196 m_xSeekable->seek( location );
199 ::sal_Int64 SAL_CALL SequenceInputStreamService::getPosition() throw ( uno::RuntimeException, io::IOException )
201 ::osl::MutexGuard aGuard( m_aMutex );
202 if ( !m_xSeekable.is() )
203 throw io::NotConnectedException();
205 return m_xSeekable->getPosition();
208 ::sal_Int64 SAL_CALL SequenceInputStreamService::getLength() throw ( uno::RuntimeException, io::IOException )
210 ::osl::MutexGuard aGuard( m_aMutex );
211 if ( !m_xSeekable.is() )
212 throw io::NotConnectedException();
214 return m_xSeekable->getLength();
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 )
220 ::osl::MutexGuard aGuard( m_aMutex );
221 if ( m_bInitialized )
222 throw frame::DoubleInitializationException();
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 );
229 uno::Sequence< sal_Int8 > aSeq;
230 if ( aArguments[0] >>= aSeq )
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;
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 );
246 } // anonymous namespace
248 void createRegistryInfo_SequenceInputStream()
250 static ::comphelper::module::OAutoRegistration< SequenceInputStreamService > aAutoRegistration;