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