1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 // MARKER( update_precomp.py ): autogen include statement, do not remove
22 #include "comphelper_module.hxx"
24 #include <sal/config.h>
25 #include <osl/mutex.hxx>
26 #include <cppuhelper/factory.hxx>
27 #include <cppuhelper/implementationentry.hxx>
28 #include <cppuhelper/implbase3.hxx>
29 #include <comphelper/seqstream.hxx>
30 #include <com/sun/star/lang/XServiceInfo.hpp>
31 #include <com/sun/star/io/XSeekableInputStream.hpp>
32 #include <com/sun/star/lang/XInitialization.hpp>
33 #include <com/sun/star/frame/DoubleInitializationException.hpp>
34 #include <com/sun/star/uno/XComponentContext.hpp>
37 using namespace ::com::sun::star
;
41 class SequenceInputStreamService
:
42 public ::cppu::WeakImplHelper3
<
44 io::XSeekableInputStream
,
45 lang::XInitialization
>
48 explicit SequenceInputStreamService();
50 // ::com::sun::star::lang::XServiceInfo:
51 virtual OUString SAL_CALL
getImplementationName() throw ( uno::RuntimeException
);
52 virtual ::sal_Bool SAL_CALL
supportsService( const OUString
& ServiceName
) throw ( uno::RuntimeException
);
53 virtual uno::Sequence
< OUString
> SAL_CALL
getSupportedServiceNames() throw ( uno::RuntimeException
);
55 // XServiceInfo - static versions (used for component registration)
56 static OUString SAL_CALL
getImplementationName_static();
57 static uno::Sequence
< OUString
> SAL_CALL
getSupportedServiceNames_static();
58 static uno::Reference
< uno::XInterface
> SAL_CALL
Create( const uno::Reference
< uno::XComponentContext
>& );
60 // ::com::sun::star::io::XInputStream:
61 virtual ::sal_Int32 SAL_CALL
readBytes( uno::Sequence
< ::sal_Int8
> & aData
, ::sal_Int32 nBytesToRead
) throw ( uno::RuntimeException
, io::NotConnectedException
, io::BufferSizeExceededException
, io::IOException
);
62 virtual ::sal_Int32 SAL_CALL
readSomeBytes( uno::Sequence
< ::sal_Int8
> & aData
, ::sal_Int32 nMaxBytesToRead
) throw ( uno::RuntimeException
, io::NotConnectedException
, io::BufferSizeExceededException
, io::IOException
);
63 virtual void SAL_CALL
skipBytes( ::sal_Int32 nBytesToSkip
) throw ( uno::RuntimeException
, io::NotConnectedException
, io::BufferSizeExceededException
, io::IOException
);
64 virtual ::sal_Int32 SAL_CALL
available() throw ( uno::RuntimeException
, io::NotConnectedException
, io::IOException
);
65 virtual void SAL_CALL
closeInput() throw ( uno::RuntimeException
, io::NotConnectedException
, io::IOException
);
67 // ::com::sun::star::io::XSeekable:
68 virtual void SAL_CALL
seek( ::sal_Int64 location
) throw ( uno::RuntimeException
, lang::IllegalArgumentException
, io::IOException
);
69 virtual ::sal_Int64 SAL_CALL
getPosition() throw ( uno::RuntimeException
, io::IOException
);
70 virtual ::sal_Int64 SAL_CALL
getLength() throw ( uno::RuntimeException
, io::IOException
);
72 // ::com::sun::star::lang::XInitialization:
73 virtual void SAL_CALL
initialize( const uno::Sequence
< ::com::sun::star::uno::Any
> & aArguments
) throw ( uno::RuntimeException
, uno::Exception
);
76 SequenceInputStreamService( SequenceInputStreamService
& ); // not defined
77 void operator =( SequenceInputStreamService
& ); // not defined
79 virtual ~SequenceInputStreamService() {}
82 ::osl::Mutex m_aMutex
;
83 sal_Bool m_bInitialized
;
84 uno::Reference
< io::XInputStream
> m_xInputStream
;
85 uno::Reference
< io::XSeekable
> m_xSeekable
;
88 SequenceInputStreamService::SequenceInputStreamService()
89 : m_bInitialized( sal_False
)
92 // com.sun.star.uno.XServiceInfo:
93 OUString SAL_CALL
SequenceInputStreamService::getImplementationName() throw ( uno::RuntimeException
)
95 return getImplementationName_static();
98 OUString SAL_CALL
SequenceInputStreamService::getImplementationName_static()
100 return OUString( "com.sun.star.comp.SequenceInputStreamService" );
103 ::sal_Bool SAL_CALL
SequenceInputStreamService::supportsService( OUString
const & serviceName
) throw ( uno::RuntimeException
)
105 uno::Sequence
< OUString
> serviceNames
= getSupportedServiceNames();
106 for ( ::sal_Int32 i
= 0; i
< serviceNames
.getLength(); ++i
) {
107 if ( serviceNames
[i
] == serviceName
)
113 uno::Sequence
< OUString
> SAL_CALL
SequenceInputStreamService::getSupportedServiceNames() throw ( uno::RuntimeException
)
115 return getSupportedServiceNames_static();
118 uno::Sequence
< OUString
> SAL_CALL
SequenceInputStreamService::getSupportedServiceNames_static()
120 uno::Sequence
< OUString
> s( 1 );
121 s
[0] = "com.sun.star.io.SequenceInputStream";
125 uno::Reference
< uno::XInterface
> SAL_CALL
SequenceInputStreamService::Create(
126 SAL_UNUSED_PARAMETER
const uno::Reference
< uno::XComponentContext
>& )
128 return static_cast< ::cppu::OWeakObject
* >( new SequenceInputStreamService() );
131 // ::com::sun::star::io::XInputStream:
132 ::sal_Int32 SAL_CALL
SequenceInputStreamService::readBytes( uno::Sequence
< ::sal_Int8
> & aData
, ::sal_Int32 nBytesToRead
) throw ( uno::RuntimeException
, io::NotConnectedException
, io::BufferSizeExceededException
, io::IOException
)
134 ::osl::MutexGuard
aGuard( m_aMutex
);
135 if ( !m_xInputStream
.is() )
136 throw io::NotConnectedException();
138 return m_xInputStream
->readBytes( aData
, nBytesToRead
);
141 ::sal_Int32 SAL_CALL
SequenceInputStreamService::readSomeBytes( uno::Sequence
< ::sal_Int8
> & aData
, ::sal_Int32 nMaxBytesToRead
) throw ( uno::RuntimeException
, io::NotConnectedException
, io::BufferSizeExceededException
, io::IOException
)
143 ::osl::MutexGuard
aGuard( m_aMutex
);
144 if ( !m_xInputStream
.is() )
145 throw io::NotConnectedException();
147 return m_xInputStream
->readSomeBytes( aData
, nMaxBytesToRead
);
150 void SAL_CALL
SequenceInputStreamService::skipBytes( ::sal_Int32 nBytesToSkip
) throw ( uno::RuntimeException
, io::NotConnectedException
, io::BufferSizeExceededException
, io::IOException
)
152 ::osl::MutexGuard
aGuard( m_aMutex
);
153 if ( !m_xInputStream
.is() )
154 throw io::NotConnectedException();
156 return m_xInputStream
->skipBytes( nBytesToSkip
);
159 ::sal_Int32 SAL_CALL
SequenceInputStreamService::available() throw ( uno::RuntimeException
, io::NotConnectedException
, io::IOException
)
161 ::osl::MutexGuard
aGuard( m_aMutex
);
162 if ( !m_xInputStream
.is() )
163 throw io::NotConnectedException();
165 return m_xInputStream
->available();
168 void SAL_CALL
SequenceInputStreamService::closeInput() throw ( uno::RuntimeException
, io::NotConnectedException
, io::IOException
)
170 ::osl::MutexGuard
aGuard( m_aMutex
);
171 if ( !m_xInputStream
.is() )
172 throw io::NotConnectedException();
174 m_xInputStream
->closeInput();
175 m_xInputStream
= uno::Reference
< io::XInputStream
>();
176 m_xSeekable
= uno::Reference
< io::XSeekable
>();
179 // ::com::sun::star::io::XSeekable:
180 void SAL_CALL
SequenceInputStreamService::seek( ::sal_Int64 location
) throw ( uno::RuntimeException
, lang::IllegalArgumentException
, io::IOException
)
182 ::osl::MutexGuard
aGuard( m_aMutex
);
183 if ( !m_xSeekable
.is() )
184 throw io::NotConnectedException();
186 m_xSeekable
->seek( location
);
189 ::sal_Int64 SAL_CALL
SequenceInputStreamService::getPosition() throw ( uno::RuntimeException
, io::IOException
)
191 ::osl::MutexGuard
aGuard( m_aMutex
);
192 if ( !m_xSeekable
.is() )
193 throw io::NotConnectedException();
195 return m_xSeekable
->getPosition();
198 ::sal_Int64 SAL_CALL
SequenceInputStreamService::getLength() throw ( uno::RuntimeException
, io::IOException
)
200 ::osl::MutexGuard
aGuard( m_aMutex
);
201 if ( !m_xSeekable
.is() )
202 throw io::NotConnectedException();
204 return m_xSeekable
->getLength();
207 // ::com::sun::star::lang::XInitialization:
208 void SAL_CALL
SequenceInputStreamService::initialize( const uno::Sequence
< ::com::sun::star::uno::Any
> & aArguments
) throw ( uno::RuntimeException
, uno::Exception
)
210 ::osl::MutexGuard
aGuard( m_aMutex
);
211 if ( m_bInitialized
)
212 throw frame::DoubleInitializationException();
214 if ( aArguments
.getLength() != 1 )
215 throw lang::IllegalArgumentException( OUString("Wrong number of arguments!\n"),
216 uno::Reference
< uno::XInterface
>( static_cast< ::cppu::OWeakObject
* >(this) ),
219 uno::Sequence
< sal_Int8
> aSeq
;
220 if ( aArguments
[0] >>= aSeq
)
222 uno::Reference
< io::XInputStream
> xInputStream(
223 static_cast< ::cppu::OWeakObject
* >( new ::comphelper::SequenceInputStream( aSeq
) ),
224 uno::UNO_QUERY_THROW
);
225 uno::Reference
< io::XSeekable
> xSeekable( xInputStream
, uno::UNO_QUERY_THROW
);
226 m_xInputStream
= xInputStream
;
227 m_xSeekable
= xSeekable
;
228 m_bInitialized
= sal_True
;
231 throw lang::IllegalArgumentException( OUString("Unexpected type of argument!\n"),
232 uno::Reference
< uno::XInterface
>( static_cast< ::cppu::OWeakObject
* >(this) ),
236 } // anonymous namespace
238 void createRegistryInfo_SequenceInputStream()
240 static ::comphelper::module::OAutoRegistration
< SequenceInputStreamService
> aAutoRegistration
;
243 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */