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 : #include <sal/config.h>
21 :
22 : #include "comphelper_module.hxx"
23 : #include "comphelper_services.hxx"
24 :
25 : #include <boost/noncopyable.hpp>
26 : #include <osl/mutex.hxx>
27 : #include <cppuhelper/factory.hxx>
28 : #include <cppuhelper/implementationentry.hxx>
29 : #include <cppuhelper/implbase3.hxx>
30 : #include <cppuhelper/supportsservice.hxx>
31 : #include <comphelper/seqstream.hxx>
32 : #include <com/sun/star/lang/XServiceInfo.hpp>
33 : #include <com/sun/star/io/XSeekableInputStream.hpp>
34 : #include <com/sun/star/lang/XInitialization.hpp>
35 : #include <com/sun/star/frame/DoubleInitializationException.hpp>
36 : #include <com/sun/star/uno/XComponentContext.hpp>
37 :
38 :
39 : using namespace ::com::sun::star;
40 :
41 : namespace {
42 :
43 : class SequenceInputStreamService:
44 : public ::cppu::WeakImplHelper3<
45 : lang::XServiceInfo,
46 : io::XSeekableInputStream,
47 : lang::XInitialization>,
48 : private boost::noncopyable
49 : {
50 : public:
51 : explicit SequenceInputStreamService();
52 :
53 : // ::com::sun::star::lang::XServiceInfo:
54 : virtual OUString SAL_CALL getImplementationName() throw ( uno::RuntimeException, std::exception ) SAL_OVERRIDE;
55 : virtual sal_Bool SAL_CALL supportsService( const OUString & ServiceName ) throw ( uno::RuntimeException, std::exception ) SAL_OVERRIDE;
56 : virtual uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() throw ( uno::RuntimeException, std::exception ) SAL_OVERRIDE;
57 :
58 : // XServiceInfo - static versions (used for component registration)
59 : static OUString SAL_CALL getImplementationName_static();
60 : static uno::Sequence< OUString > SAL_CALL getSupportedServiceNames_static();
61 : static uno::Reference< uno::XInterface > SAL_CALL Create( const uno::Reference< uno::XComponentContext >& );
62 :
63 : // ::com::sun::star::io::XInputStream:
64 : virtual ::sal_Int32 SAL_CALL readBytes( uno::Sequence< ::sal_Int8 > & aData, ::sal_Int32 nBytesToRead ) throw ( uno::RuntimeException, io::NotConnectedException, io::BufferSizeExceededException, io::IOException, std::exception ) SAL_OVERRIDE;
65 : virtual ::sal_Int32 SAL_CALL readSomeBytes( uno::Sequence< ::sal_Int8 > & aData, ::sal_Int32 nMaxBytesToRead ) throw ( uno::RuntimeException, io::NotConnectedException, io::BufferSizeExceededException, io::IOException, std::exception ) SAL_OVERRIDE;
66 : virtual void SAL_CALL skipBytes( ::sal_Int32 nBytesToSkip ) throw ( uno::RuntimeException, io::NotConnectedException, io::BufferSizeExceededException, io::IOException, std::exception ) SAL_OVERRIDE;
67 : virtual ::sal_Int32 SAL_CALL available() throw ( uno::RuntimeException, io::NotConnectedException, io::IOException, std::exception ) SAL_OVERRIDE;
68 : virtual void SAL_CALL closeInput() throw ( uno::RuntimeException, io::NotConnectedException, io::IOException, std::exception ) SAL_OVERRIDE;
69 :
70 : // ::com::sun::star::io::XSeekable:
71 : virtual void SAL_CALL seek( ::sal_Int64 location ) throw ( uno::RuntimeException, lang::IllegalArgumentException, io::IOException, std::exception ) SAL_OVERRIDE;
72 : virtual ::sal_Int64 SAL_CALL getPosition() throw ( uno::RuntimeException, io::IOException, std::exception ) SAL_OVERRIDE;
73 : virtual ::sal_Int64 SAL_CALL getLength() throw ( uno::RuntimeException, io::IOException, std::exception ) SAL_OVERRIDE;
74 :
75 : // ::com::sun::star::lang::XInitialization:
76 : virtual void SAL_CALL initialize( const uno::Sequence< ::com::sun::star::uno::Any > & aArguments ) throw ( uno::RuntimeException, uno::Exception, std::exception ) SAL_OVERRIDE;
77 :
78 : private:
79 20 : virtual ~SequenceInputStreamService() {}
80 :
81 :
82 : ::osl::Mutex m_aMutex;
83 : bool m_bInitialized;
84 : uno::Reference< io::XInputStream > m_xInputStream;
85 : uno::Reference< io::XSeekable > m_xSeekable;
86 : };
87 :
88 10 : SequenceInputStreamService::SequenceInputStreamService()
89 10 : : m_bInitialized( false )
90 10 : {}
91 :
92 : // com.sun.star.uno.XServiceInfo:
93 0 : OUString SAL_CALL SequenceInputStreamService::getImplementationName() throw ( uno::RuntimeException, std::exception )
94 : {
95 0 : return getImplementationName_static();
96 : }
97 :
98 284 : OUString SAL_CALL SequenceInputStreamService::getImplementationName_static()
99 : {
100 284 : return OUString( "com.sun.star.comp.SequenceInputStreamService" );
101 : }
102 :
103 0 : sal_Bool SAL_CALL SequenceInputStreamService::supportsService( OUString const & serviceName ) throw ( uno::RuntimeException, std::exception )
104 : {
105 0 : return cppu::supportsService(this, serviceName);
106 : }
107 :
108 0 : uno::Sequence< OUString > SAL_CALL SequenceInputStreamService::getSupportedServiceNames() throw ( uno::RuntimeException, std::exception )
109 : {
110 0 : return getSupportedServiceNames_static();
111 : }
112 :
113 284 : uno::Sequence< OUString > SAL_CALL SequenceInputStreamService::getSupportedServiceNames_static()
114 : {
115 284 : uno::Sequence< OUString > s( 1 );
116 284 : s[0] = "com.sun.star.io.SequenceInputStream";
117 284 : return s;
118 : }
119 :
120 10 : uno::Reference< uno::XInterface > SAL_CALL SequenceInputStreamService::Create(
121 : SAL_UNUSED_PARAMETER const uno::Reference< uno::XComponentContext >& )
122 : {
123 10 : return static_cast< ::cppu::OWeakObject * >( new SequenceInputStreamService() );
124 : }
125 :
126 : // ::com::sun::star::io::XInputStream:
127 6 : ::sal_Int32 SAL_CALL SequenceInputStreamService::readBytes( uno::Sequence< ::sal_Int8 > & aData, ::sal_Int32 nBytesToRead ) throw ( uno::RuntimeException, io::NotConnectedException, io::BufferSizeExceededException, io::IOException, std::exception )
128 : {
129 6 : ::osl::MutexGuard aGuard( m_aMutex );
130 6 : if ( !m_xInputStream.is() )
131 0 : throw io::NotConnectedException();
132 :
133 6 : return m_xInputStream->readBytes( aData, nBytesToRead );
134 : }
135 :
136 0 : ::sal_Int32 SAL_CALL SequenceInputStreamService::readSomeBytes( uno::Sequence< ::sal_Int8 > & aData, ::sal_Int32 nMaxBytesToRead ) throw ( uno::RuntimeException, io::NotConnectedException, io::BufferSizeExceededException, io::IOException, std::exception )
137 : {
138 0 : ::osl::MutexGuard aGuard( m_aMutex );
139 0 : if ( !m_xInputStream.is() )
140 0 : throw io::NotConnectedException();
141 :
142 0 : return m_xInputStream->readSomeBytes( aData, nMaxBytesToRead );
143 : }
144 :
145 0 : void SAL_CALL SequenceInputStreamService::skipBytes( ::sal_Int32 nBytesToSkip ) throw ( uno::RuntimeException, io::NotConnectedException, io::BufferSizeExceededException, io::IOException, std::exception )
146 : {
147 0 : ::osl::MutexGuard aGuard( m_aMutex );
148 0 : if ( !m_xInputStream.is() )
149 0 : throw io::NotConnectedException();
150 :
151 0 : return m_xInputStream->skipBytes( nBytesToSkip );
152 : }
153 :
154 0 : ::sal_Int32 SAL_CALL SequenceInputStreamService::available() throw ( uno::RuntimeException, io::NotConnectedException, io::IOException, std::exception )
155 : {
156 0 : ::osl::MutexGuard aGuard( m_aMutex );
157 0 : if ( !m_xInputStream.is() )
158 0 : throw io::NotConnectedException();
159 :
160 0 : return m_xInputStream->available();
161 : }
162 :
163 2 : void SAL_CALL SequenceInputStreamService::closeInput() throw ( uno::RuntimeException, io::NotConnectedException, io::IOException, std::exception )
164 : {
165 2 : ::osl::MutexGuard aGuard( m_aMutex );
166 2 : if ( !m_xInputStream.is() )
167 0 : throw io::NotConnectedException();
168 :
169 2 : m_xInputStream->closeInput();
170 2 : m_xInputStream = uno::Reference< io::XInputStream >();
171 2 : m_xSeekable = uno::Reference< io::XSeekable >();
172 2 : }
173 :
174 : // ::com::sun::star::io::XSeekable:
175 0 : void SAL_CALL SequenceInputStreamService::seek( ::sal_Int64 location ) throw ( uno::RuntimeException, lang::IllegalArgumentException, io::IOException, std::exception )
176 : {
177 0 : ::osl::MutexGuard aGuard( m_aMutex );
178 0 : if ( !m_xSeekable.is() )
179 0 : throw io::NotConnectedException();
180 :
181 0 : m_xSeekable->seek( location );
182 0 : }
183 :
184 0 : ::sal_Int64 SAL_CALL SequenceInputStreamService::getPosition() throw ( uno::RuntimeException, io::IOException, std::exception )
185 : {
186 0 : ::osl::MutexGuard aGuard( m_aMutex );
187 0 : if ( !m_xSeekable.is() )
188 0 : throw io::NotConnectedException();
189 :
190 0 : return m_xSeekable->getPosition();
191 : }
192 :
193 0 : ::sal_Int64 SAL_CALL SequenceInputStreamService::getLength() throw ( uno::RuntimeException, io::IOException, std::exception )
194 : {
195 0 : ::osl::MutexGuard aGuard( m_aMutex );
196 0 : if ( !m_xSeekable.is() )
197 0 : throw io::NotConnectedException();
198 :
199 0 : return m_xSeekable->getLength();
200 : }
201 :
202 : // ::com::sun::star::lang::XInitialization:
203 10 : void SAL_CALL SequenceInputStreamService::initialize( const uno::Sequence< ::com::sun::star::uno::Any > & aArguments ) throw ( uno::RuntimeException, uno::Exception, std::exception )
204 : {
205 10 : ::osl::MutexGuard aGuard( m_aMutex );
206 10 : if ( m_bInitialized )
207 0 : throw frame::DoubleInitializationException();
208 :
209 10 : if ( aArguments.getLength() != 1 )
210 : throw lang::IllegalArgumentException( "Wrong number of arguments!",
211 : static_cast< ::cppu::OWeakObject* >(this),
212 0 : 1 );
213 :
214 20 : uno::Sequence< sal_Int8 > aSeq;
215 10 : if ( aArguments[0] >>= aSeq )
216 : {
217 : uno::Reference< io::XInputStream > xInputStream(
218 10 : static_cast< ::cppu::OWeakObject* >( new ::comphelper::SequenceInputStream( aSeq ) ),
219 20 : uno::UNO_QUERY_THROW );
220 20 : uno::Reference< io::XSeekable > xSeekable( xInputStream, uno::UNO_QUERY_THROW );
221 10 : m_xInputStream = xInputStream;
222 10 : m_xSeekable = xSeekable;
223 20 : m_bInitialized = true;
224 : }
225 : else
226 : throw lang::IllegalArgumentException( "Unexpected type of argument!",
227 : static_cast< ::cppu::OWeakObject* >(this),
228 10 : 1 );
229 10 : }
230 :
231 : } // anonymous namespace
232 :
233 284 : void createRegistryInfo_SequenceInputStream()
234 : {
235 284 : static ::comphelper::module::OAutoRegistration< SequenceInputStreamService > aAutoRegistration;
236 284 : }
237 :
238 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|