LibreOffice Module comphelper (master)  1
seqinputstreamserv.cxx
Go to the documentation of this file.
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 <osl/mutex.hxx>
23 #include <cppuhelper/implbase.hxx>
25 #include <comphelper/seqstream.hxx>
26 #include <com/sun/star/lang/IllegalArgumentException.hpp>
27 #include <com/sun/star/lang/XServiceInfo.hpp>
28 #include <com/sun/star/io/NotConnectedException.hpp>
29 #include <com/sun/star/io/XSeekableInputStream.hpp>
30 #include <com/sun/star/lang/XInitialization.hpp>
31 #include <com/sun/star/frame/DoubleInitializationException.hpp>
32 
33 namespace com::sun::star::uno { class XComponentContext; }
34 
35 using namespace ::com::sun::star;
36 
37 namespace {
38 
39 class SequenceInputStreamService:
40  public ::cppu::WeakImplHelper<
41  lang::XServiceInfo,
42  io::XSeekableInputStream,
43  lang::XInitialization>
44 {
45 public:
46  explicit SequenceInputStreamService();
47 
48  // noncopyable
49  SequenceInputStreamService(const SequenceInputStreamService&) = delete;
50  const SequenceInputStreamService& operator=(const SequenceInputStreamService&) = delete;
51 
52  // css::lang::XServiceInfo:
53  virtual OUString SAL_CALL getImplementationName() override;
54  virtual sal_Bool SAL_CALL supportsService( const OUString & ServiceName ) override;
55  virtual uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override;
56 
57  // css::io::XInputStream:
58  virtual ::sal_Int32 SAL_CALL readBytes( uno::Sequence< ::sal_Int8 > & aData, ::sal_Int32 nBytesToRead ) override;
59  virtual ::sal_Int32 SAL_CALL readSomeBytes( uno::Sequence< ::sal_Int8 > & aData, ::sal_Int32 nMaxBytesToRead ) override;
60  virtual void SAL_CALL skipBytes( ::sal_Int32 nBytesToSkip ) override;
61  virtual ::sal_Int32 SAL_CALL available() override;
62  virtual void SAL_CALL closeInput() override;
63 
64  // css::io::XSeekable:
65  virtual void SAL_CALL seek( ::sal_Int64 location ) override;
66  virtual ::sal_Int64 SAL_CALL getPosition() override;
67  virtual ::sal_Int64 SAL_CALL getLength() override;
68 
69  // css::lang::XInitialization:
70  virtual void SAL_CALL initialize( const uno::Sequence< css::uno::Any > & aArguments ) override;
71 
72 private:
73  virtual ~SequenceInputStreamService() override {}
74 
75 
76  ::osl::Mutex m_aMutex;
77  bool m_bInitialized;
78  uno::Reference< io::XInputStream > m_xInputStream;
79  uno::Reference< io::XSeekable > m_xSeekable;
80 };
81 
82 SequenceInputStreamService::SequenceInputStreamService()
83 : m_bInitialized( false )
84 {}
85 
86 // com.sun.star.uno.XServiceInfo:
87 OUString SAL_CALL SequenceInputStreamService::getImplementationName()
88 {
89  return "com.sun.star.comp.SequenceInputStreamService";
90 }
91 
92 sal_Bool SAL_CALL SequenceInputStreamService::supportsService( OUString const & serviceName )
93 {
94  return cppu::supportsService(this, serviceName);
95 }
96 
97 uno::Sequence< OUString > SAL_CALL SequenceInputStreamService::getSupportedServiceNames()
98 {
99  return { "com.sun.star.io.SequenceInputStream" };
100 }
101 
102 // css::io::XInputStream:
103 ::sal_Int32 SAL_CALL SequenceInputStreamService::readBytes( uno::Sequence< ::sal_Int8 > & aData, ::sal_Int32 nBytesToRead )
104 {
105  ::osl::MutexGuard aGuard( m_aMutex );
106  if ( !m_xInputStream.is() )
107  throw io::NotConnectedException();
108 
109  return m_xInputStream->readBytes( aData, nBytesToRead );
110 }
111 
112 ::sal_Int32 SAL_CALL SequenceInputStreamService::readSomeBytes( uno::Sequence< ::sal_Int8 > & aData, ::sal_Int32 nMaxBytesToRead )
113 {
114  ::osl::MutexGuard aGuard( m_aMutex );
115  if ( !m_xInputStream.is() )
116  throw io::NotConnectedException();
117 
118  return m_xInputStream->readSomeBytes( aData, nMaxBytesToRead );
119 }
120 
121 void SAL_CALL SequenceInputStreamService::skipBytes( ::sal_Int32 nBytesToSkip )
122 {
123  ::osl::MutexGuard aGuard( m_aMutex );
124  if ( !m_xInputStream.is() )
125  throw io::NotConnectedException();
126 
127  return m_xInputStream->skipBytes( nBytesToSkip );
128 }
129 
130 ::sal_Int32 SAL_CALL SequenceInputStreamService::available()
131 {
132  ::osl::MutexGuard aGuard( m_aMutex );
133  if ( !m_xInputStream.is() )
134  throw io::NotConnectedException();
135 
136  return m_xInputStream->available();
137 }
138 
139 void SAL_CALL SequenceInputStreamService::closeInput()
140 {
141  ::osl::MutexGuard aGuard( m_aMutex );
142  if ( !m_xInputStream.is() )
143  throw io::NotConnectedException();
144 
145  m_xInputStream->closeInput();
146  m_xInputStream.clear();
147  m_xSeekable.clear();
148 }
149 
150 // css::io::XSeekable:
151 void SAL_CALL SequenceInputStreamService::seek( ::sal_Int64 location )
152 {
153  ::osl::MutexGuard aGuard( m_aMutex );
154  if ( !m_xSeekable.is() )
155  throw io::NotConnectedException();
156 
157  m_xSeekable->seek( location );
158 }
159 
160 ::sal_Int64 SAL_CALL SequenceInputStreamService::getPosition()
161 {
162  ::osl::MutexGuard aGuard( m_aMutex );
163  if ( !m_xSeekable.is() )
164  throw io::NotConnectedException();
165 
166  return m_xSeekable->getPosition();
167 }
168 
169 ::sal_Int64 SAL_CALL SequenceInputStreamService::getLength()
170 {
171  ::osl::MutexGuard aGuard( m_aMutex );
172  if ( !m_xSeekable.is() )
173  throw io::NotConnectedException();
174 
175  return m_xSeekable->getLength();
176 }
177 
178 // css::lang::XInitialization:
179 void SAL_CALL SequenceInputStreamService::initialize( const uno::Sequence< css::uno::Any > & aArguments )
180 {
181  ::osl::MutexGuard aGuard( m_aMutex );
182  if ( m_bInitialized )
183  throw frame::DoubleInitializationException();
184 
185  if ( aArguments.getLength() != 1 )
186  throw lang::IllegalArgumentException( "Wrong number of arguments!",
187  static_cast< ::cppu::OWeakObject* >(this),
188  1 );
189 
190  uno::Sequence< sal_Int8 > aSeq;
191  if ( !(aArguments[0] >>= aSeq) )
192  throw lang::IllegalArgumentException( "Unexpected type of argument!",
193  static_cast< ::cppu::OWeakObject* >(this),
194  1 );
195 
196  uno::Reference< io::XInputStream > xInputStream(
197  static_cast< ::cppu::OWeakObject* >( new ::comphelper::SequenceInputStream( aSeq ) ),
198  uno::UNO_QUERY_THROW );
199  uno::Reference< io::XSeekable > xSeekable( xInputStream, uno::UNO_QUERY_THROW );
200  m_xInputStream = xInputStream;
201  m_xSeekable = xSeekable;
202  m_bInitialized = true;
203 }
204 
205 } // anonymous namespace
206 
207 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
209  css::uno::XComponentContext *,
210  css::uno::Sequence<css::uno::Any> const &)
211 {
212  return cppu::acquire(new SequenceInputStreamService());
213 }
214 
215 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
::osl::Mutex m_aMutex
uno::Reference< io::XSeekable > m_xSeekable
css::uno::Sequence< OUString > getSupportedServiceNames()
OUString getImplementationName()
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
unsigned char sal_Bool
Sequence< sal_Int8 > aSeq
double getLength(const B2DPolygon &rCandidate)
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * com_sun_star_comp_SequenceInputStreamService(css::uno::XComponentContext *, css::uno::Sequence< css::uno::Any > const &)