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