Version 6.1.4.1, tag libreoffice-6.1.4.1
[LibreOffice.git] / comphelper / source / streaming / seqinputstreamserv.cxx
blobfbf3cbab04b155f9ac60d44fab4c4dda0e3c5768
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 <osl/mutex.hxx>
26 #include <cppuhelper/factory.hxx>
27 #include <cppuhelper/implementationentry.hxx>
28 #include <cppuhelper/implbase.hxx>
29 #include <cppuhelper/supportsservice.hxx>
30 #include <comphelper/seqstream.hxx>
31 #include <com/sun/star/lang/IllegalArgumentException.hpp>
32 #include <com/sun/star/lang/XServiceInfo.hpp>
33 #include <com/sun/star/io/NotConnectedException.hpp>
34 #include <com/sun/star/io/XSeekableInputStream.hpp>
35 #include <com/sun/star/lang/XInitialization.hpp>
36 #include <com/sun/star/frame/DoubleInitializationException.hpp>
37 #include <com/sun/star/uno/XComponentContext.hpp>
40 using namespace ::com::sun::star;
42 namespace {
44 class SequenceInputStreamService:
45 public ::cppu::WeakImplHelper<
46 lang::XServiceInfo,
47 io::XSeekableInputStream,
48 lang::XInitialization>
50 public:
51 explicit SequenceInputStreamService();
53 // noncopyable
54 SequenceInputStreamService(const SequenceInputStreamService&) = delete;
55 const SequenceInputStreamService& operator=(const SequenceInputStreamService&) = delete;
57 // css::lang::XServiceInfo:
58 virtual OUString SAL_CALL getImplementationName() override;
59 virtual sal_Bool SAL_CALL supportsService( const OUString & ServiceName ) override;
60 virtual uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override;
62 // css::io::XInputStream:
63 virtual ::sal_Int32 SAL_CALL readBytes( uno::Sequence< ::sal_Int8 > & aData, ::sal_Int32 nBytesToRead ) override;
64 virtual ::sal_Int32 SAL_CALL readSomeBytes( uno::Sequence< ::sal_Int8 > & aData, ::sal_Int32 nMaxBytesToRead ) override;
65 virtual void SAL_CALL skipBytes( ::sal_Int32 nBytesToSkip ) override;
66 virtual ::sal_Int32 SAL_CALL available() override;
67 virtual void SAL_CALL closeInput() override;
69 // css::io::XSeekable:
70 virtual void SAL_CALL seek( ::sal_Int64 location ) override;
71 virtual ::sal_Int64 SAL_CALL getPosition() override;
72 virtual ::sal_Int64 SAL_CALL getLength() override;
74 // css::lang::XInitialization:
75 virtual void SAL_CALL initialize( const uno::Sequence< css::uno::Any > & aArguments ) override;
77 private:
78 virtual ~SequenceInputStreamService() override {}
81 ::osl::Mutex m_aMutex;
82 bool m_bInitialized;
83 uno::Reference< io::XInputStream > m_xInputStream;
84 uno::Reference< io::XSeekable > m_xSeekable;
87 SequenceInputStreamService::SequenceInputStreamService()
88 : m_bInitialized( false )
91 // com.sun.star.uno.XServiceInfo:
92 OUString SAL_CALL SequenceInputStreamService::getImplementationName()
94 return OUString ( "com.sun.star.comp.SequenceInputStreamService" );
97 sal_Bool SAL_CALL SequenceInputStreamService::supportsService( OUString const & serviceName )
99 return cppu::supportsService(this, serviceName);
102 uno::Sequence< OUString > SAL_CALL SequenceInputStreamService::getSupportedServiceNames()
104 uno::Sequence<OUString> s { "com.sun.star.io.SequenceInputStream" };
105 return s;
108 // css::io::XInputStream:
109 ::sal_Int32 SAL_CALL SequenceInputStreamService::readBytes( uno::Sequence< ::sal_Int8 > & aData, ::sal_Int32 nBytesToRead )
111 ::osl::MutexGuard aGuard( m_aMutex );
112 if ( !m_xInputStream.is() )
113 throw io::NotConnectedException();
115 return m_xInputStream->readBytes( aData, nBytesToRead );
118 ::sal_Int32 SAL_CALL SequenceInputStreamService::readSomeBytes( uno::Sequence< ::sal_Int8 > & aData, ::sal_Int32 nMaxBytesToRead )
120 ::osl::MutexGuard aGuard( m_aMutex );
121 if ( !m_xInputStream.is() )
122 throw io::NotConnectedException();
124 return m_xInputStream->readSomeBytes( aData, nMaxBytesToRead );
127 void SAL_CALL SequenceInputStreamService::skipBytes( ::sal_Int32 nBytesToSkip )
129 ::osl::MutexGuard aGuard( m_aMutex );
130 if ( !m_xInputStream.is() )
131 throw io::NotConnectedException();
133 return m_xInputStream->skipBytes( nBytesToSkip );
136 ::sal_Int32 SAL_CALL SequenceInputStreamService::available()
138 ::osl::MutexGuard aGuard( m_aMutex );
139 if ( !m_xInputStream.is() )
140 throw io::NotConnectedException();
142 return m_xInputStream->available();
145 void SAL_CALL SequenceInputStreamService::closeInput()
147 ::osl::MutexGuard aGuard( m_aMutex );
148 if ( !m_xInputStream.is() )
149 throw io::NotConnectedException();
151 m_xInputStream->closeInput();
152 m_xInputStream.clear();
153 m_xSeekable.clear();
156 // css::io::XSeekable:
157 void SAL_CALL SequenceInputStreamService::seek( ::sal_Int64 location )
159 ::osl::MutexGuard aGuard( m_aMutex );
160 if ( !m_xSeekable.is() )
161 throw io::NotConnectedException();
163 m_xSeekable->seek( location );
166 ::sal_Int64 SAL_CALL SequenceInputStreamService::getPosition()
168 ::osl::MutexGuard aGuard( m_aMutex );
169 if ( !m_xSeekable.is() )
170 throw io::NotConnectedException();
172 return m_xSeekable->getPosition();
175 ::sal_Int64 SAL_CALL SequenceInputStreamService::getLength()
177 ::osl::MutexGuard aGuard( m_aMutex );
178 if ( !m_xSeekable.is() )
179 throw io::NotConnectedException();
181 return m_xSeekable->getLength();
184 // css::lang::XInitialization:
185 void SAL_CALL SequenceInputStreamService::initialize( const uno::Sequence< css::uno::Any > & aArguments )
187 ::osl::MutexGuard aGuard( m_aMutex );
188 if ( m_bInitialized )
189 throw frame::DoubleInitializationException();
191 if ( aArguments.getLength() != 1 )
192 throw lang::IllegalArgumentException( "Wrong number of arguments!",
193 static_cast< ::cppu::OWeakObject* >(this),
194 1 );
196 uno::Sequence< sal_Int8 > aSeq;
197 if ( !(aArguments[0] >>= aSeq) )
198 throw lang::IllegalArgumentException( "Unexpected type of argument!",
199 static_cast< ::cppu::OWeakObject* >(this),
200 1 );
202 uno::Reference< io::XInputStream > xInputStream(
203 static_cast< ::cppu::OWeakObject* >( new ::comphelper::SequenceInputStream( aSeq ) ),
204 uno::UNO_QUERY_THROW );
205 uno::Reference< io::XSeekable > xSeekable( xInputStream, uno::UNO_QUERY_THROW );
206 m_xInputStream = xInputStream;
207 m_xSeekable = xSeekable;
208 m_bInitialized = true;
211 } // anonymous namespace
213 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
214 com_sun_star_comp_SequenceInputStreamService(
215 css::uno::XComponentContext *,
216 css::uno::Sequence<css::uno::Any> const &)
218 return cppu::acquire(new SequenceInputStreamService());
221 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */