1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <comphelper_module.hxx>
23 #include <comphelper_services.hxx>
25 #include <osl/mutex.hxx>
26 #include <cppuhelper/factory.hxx>
27 #include <cppuhelper/implementationentry.hxx>
28 #include <cppuhelper/implbase.hxx>
29 #include <cppuhelper/supportsservice.hxx>
30 #include <comphelper/seqstream.hxx>
31 #include <com/sun/star/lang/XServiceInfo.hpp>
32 #include <com/sun/star/io/NotConnectedException.hpp>
33 #include <com/sun/star/io/XSequenceOutputStream.hpp>
34 #include <com/sun/star/uno/XComponentContext.hpp>
36 using namespace ::com::sun::star
;
41 class SequenceOutputStreamService
:
42 public cppu::WeakImplHelper
<lang::XServiceInfo
, io::XSequenceOutputStream
>
45 explicit SequenceOutputStreamService();
48 SequenceOutputStreamService(const SequenceOutputStreamService
&) = delete;
49 const SequenceOutputStreamService
& operator=(const SequenceOutputStreamService
&) = delete;
51 // css::lang::XServiceInfo:
52 virtual OUString SAL_CALL
getImplementationName() override
;
53 virtual sal_Bool SAL_CALL
supportsService( const OUString
& ServiceName
) override
;
54 virtual uno::Sequence
< OUString
> SAL_CALL
getSupportedServiceNames() override
;
56 // css::io::XOutputStream:
57 virtual void SAL_CALL
writeBytes( const uno::Sequence
< ::sal_Int8
> & aData
) override
;
58 virtual void SAL_CALL
flush() override
;
59 virtual void SAL_CALL
closeOutput() override
;
61 // css::io::XSequenceOutputStream:
62 virtual uno::Sequence
< ::sal_Int8
> SAL_CALL
getWrittenBytes( ) override
;
65 virtual ~SequenceOutputStreamService() override
{};
68 ::osl::Mutex m_aMutex
;
69 uno::Reference
< io::XOutputStream
> m_xOutputStream
;
70 uno::Sequence
< ::sal_Int8
> m_aSequence
;
72 SequenceOutputStreamService::SequenceOutputStreamService()
74 m_xOutputStream
.set( static_cast < ::cppu::OWeakObject
* >( new ::comphelper::OSequenceOutputStream( m_aSequence
) ), uno::UNO_QUERY_THROW
);
77 // com.sun.star.uno.XServiceInfo:
78 OUString SAL_CALL
SequenceOutputStreamService::getImplementationName()
80 return OUString("com.sun.star.comp.SequenceOutputStreamService");
83 sal_Bool SAL_CALL
SequenceOutputStreamService::supportsService( OUString
const & serviceName
)
85 return cppu::supportsService(this, serviceName
);
88 uno::Sequence
< OUString
> SAL_CALL
SequenceOutputStreamService::getSupportedServiceNames()
90 uno::Sequence
<OUString
> s
{ "com.sun.star.io.SequenceOutputStream" };
94 // css::io::XOutputStream:
95 void SAL_CALL
SequenceOutputStreamService::writeBytes( const uno::Sequence
< ::sal_Int8
> & aData
)
97 ::osl::MutexGuard
aGuard( m_aMutex
);
98 if ( !m_xOutputStream
.is() )
99 throw io::NotConnectedException();
101 m_xOutputStream
->writeBytes( aData
);
105 void SAL_CALL
SequenceOutputStreamService::flush()
107 ::osl::MutexGuard
aGuard( m_aMutex
);
108 if ( !m_xOutputStream
.is() )
109 throw io::NotConnectedException();
111 m_xOutputStream
->flush();
114 void SAL_CALL
SequenceOutputStreamService::closeOutput()
116 ::osl::MutexGuard
aGuard( m_aMutex
);
117 if ( !m_xOutputStream
.is() )
118 throw io::NotConnectedException();
120 m_xOutputStream
->closeOutput();
121 m_xOutputStream
.clear();
124 // css::io::XSequenceOutputStream:
125 uno::Sequence
< ::sal_Int8
> SAL_CALL
SequenceOutputStreamService::getWrittenBytes()
127 ::osl::MutexGuard
aGuard( m_aMutex
);
128 if ( !m_xOutputStream
.is() )
129 throw io::NotConnectedException();
131 m_xOutputStream
->flush();
135 } // anonymous namespace
137 extern "C" SAL_DLLPUBLIC_EXPORT
css::uno::XInterface
*
138 com_sun_star_comp_SequenceOutputStreamService(
139 css::uno::XComponentContext
*,
140 css::uno::Sequence
<css::uno::Any
> const &)
142 return cppu::acquire(new SequenceOutputStreamService());
145 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */