Update git submodules
[LibreOffice.git] / comphelper / source / streaming / seqoutputstreamserv.cxx
blob144ba259a37d91784370fcba2fb4fac7f8752fac
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <sal/config.h>
22 #include <cppuhelper/implbase.hxx>
23 #include <cppuhelper/supportsservice.hxx>
24 #include <comphelper/seqstream.hxx>
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>
30 namespace com::sun::star::uno { class XComponentContext; }
32 using namespace ::com::sun::star;
35 namespace {
37 class SequenceOutputStreamService:
38 public cppu::WeakImplHelper<lang::XServiceInfo, io::XSequenceOutputStream>
40 public:
41 explicit SequenceOutputStreamService();
43 // noncopyable
44 SequenceOutputStreamService(const SequenceOutputStreamService&) = delete;
45 const SequenceOutputStreamService& operator=(const SequenceOutputStreamService&) = delete;
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;
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;
57 // css::io::XSequenceOutputStream:
58 virtual uno::Sequence< ::sal_Int8 > SAL_CALL getWrittenBytes( ) override;
60 private:
61 virtual ~SequenceOutputStreamService() override {};
64 std::mutex m_aMutex;
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;
69 SequenceOutputStreamService::SequenceOutputStreamService()
71 m_xOutputStream.set( static_cast < ::cppu::OWeakObject* >( new ::comphelper::OSequenceOutputStream( m_aSequence ) ), uno::UNO_QUERY_THROW );
74 // com.sun.star.uno.XServiceInfo:
75 OUString SAL_CALL SequenceOutputStreamService::getImplementationName()
77 return u"com.sun.star.comp.SequenceOutputStreamService"_ustr;
80 sal_Bool SAL_CALL SequenceOutputStreamService::supportsService( OUString const & serviceName )
82 return cppu::supportsService(this, serviceName);
85 uno::Sequence< OUString > SAL_CALL SequenceOutputStreamService::getSupportedServiceNames()
87 return { u"com.sun.star.io.SequenceOutputStream"_ustr };
90 // css::io::XOutputStream:
91 void SAL_CALL SequenceOutputStreamService::writeBytes( const uno::Sequence< ::sal_Int8 > & aData )
93 std::scoped_lock aGuard( m_aMutex );
94 if ( !m_xOutputStream.is() )
95 throw io::NotConnectedException();
97 m_xOutputStream->writeBytes( aData );
100 void SAL_CALL SequenceOutputStreamService::flush()
102 std::scoped_lock aGuard( m_aMutex );
103 if ( !m_xOutputStream.is() )
104 throw io::NotConnectedException();
106 m_xOutputStream->flush();
109 void SAL_CALL SequenceOutputStreamService::closeOutput()
111 std::scoped_lock aGuard( m_aMutex );
112 if ( !m_xOutputStream.is() )
113 throw io::NotConnectedException();
115 m_xOutputStream->flush();
116 m_xOutputStream->closeOutput();
117 m_xOutputStream.clear();
120 // css::io::XSequenceOutputStream:
121 uno::Sequence< ::sal_Int8 > SAL_CALL SequenceOutputStreamService::getWrittenBytes()
123 std::scoped_lock aGuard( m_aMutex );
125 if (m_xOutputStream.is())
127 m_xOutputStream->flush();
129 // else: no exception, just return the finished sequence
131 return m_aSequence;
134 } // anonymous namespace
136 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
137 com_sun_star_comp_SequenceOutputStreamService(
138 css::uno::XComponentContext *,
139 css::uno::Sequence<css::uno::Any> const &)
141 return cppu::acquire(new SequenceOutputStreamService());
144 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */