1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: seqinputstreamserv.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER( update_precomp.py ): autogen include statement, do not remove
32 #include "precompiled_comphelper.hxx"
34 #include "comphelper_module.hxx"
36 #include <sal/config.h>
37 #include <osl/mutex.hxx>
38 #include <cppuhelper/factory.hxx>
39 #include <cppuhelper/implementationentry.hxx>
40 #include <cppuhelper/implbase3.hxx>
41 #include <comphelper/seqstream.hxx>
42 #include <com/sun/star/lang/XServiceInfo.hpp>
43 #include <com/sun/star/io/XSeekableInputStream.hpp>
44 #include <com/sun/star/lang/XInitialization.hpp>
45 #include <com/sun/star/frame/DoubleInitializationException.hpp>
46 #include <com/sun/star/uno/XComponentContext.hpp>
49 using namespace ::com::sun::star
;
53 class SequenceInputStreamService
:
54 public ::cppu::WeakImplHelper3
<
56 io::XSeekableInputStream
,
57 lang::XInitialization
>
60 explicit SequenceInputStreamService();
62 // ::com::sun::star::lang::XServiceInfo:
63 virtual ::rtl::OUString SAL_CALL
getImplementationName() throw ( uno::RuntimeException
);
64 virtual ::sal_Bool SAL_CALL
supportsService( const ::rtl::OUString
& ServiceName
) throw ( uno::RuntimeException
);
65 virtual uno::Sequence
< ::rtl::OUString
> SAL_CALL
getSupportedServiceNames() throw ( uno::RuntimeException
);
67 // XServiceInfo - static versions (used for component registration)
68 static ::rtl::OUString SAL_CALL
getImplementationName_static();
69 static uno::Sequence
< ::rtl::OUString
> SAL_CALL
getSupportedServiceNames_static();
70 static uno::Reference
< uno::XInterface
> SAL_CALL
Create( const uno::Reference
< uno::XComponentContext
>& );
72 // ::com::sun::star::io::XInputStream:
73 virtual ::sal_Int32 SAL_CALL
readBytes( uno::Sequence
< ::sal_Int8
> & aData
, ::sal_Int32 nBytesToRead
) throw ( uno::RuntimeException
, io::NotConnectedException
, io::BufferSizeExceededException
, io::IOException
);
74 virtual ::sal_Int32 SAL_CALL
readSomeBytes( uno::Sequence
< ::sal_Int8
> & aData
, ::sal_Int32 nMaxBytesToRead
) throw ( uno::RuntimeException
, io::NotConnectedException
, io::BufferSizeExceededException
, io::IOException
);
75 virtual void SAL_CALL
skipBytes( ::sal_Int32 nBytesToSkip
) throw ( uno::RuntimeException
, io::NotConnectedException
, io::BufferSizeExceededException
, io::IOException
);
76 virtual ::sal_Int32 SAL_CALL
available() throw ( uno::RuntimeException
, io::NotConnectedException
, io::IOException
);
77 virtual void SAL_CALL
closeInput() throw ( uno::RuntimeException
, io::NotConnectedException
, io::IOException
);
79 // ::com::sun::star::io::XSeekable:
80 virtual void SAL_CALL
seek( ::sal_Int64 location
) throw ( uno::RuntimeException
, lang::IllegalArgumentException
, io::IOException
);
81 virtual ::sal_Int64 SAL_CALL
getPosition() throw ( uno::RuntimeException
, io::IOException
);
82 virtual ::sal_Int64 SAL_CALL
getLength() throw ( uno::RuntimeException
, io::IOException
);
84 // ::com::sun::star::lang::XInitialization:
85 virtual void SAL_CALL
initialize( const uno::Sequence
< ::com::sun::star::uno::Any
> & aArguments
) throw ( uno::RuntimeException
, uno::Exception
);
88 SequenceInputStreamService( SequenceInputStreamService
& ); // not defined
89 void operator =( SequenceInputStreamService
& ); // not defined
91 virtual ~SequenceInputStreamService() {}
94 ::osl::Mutex m_aMutex
;
95 sal_Bool m_bInitialized
;
96 uno::Reference
< io::XInputStream
> m_xInputStream
;
97 uno::Reference
< io::XSeekable
> m_xSeekable
;
100 SequenceInputStreamService::SequenceInputStreamService()
101 : m_bInitialized( sal_False
)
104 // com.sun.star.uno.XServiceInfo:
105 ::rtl::OUString SAL_CALL
SequenceInputStreamService::getImplementationName() throw ( uno::RuntimeException
)
107 return getImplementationName_static();
110 ::rtl::OUString SAL_CALL
SequenceInputStreamService::getImplementationName_static()
112 return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.comp.SequenceInputStreamService" ) );
115 ::sal_Bool SAL_CALL
SequenceInputStreamService::supportsService( ::rtl::OUString
const & serviceName
) throw ( uno::RuntimeException
)
117 uno::Sequence
< ::rtl::OUString
> serviceNames
= getSupportedServiceNames();
118 for ( ::sal_Int32 i
= 0; i
< serviceNames
.getLength(); ++i
) {
119 if ( serviceNames
[i
] == serviceName
)
125 uno::Sequence
< ::rtl::OUString
> SAL_CALL
SequenceInputStreamService::getSupportedServiceNames() throw ( uno::RuntimeException
)
127 return getSupportedServiceNames_static();
130 uno::Sequence
< ::rtl::OUString
> SAL_CALL
SequenceInputStreamService::getSupportedServiceNames_static()
132 uno::Sequence
< ::rtl::OUString
> s( 1 );
133 s
[0] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(
134 "com.sun.star.io.SequenceInputStream" ) );
138 uno::Reference
< uno::XInterface
> SAL_CALL
SequenceInputStreamService::Create(
139 const uno::Reference
< uno::XComponentContext
>& )
141 return static_cast< ::cppu::OWeakObject
* >( new SequenceInputStreamService() );
144 // ::com::sun::star::io::XInputStream:
145 ::sal_Int32 SAL_CALL
SequenceInputStreamService::readBytes( uno::Sequence
< ::sal_Int8
> & aData
, ::sal_Int32 nBytesToRead
) throw ( uno::RuntimeException
, io::NotConnectedException
, io::BufferSizeExceededException
, io::IOException
)
147 ::osl::MutexGuard
aGuard( m_aMutex
);
148 if ( !m_xInputStream
.is() )
149 throw io::NotConnectedException();
151 return m_xInputStream
->readBytes( aData
, nBytesToRead
);
154 ::sal_Int32 SAL_CALL
SequenceInputStreamService::readSomeBytes( uno::Sequence
< ::sal_Int8
> & aData
, ::sal_Int32 nMaxBytesToRead
) throw ( uno::RuntimeException
, io::NotConnectedException
, io::BufferSizeExceededException
, io::IOException
)
156 ::osl::MutexGuard
aGuard( m_aMutex
);
157 if ( !m_xInputStream
.is() )
158 throw io::NotConnectedException();
160 return m_xInputStream
->readSomeBytes( aData
, nMaxBytesToRead
);
163 void SAL_CALL
SequenceInputStreamService::skipBytes( ::sal_Int32 nBytesToSkip
) throw ( uno::RuntimeException
, io::NotConnectedException
, io::BufferSizeExceededException
, io::IOException
)
165 ::osl::MutexGuard
aGuard( m_aMutex
);
166 if ( !m_xInputStream
.is() )
167 throw io::NotConnectedException();
169 return m_xInputStream
->skipBytes( nBytesToSkip
);
172 ::sal_Int32 SAL_CALL
SequenceInputStreamService::available() throw ( uno::RuntimeException
, io::NotConnectedException
, io::IOException
)
174 ::osl::MutexGuard
aGuard( m_aMutex
);
175 if ( !m_xInputStream
.is() )
176 throw io::NotConnectedException();
178 return m_xInputStream
->available();
181 void SAL_CALL
SequenceInputStreamService::closeInput() throw ( uno::RuntimeException
, io::NotConnectedException
, io::IOException
)
183 ::osl::MutexGuard
aGuard( m_aMutex
);
184 if ( !m_xInputStream
.is() )
185 throw io::NotConnectedException();
187 m_xInputStream
->closeInput();
188 m_xInputStream
= uno::Reference
< io::XInputStream
>();
189 m_xSeekable
= uno::Reference
< io::XSeekable
>();
192 // ::com::sun::star::io::XSeekable:
193 void SAL_CALL
SequenceInputStreamService::seek( ::sal_Int64 location
) throw ( uno::RuntimeException
, lang::IllegalArgumentException
, io::IOException
)
195 ::osl::MutexGuard
aGuard( m_aMutex
);
196 if ( !m_xSeekable
.is() )
197 throw io::NotConnectedException();
199 m_xSeekable
->seek( location
);
202 ::sal_Int64 SAL_CALL
SequenceInputStreamService::getPosition() throw ( uno::RuntimeException
, io::IOException
)
204 ::osl::MutexGuard
aGuard( m_aMutex
);
205 if ( !m_xSeekable
.is() )
206 throw io::NotConnectedException();
208 return m_xSeekable
->getPosition();
211 ::sal_Int64 SAL_CALL
SequenceInputStreamService::getLength() throw ( uno::RuntimeException
, io::IOException
)
213 ::osl::MutexGuard
aGuard( m_aMutex
);
214 if ( !m_xSeekable
.is() )
215 throw io::NotConnectedException();
217 return m_xSeekable
->getLength();
220 // ::com::sun::star::lang::XInitialization:
221 void SAL_CALL
SequenceInputStreamService::initialize( const uno::Sequence
< ::com::sun::star::uno::Any
> & aArguments
) throw ( uno::RuntimeException
, uno::Exception
)
223 ::osl::MutexGuard
aGuard( m_aMutex
);
224 if ( m_bInitialized
)
225 throw frame::DoubleInitializationException();
227 if ( aArguments
.getLength() != 1 )
228 throw lang::IllegalArgumentException( ::rtl::OUString::createFromAscii( "Wrong number of arguments!\n" ),
229 uno::Reference
< uno::XInterface
>( static_cast< ::cppu::OWeakObject
* >(this) ),
232 uno::Sequence
< sal_Int8
> aSeq
;
233 if ( aArguments
[0] >>= aSeq
)
235 uno::Reference
< io::XInputStream
> xInputStream(
236 static_cast< ::cppu::OWeakObject
* >( new ::comphelper::SequenceInputStream( aSeq
) ),
237 uno::UNO_QUERY_THROW
);
238 uno::Reference
< io::XSeekable
> xSeekable( xInputStream
, uno::UNO_QUERY_THROW
);
239 m_xInputStream
= xInputStream
;
240 m_xSeekable
= xSeekable
;
241 m_bInitialized
= sal_True
;
244 throw lang::IllegalArgumentException( ::rtl::OUString::createFromAscii( "Unexpected type of argument!\n" ),
245 uno::Reference
< uno::XInterface
>( static_cast< ::cppu::OWeakObject
* >(this) ),
249 } // anonymous namespace
251 void createRegistryInfo_SequenceInputStream()
253 static ::comphelper::module::OAutoRegistration
< SequenceInputStreamService
> aAutoRegistration
;