LibreOffice Module comphelper (master)  1
seqoutputstreamserv.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/XServiceInfo.hpp>
27 #include <com/sun/star/io/NotConnectedException.hpp>
28 #include <com/sun/star/io/XSequenceOutputStream.hpp>
29 #include <mutex>
30 
31 namespace com::sun::star::uno { class XComponentContext; }
32 
33 using namespace ::com::sun::star;
34 
35 
36 namespace {
37 
38 class SequenceOutputStreamService:
39  public cppu::WeakImplHelper<lang::XServiceInfo, io::XSequenceOutputStream>
40 {
41 public:
42  explicit SequenceOutputStreamService();
43 
44  // noncopyable
45  SequenceOutputStreamService(const SequenceOutputStreamService&) = delete;
46  const SequenceOutputStreamService& operator=(const SequenceOutputStreamService&) = delete;
47 
48  // css::lang::XServiceInfo:
49  virtual OUString SAL_CALL getImplementationName() override;
50  virtual sal_Bool SAL_CALL supportsService( const OUString & ServiceName ) override;
51  virtual uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override;
52 
53  // css::io::XOutputStream:
54  virtual void SAL_CALL writeBytes( const uno::Sequence< ::sal_Int8 > & aData ) override;
55  virtual void SAL_CALL flush() override;
56  virtual void SAL_CALL closeOutput() override;
57 
58  // css::io::XSequenceOutputStream:
59  virtual uno::Sequence< ::sal_Int8 > SAL_CALL getWrittenBytes( ) override;
60 
61 private:
62  virtual ~SequenceOutputStreamService() override {};
63 
64 
66  uno::Reference< io::XOutputStream > m_xOutputStream;
67  uno::Sequence< ::sal_Int8 > m_aSequence;
68 };
69 SequenceOutputStreamService::SequenceOutputStreamService()
70 {
71  m_xOutputStream.set( static_cast < ::cppu::OWeakObject* >( new ::comphelper::OSequenceOutputStream( m_aSequence ) ), uno::UNO_QUERY_THROW );
72 }
73 
74 // com.sun.star.uno.XServiceInfo:
75 OUString SAL_CALL SequenceOutputStreamService::getImplementationName()
76 {
77  return "com.sun.star.comp.SequenceOutputStreamService";
78 }
79 
80 sal_Bool SAL_CALL SequenceOutputStreamService::supportsService( OUString const & serviceName )
81 {
82  return cppu::supportsService(this, serviceName);
83 }
84 
85 uno::Sequence< OUString > SAL_CALL SequenceOutputStreamService::getSupportedServiceNames()
86 {
87  return { "com.sun.star.io.SequenceOutputStream" };
88 }
89 
90 // css::io::XOutputStream:
91 void SAL_CALL SequenceOutputStreamService::writeBytes( const uno::Sequence< ::sal_Int8 > & aData )
92 {
93  std::scoped_lock aGuard( m_aMutex );
94  if ( !m_xOutputStream.is() )
95  throw io::NotConnectedException();
96 
97  m_xOutputStream->writeBytes( aData );
98 }
99 
100 void SAL_CALL SequenceOutputStreamService::flush()
101 {
102  std::scoped_lock aGuard( m_aMutex );
103  if ( !m_xOutputStream.is() )
104  throw io::NotConnectedException();
105 
106  m_xOutputStream->flush();
107 };
108 
109 void SAL_CALL SequenceOutputStreamService::closeOutput()
110 {
111  std::scoped_lock aGuard( m_aMutex );
112  if ( !m_xOutputStream.is() )
113  throw io::NotConnectedException();
114 
115  m_xOutputStream->flush();
116  m_xOutputStream->closeOutput();
117  m_xOutputStream.clear();
118 }
119 
120 // css::io::XSequenceOutputStream:
121 uno::Sequence< ::sal_Int8 > SAL_CALL SequenceOutputStreamService::getWrittenBytes()
122 {
123  std::scoped_lock aGuard( m_aMutex );
124 
125  if (m_xOutputStream.is())
126  {
127  m_xOutputStream->flush();
128  }
129  // else: no exception, just return the finished sequence
130 
131  return m_aSequence;
132 }
133 
134 } // anonymous namespace
135 
136 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
138  css::uno::XComponentContext *,
139  css::uno::Sequence<css::uno::Any> const &)
140 {
141  return cppu::acquire(new SequenceOutputStreamService());
142 }
143 
144 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
::osl::Mutex m_aMutex
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
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * com_sun_star_comp_SequenceOutputStreamService(css::uno::XComponentContext *, css::uno::Sequence< css::uno::Any > const &)