Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : // MARKER( update_precomp.py ): autogen include statement, do not remove
21 :
22 : #include "comphelper_module.hxx"
23 :
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>
35 :
36 :
37 : using namespace ::com::sun::star;
38 :
39 : namespace {
40 :
41 : class SequenceInputStreamService:
42 : public ::cppu::WeakImplHelper3<
43 : lang::XServiceInfo,
44 : io::XSeekableInputStream,
45 : lang::XInitialization>
46 : {
47 : public:
48 : explicit SequenceInputStreamService();
49 :
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 );
54 :
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 >& );
59 :
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 );
66 :
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 );
71 :
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 );
74 :
75 : private:
76 : SequenceInputStreamService( SequenceInputStreamService & ); // not defined
77 : void operator =( SequenceInputStreamService & ); // not defined
78 :
79 8 : virtual ~SequenceInputStreamService() {}
80 :
81 :
82 : ::osl::Mutex m_aMutex;
83 : sal_Bool m_bInitialized;
84 : uno::Reference< io::XInputStream > m_xInputStream;
85 : uno::Reference< io::XSeekable > m_xSeekable;
86 : };
87 :
88 4 : SequenceInputStreamService::SequenceInputStreamService()
89 4 : : m_bInitialized( sal_False )
90 4 : {}
91 :
92 : // com.sun.star.uno.XServiceInfo:
93 0 : OUString SAL_CALL SequenceInputStreamService::getImplementationName() throw ( uno::RuntimeException )
94 : {
95 0 : return getImplementationName_static();
96 : }
97 :
98 119 : OUString SAL_CALL SequenceInputStreamService::getImplementationName_static()
99 : {
100 119 : return OUString( "com.sun.star.comp.SequenceInputStreamService" );
101 : }
102 :
103 0 : ::sal_Bool SAL_CALL SequenceInputStreamService::supportsService( OUString const & serviceName ) throw ( uno::RuntimeException )
104 : {
105 0 : uno::Sequence< OUString > serviceNames = getSupportedServiceNames();
106 0 : for ( ::sal_Int32 i = 0; i < serviceNames.getLength(); ++i ) {
107 0 : if ( serviceNames[i] == serviceName )
108 0 : return sal_True;
109 : }
110 0 : return sal_False;
111 : }
112 :
113 0 : uno::Sequence< OUString > SAL_CALL SequenceInputStreamService::getSupportedServiceNames() throw ( uno::RuntimeException )
114 : {
115 0 : return getSupportedServiceNames_static();
116 : }
117 :
118 119 : uno::Sequence< OUString > SAL_CALL SequenceInputStreamService::getSupportedServiceNames_static()
119 : {
120 119 : uno::Sequence< OUString > s( 1 );
121 119 : s[0] = "com.sun.star.io.SequenceInputStream";
122 119 : return s;
123 : }
124 :
125 4 : uno::Reference< uno::XInterface > SAL_CALL SequenceInputStreamService::Create(
126 : SAL_UNUSED_PARAMETER const uno::Reference< uno::XComponentContext >& )
127 : {
128 4 : return static_cast< ::cppu::OWeakObject * >( new SequenceInputStreamService() );
129 : }
130 :
131 : // ::com::sun::star::io::XInputStream:
132 1 : ::sal_Int32 SAL_CALL SequenceInputStreamService::readBytes( uno::Sequence< ::sal_Int8 > & aData, ::sal_Int32 nBytesToRead ) throw ( uno::RuntimeException, io::NotConnectedException, io::BufferSizeExceededException, io::IOException )
133 : {
134 1 : ::osl::MutexGuard aGuard( m_aMutex );
135 1 : if ( !m_xInputStream.is() )
136 0 : throw io::NotConnectedException();
137 :
138 1 : return m_xInputStream->readBytes( aData, nBytesToRead );
139 : }
140 :
141 0 : ::sal_Int32 SAL_CALL SequenceInputStreamService::readSomeBytes( uno::Sequence< ::sal_Int8 > & aData, ::sal_Int32 nMaxBytesToRead ) throw ( uno::RuntimeException, io::NotConnectedException, io::BufferSizeExceededException, io::IOException )
142 : {
143 0 : ::osl::MutexGuard aGuard( m_aMutex );
144 0 : if ( !m_xInputStream.is() )
145 0 : throw io::NotConnectedException();
146 :
147 0 : return m_xInputStream->readSomeBytes( aData, nMaxBytesToRead );
148 : }
149 :
150 0 : void SAL_CALL SequenceInputStreamService::skipBytes( ::sal_Int32 nBytesToSkip ) throw ( uno::RuntimeException, io::NotConnectedException, io::BufferSizeExceededException, io::IOException )
151 : {
152 0 : ::osl::MutexGuard aGuard( m_aMutex );
153 0 : if ( !m_xInputStream.is() )
154 0 : throw io::NotConnectedException();
155 :
156 0 : return m_xInputStream->skipBytes( nBytesToSkip );
157 : }
158 :
159 0 : ::sal_Int32 SAL_CALL SequenceInputStreamService::available() throw ( uno::RuntimeException, io::NotConnectedException, io::IOException )
160 : {
161 0 : ::osl::MutexGuard aGuard( m_aMutex );
162 0 : if ( !m_xInputStream.is() )
163 0 : throw io::NotConnectedException();
164 :
165 0 : return m_xInputStream->available();
166 : }
167 :
168 1 : void SAL_CALL SequenceInputStreamService::closeInput() throw ( uno::RuntimeException, io::NotConnectedException, io::IOException )
169 : {
170 1 : ::osl::MutexGuard aGuard( m_aMutex );
171 1 : if ( !m_xInputStream.is() )
172 0 : throw io::NotConnectedException();
173 :
174 1 : m_xInputStream->closeInput();
175 1 : m_xInputStream = uno::Reference< io::XInputStream >();
176 1 : m_xSeekable = uno::Reference< io::XSeekable >();
177 1 : }
178 :
179 : // ::com::sun::star::io::XSeekable:
180 0 : void SAL_CALL SequenceInputStreamService::seek( ::sal_Int64 location ) throw ( uno::RuntimeException, lang::IllegalArgumentException, io::IOException )
181 : {
182 0 : ::osl::MutexGuard aGuard( m_aMutex );
183 0 : if ( !m_xSeekable.is() )
184 0 : throw io::NotConnectedException();
185 :
186 0 : m_xSeekable->seek( location );
187 0 : }
188 :
189 0 : ::sal_Int64 SAL_CALL SequenceInputStreamService::getPosition() throw ( uno::RuntimeException, io::IOException )
190 : {
191 0 : ::osl::MutexGuard aGuard( m_aMutex );
192 0 : if ( !m_xSeekable.is() )
193 0 : throw io::NotConnectedException();
194 :
195 0 : return m_xSeekable->getPosition();
196 : }
197 :
198 0 : ::sal_Int64 SAL_CALL SequenceInputStreamService::getLength() throw ( uno::RuntimeException, io::IOException )
199 : {
200 0 : ::osl::MutexGuard aGuard( m_aMutex );
201 0 : if ( !m_xSeekable.is() )
202 0 : throw io::NotConnectedException();
203 :
204 0 : return m_xSeekable->getLength();
205 : }
206 :
207 : // ::com::sun::star::lang::XInitialization:
208 4 : void SAL_CALL SequenceInputStreamService::initialize( const uno::Sequence< ::com::sun::star::uno::Any > & aArguments ) throw ( uno::RuntimeException, uno::Exception )
209 : {
210 4 : ::osl::MutexGuard aGuard( m_aMutex );
211 4 : if ( m_bInitialized )
212 0 : throw frame::DoubleInitializationException();
213 :
214 4 : if ( aArguments.getLength() != 1 )
215 : throw lang::IllegalArgumentException( OUString("Wrong number of arguments!\n"),
216 : uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ),
217 0 : 1 );
218 :
219 8 : uno::Sequence< sal_Int8 > aSeq;
220 4 : if ( aArguments[0] >>= aSeq )
221 : {
222 : uno::Reference< io::XInputStream > xInputStream(
223 4 : static_cast< ::cppu::OWeakObject* >( new ::comphelper::SequenceInputStream( aSeq ) ),
224 8 : uno::UNO_QUERY_THROW );
225 8 : uno::Reference< io::XSeekable > xSeekable( xInputStream, uno::UNO_QUERY_THROW );
226 4 : m_xInputStream = xInputStream;
227 4 : m_xSeekable = xSeekable;
228 8 : m_bInitialized = sal_True;
229 : }
230 : else
231 : throw lang::IllegalArgumentException( OUString("Unexpected type of argument!\n"),
232 : uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ),
233 4 : 1 );
234 4 : }
235 :
236 : } // anonymous namespace
237 :
238 119 : void createRegistryInfo_SequenceInputStream()
239 : {
240 119 : static ::comphelper::module::OAutoRegistration< SequenceInputStreamService > aAutoRegistration;
241 119 : }
242 :
243 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|