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
25#include <com/sun/star/lang/XServiceInfo.hpp>
26#include <com/sun/star/io/NotConnectedException.hpp>
27#include <com/sun/star/io/XSequenceOutputStream.hpp>
28#include <mutex>
29
30namespace com::sun::star::uno { class XComponentContext; }
31
32using namespace ::com::sun::star;
33
34
35namespace {
36
37class SequenceOutputStreamService:
38 public cppu::WeakImplHelper<lang::XServiceInfo, io::XSequenceOutputStream>
39{
40public:
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
60private:
61 virtual ~SequenceOutputStreamService() override {};
62
63
65 // WARNING: dtor of m_xOutputStream writes into m_aSequence so that must live longer!
66 uno::Sequence< ::sal_Int8 > m_aSequence;
67 uno::Reference< io::XOutputStream > m_xOutputStream;
68};
69SequenceOutputStreamService::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:
75OUString SAL_CALL SequenceOutputStreamService::getImplementationName()
76{
77 return "com.sun.star.comp.SequenceOutputStreamService";
78}
79
80sal_Bool SAL_CALL SequenceOutputStreamService::supportsService( OUString const & serviceName )
81{
82 return cppu::supportsService(this, serviceName);
83}
84
85uno::Sequence< OUString > SAL_CALL SequenceOutputStreamService::getSupportedServiceNames()
86{
87 return { "com.sun.star.io.SequenceOutputStream" };
88}
89
90// css::io::XOutputStream:
91void 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
100void 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
109void 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:
121uno::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
136extern "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)
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 &)
unsigned char sal_Bool