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 ::rtl::OUString SAL_CALL getImplementationName() throw ( uno::RuntimeException );
52 : virtual ::sal_Bool SAL_CALL supportsService( const ::rtl::OUString & ServiceName ) throw ( uno::RuntimeException );
53 : virtual uno::Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames() throw ( uno::RuntimeException );
54 :
55 : // XServiceInfo - static versions (used for component registration)
56 : static ::rtl::OUString SAL_CALL getImplementationName_static();
57 : static uno::Sequence< ::rtl::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 0 : 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 0 : SequenceInputStreamService::SequenceInputStreamService()
89 0 : : m_bInitialized( sal_False )
90 0 : {}
91 :
92 : // com.sun.star.uno.XServiceInfo:
93 0 : ::rtl::OUString SAL_CALL SequenceInputStreamService::getImplementationName() throw ( uno::RuntimeException )
94 : {
95 0 : return getImplementationName_static();
96 : }
97 :
98 14 : ::rtl::OUString SAL_CALL SequenceInputStreamService::getImplementationName_static()
99 : {
100 14 : return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.comp.SequenceInputStreamService" ) );
101 : }
102 :
103 0 : ::sal_Bool SAL_CALL SequenceInputStreamService::supportsService( ::rtl::OUString const & serviceName ) throw ( uno::RuntimeException )
104 : {
105 0 : uno::Sequence< ::rtl::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< ::rtl::OUString > SAL_CALL SequenceInputStreamService::getSupportedServiceNames() throw ( uno::RuntimeException )
114 : {
115 0 : return getSupportedServiceNames_static();
116 : }
117 :
118 14 : uno::Sequence< ::rtl::OUString > SAL_CALL SequenceInputStreamService::getSupportedServiceNames_static()
119 : {
120 14 : uno::Sequence< ::rtl::OUString > s( 1 );
121 14 : s[0] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(
122 28 : "com.sun.star.io.SequenceInputStream" ) );
123 14 : return s;
124 : }
125 :
126 0 : uno::Reference< uno::XInterface > SAL_CALL SequenceInputStreamService::Create(
127 : SAL_UNUSED_PARAMETER const uno::Reference< uno::XComponentContext >& )
128 : {
129 0 : return static_cast< ::cppu::OWeakObject * >( new SequenceInputStreamService() );
130 : }
131 :
132 : // ::com::sun::star::io::XInputStream:
133 0 : ::sal_Int32 SAL_CALL SequenceInputStreamService::readBytes( uno::Sequence< ::sal_Int8 > & aData, ::sal_Int32 nBytesToRead ) throw ( uno::RuntimeException, io::NotConnectedException, io::BufferSizeExceededException, io::IOException )
134 : {
135 0 : ::osl::MutexGuard aGuard( m_aMutex );
136 0 : if ( !m_xInputStream.is() )
137 0 : throw io::NotConnectedException();
138 :
139 0 : return m_xInputStream->readBytes( aData, nBytesToRead );
140 : }
141 :
142 0 : ::sal_Int32 SAL_CALL SequenceInputStreamService::readSomeBytes( uno::Sequence< ::sal_Int8 > & aData, ::sal_Int32 nMaxBytesToRead ) throw ( uno::RuntimeException, io::NotConnectedException, io::BufferSizeExceededException, io::IOException )
143 : {
144 0 : ::osl::MutexGuard aGuard( m_aMutex );
145 0 : if ( !m_xInputStream.is() )
146 0 : throw io::NotConnectedException();
147 :
148 0 : return m_xInputStream->readSomeBytes( aData, nMaxBytesToRead );
149 : }
150 :
151 0 : void SAL_CALL SequenceInputStreamService::skipBytes( ::sal_Int32 nBytesToSkip ) throw ( uno::RuntimeException, io::NotConnectedException, io::BufferSizeExceededException, io::IOException )
152 : {
153 0 : ::osl::MutexGuard aGuard( m_aMutex );
154 0 : if ( !m_xInputStream.is() )
155 0 : throw io::NotConnectedException();
156 :
157 0 : return m_xInputStream->skipBytes( nBytesToSkip );
158 : }
159 :
160 0 : ::sal_Int32 SAL_CALL SequenceInputStreamService::available() throw ( uno::RuntimeException, io::NotConnectedException, io::IOException )
161 : {
162 0 : ::osl::MutexGuard aGuard( m_aMutex );
163 0 : if ( !m_xInputStream.is() )
164 0 : throw io::NotConnectedException();
165 :
166 0 : return m_xInputStream->available();
167 : }
168 :
169 0 : void SAL_CALL SequenceInputStreamService::closeInput() throw ( uno::RuntimeException, io::NotConnectedException, io::IOException )
170 : {
171 0 : ::osl::MutexGuard aGuard( m_aMutex );
172 0 : if ( !m_xInputStream.is() )
173 0 : throw io::NotConnectedException();
174 :
175 0 : m_xInputStream->closeInput();
176 0 : m_xInputStream = uno::Reference< io::XInputStream >();
177 0 : m_xSeekable = uno::Reference< io::XSeekable >();
178 0 : }
179 :
180 : // ::com::sun::star::io::XSeekable:
181 0 : void SAL_CALL SequenceInputStreamService::seek( ::sal_Int64 location ) throw ( uno::RuntimeException, lang::IllegalArgumentException, io::IOException )
182 : {
183 0 : ::osl::MutexGuard aGuard( m_aMutex );
184 0 : if ( !m_xSeekable.is() )
185 0 : throw io::NotConnectedException();
186 :
187 0 : m_xSeekable->seek( location );
188 0 : }
189 :
190 0 : ::sal_Int64 SAL_CALL SequenceInputStreamService::getPosition() throw ( uno::RuntimeException, io::IOException )
191 : {
192 0 : ::osl::MutexGuard aGuard( m_aMutex );
193 0 : if ( !m_xSeekable.is() )
194 0 : throw io::NotConnectedException();
195 :
196 0 : return m_xSeekable->getPosition();
197 : }
198 :
199 0 : ::sal_Int64 SAL_CALL SequenceInputStreamService::getLength() throw ( uno::RuntimeException, io::IOException )
200 : {
201 0 : ::osl::MutexGuard aGuard( m_aMutex );
202 0 : if ( !m_xSeekable.is() )
203 0 : throw io::NotConnectedException();
204 :
205 0 : return m_xSeekable->getLength();
206 : }
207 :
208 : // ::com::sun::star::lang::XInitialization:
209 0 : void SAL_CALL SequenceInputStreamService::initialize( const uno::Sequence< ::com::sun::star::uno::Any > & aArguments ) throw ( uno::RuntimeException, uno::Exception )
210 : {
211 0 : ::osl::MutexGuard aGuard( m_aMutex );
212 0 : if ( m_bInitialized )
213 0 : throw frame::DoubleInitializationException();
214 :
215 0 : if ( aArguments.getLength() != 1 )
216 : throw lang::IllegalArgumentException( ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Wrong number of arguments!\n")),
217 : uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ),
218 0 : 1 );
219 :
220 0 : uno::Sequence< sal_Int8 > aSeq;
221 0 : if ( aArguments[0] >>= aSeq )
222 : {
223 : uno::Reference< io::XInputStream > xInputStream(
224 0 : static_cast< ::cppu::OWeakObject* >( new ::comphelper::SequenceInputStream( aSeq ) ),
225 0 : uno::UNO_QUERY_THROW );
226 0 : uno::Reference< io::XSeekable > xSeekable( xInputStream, uno::UNO_QUERY_THROW );
227 0 : m_xInputStream = xInputStream;
228 0 : m_xSeekable = xSeekable;
229 0 : m_bInitialized = sal_True;
230 : }
231 : else
232 : throw lang::IllegalArgumentException( ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Unexpected type of argument!\n")),
233 : uno::Reference< uno::XInterface >( static_cast< ::cppu::OWeakObject* >(this) ),
234 0 : 1 );
235 0 : }
236 :
237 : } // anonymous namespace
238 :
239 14 : void createRegistryInfo_SequenceInputStream()
240 : {
241 14 : static ::comphelper::module::OAutoRegistration< SequenceInputStreamService > aAutoRegistration;
242 14 : }
243 :
244 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|