bump product version to 5.0.4.1
[LibreOffice.git] / comphelper / source / streaming / seqinputstreamserv.cxx
blob1ea60fa5a63f50df30fe4954912f4712ab06e32a
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 #include <sal/config.h>
22 #include "comphelper_module.hxx"
23 #include "comphelper_services.hxx"
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>
39 using namespace ::com::sun::star;
41 namespace {
43 class SequenceInputStreamService:
44 public ::cppu::WeakImplHelper3<
45 lang::XServiceInfo,
46 io::XSeekableInputStream,
47 lang::XInitialization>,
48 private boost::noncopyable
50 public:
51 explicit SequenceInputStreamService();
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;
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 >& );
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;
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;
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;
78 private:
79 virtual ~SequenceInputStreamService() {}
82 ::osl::Mutex m_aMutex;
83 bool m_bInitialized;
84 uno::Reference< io::XInputStream > m_xInputStream;
85 uno::Reference< io::XSeekable > m_xSeekable;
88 SequenceInputStreamService::SequenceInputStreamService()
89 : m_bInitialized( false )
92 // com.sun.star.uno.XServiceInfo:
93 OUString SAL_CALL SequenceInputStreamService::getImplementationName() throw ( uno::RuntimeException, std::exception )
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, std::exception )
105 return cppu::supportsService(this, serviceName);
108 uno::Sequence< OUString > SAL_CALL SequenceInputStreamService::getSupportedServiceNames() throw ( uno::RuntimeException, std::exception )
110 return getSupportedServiceNames_static();
113 uno::Sequence< OUString > SAL_CALL SequenceInputStreamService::getSupportedServiceNames_static()
115 uno::Sequence< OUString > s( 1 );
116 s[0] = "com.sun.star.io.SequenceInputStream";
117 return s;
120 uno::Reference< uno::XInterface > SAL_CALL SequenceInputStreamService::Create(
121 SAL_UNUSED_PARAMETER const uno::Reference< uno::XComponentContext >& )
123 return static_cast< ::cppu::OWeakObject * >( new SequenceInputStreamService() );
126 // ::com::sun::star::io::XInputStream:
127 ::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 )
129 ::osl::MutexGuard aGuard( m_aMutex );
130 if ( !m_xInputStream.is() )
131 throw io::NotConnectedException();
133 return m_xInputStream->readBytes( aData, nBytesToRead );
136 ::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 )
138 ::osl::MutexGuard aGuard( m_aMutex );
139 if ( !m_xInputStream.is() )
140 throw io::NotConnectedException();
142 return m_xInputStream->readSomeBytes( aData, nMaxBytesToRead );
145 void SAL_CALL SequenceInputStreamService::skipBytes( ::sal_Int32 nBytesToSkip ) throw ( uno::RuntimeException, io::NotConnectedException, io::BufferSizeExceededException, io::IOException, std::exception )
147 ::osl::MutexGuard aGuard( m_aMutex );
148 if ( !m_xInputStream.is() )
149 throw io::NotConnectedException();
151 return m_xInputStream->skipBytes( nBytesToSkip );
154 ::sal_Int32 SAL_CALL SequenceInputStreamService::available() throw ( uno::RuntimeException, io::NotConnectedException, io::IOException, std::exception )
156 ::osl::MutexGuard aGuard( m_aMutex );
157 if ( !m_xInputStream.is() )
158 throw io::NotConnectedException();
160 return m_xInputStream->available();
163 void SAL_CALL SequenceInputStreamService::closeInput() throw ( uno::RuntimeException, io::NotConnectedException, io::IOException, std::exception )
165 ::osl::MutexGuard aGuard( m_aMutex );
166 if ( !m_xInputStream.is() )
167 throw io::NotConnectedException();
169 m_xInputStream->closeInput();
170 m_xInputStream = uno::Reference< io::XInputStream >();
171 m_xSeekable = uno::Reference< io::XSeekable >();
174 // ::com::sun::star::io::XSeekable:
175 void SAL_CALL SequenceInputStreamService::seek( ::sal_Int64 location ) throw ( uno::RuntimeException, lang::IllegalArgumentException, io::IOException, std::exception )
177 ::osl::MutexGuard aGuard( m_aMutex );
178 if ( !m_xSeekable.is() )
179 throw io::NotConnectedException();
181 m_xSeekable->seek( location );
184 ::sal_Int64 SAL_CALL SequenceInputStreamService::getPosition() throw ( uno::RuntimeException, io::IOException, std::exception )
186 ::osl::MutexGuard aGuard( m_aMutex );
187 if ( !m_xSeekable.is() )
188 throw io::NotConnectedException();
190 return m_xSeekable->getPosition();
193 ::sal_Int64 SAL_CALL SequenceInputStreamService::getLength() throw ( uno::RuntimeException, io::IOException, std::exception )
195 ::osl::MutexGuard aGuard( m_aMutex );
196 if ( !m_xSeekable.is() )
197 throw io::NotConnectedException();
199 return m_xSeekable->getLength();
202 // ::com::sun::star::lang::XInitialization:
203 void SAL_CALL SequenceInputStreamService::initialize( const uno::Sequence< ::com::sun::star::uno::Any > & aArguments ) throw ( uno::RuntimeException, uno::Exception, std::exception )
205 ::osl::MutexGuard aGuard( m_aMutex );
206 if ( m_bInitialized )
207 throw frame::DoubleInitializationException();
209 if ( aArguments.getLength() != 1 )
210 throw lang::IllegalArgumentException( "Wrong number of arguments!",
211 static_cast< ::cppu::OWeakObject* >(this),
212 1 );
214 uno::Sequence< sal_Int8 > aSeq;
215 if ( aArguments[0] >>= aSeq )
217 uno::Reference< io::XInputStream > xInputStream(
218 static_cast< ::cppu::OWeakObject* >( new ::comphelper::SequenceInputStream( aSeq ) ),
219 uno::UNO_QUERY_THROW );
220 uno::Reference< io::XSeekable > xSeekable( xInputStream, uno::UNO_QUERY_THROW );
221 m_xInputStream = xInputStream;
222 m_xSeekable = xSeekable;
223 m_bInitialized = true;
225 else
226 throw lang::IllegalArgumentException( "Unexpected type of argument!",
227 static_cast< ::cppu::OWeakObject* >(this),
228 1 );
231 } // anonymous namespace
233 void createRegistryInfo_SequenceInputStream()
235 static ::comphelper::module::OAutoRegistration< SequenceInputStreamService > aAutoRegistration;
238 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */