Line data Source code
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 :
21 : #include "comphelper_module.hxx"
22 :
23 : #include <sal/config.h>
24 : #include <osl/mutex.hxx>
25 : #include <cppuhelper/factory.hxx>
26 : #include <cppuhelper/implementationentry.hxx>
27 : #include <cppuhelper/implbase2.hxx>
28 : #include <comphelper/seqstream.hxx>
29 : #include <com/sun/star/lang/XServiceInfo.hpp>
30 : #include <com/sun/star/io/XSequenceOutputStream.hpp>
31 : #include <com/sun/star/uno/XComponentContext.hpp>
32 :
33 : using namespace ::com::sun::star;
34 :
35 :
36 : namespace {
37 :
38 : class SequenceOutputStreamService:
39 : public ::cppu::WeakImplHelper2 < lang::XServiceInfo, io::XSequenceOutputStream >
40 : {
41 : public:
42 : explicit SequenceOutputStreamService();
43 :
44 : // ::com::sun::star::lang::XServiceInfo:
45 : virtual ::rtl::OUString SAL_CALL getImplementationName() throw ( uno::RuntimeException );
46 : virtual ::sal_Bool SAL_CALL supportsService( const ::rtl::OUString & ServiceName ) throw ( uno::RuntimeException );
47 : virtual uno::Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames() throw ( uno::RuntimeException );
48 :
49 : // XServiceInfo - static versions (used for component registration)
50 : static ::rtl::OUString SAL_CALL getImplementationName_static();
51 : static uno::Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames_static();
52 : static uno::Reference< uno::XInterface > SAL_CALL Create( const uno::Reference< uno::XComponentContext >& );
53 :
54 : // ::com::sun::star::io::XOutputStream:
55 : virtual void SAL_CALL writeBytes( const uno::Sequence< ::sal_Int8 > & aData ) throw ( io::NotConnectedException, io::BufferSizeExceededException, io::IOException, uno::RuntimeException );
56 : virtual void SAL_CALL flush() throw ( uno::RuntimeException, io::NotConnectedException, io::BufferSizeExceededException, io::IOException );
57 : virtual void SAL_CALL closeOutput() throw ( uno::RuntimeException, io::NotConnectedException, io::BufferSizeExceededException, io::IOException );
58 :
59 : // ::com::sun::star::io::XSequenceOutputStream:
60 : virtual uno::Sequence< ::sal_Int8 > SAL_CALL getWrittenBytes( ) throw ( io::NotConnectedException, io::IOException, uno::RuntimeException);
61 :
62 : private:
63 : SequenceOutputStreamService( SequenceOutputStreamService & ); //not defined
64 : void operator =( SequenceOutputStreamService & ); //not defined
65 :
66 0 : virtual ~SequenceOutputStreamService() {};
67 :
68 :
69 : ::osl::Mutex m_aMutex;
70 : uno::Reference< io::XOutputStream > m_xOutputStream;
71 : uno::Sequence< ::sal_Int8 > m_aSequence;
72 : };
73 0 : SequenceOutputStreamService::SequenceOutputStreamService()
74 : {
75 0 : m_xOutputStream.set( static_cast < ::cppu::OWeakObject* >( new ::comphelper::OSequenceOutputStream( m_aSequence ) ), uno::UNO_QUERY_THROW );
76 0 : }
77 :
78 : // com.sun.star.uno.XServiceInfo:
79 0 : ::rtl::OUString SAL_CALL SequenceOutputStreamService::getImplementationName() throw ( uno::RuntimeException )
80 : {
81 0 : return getImplementationName_static();
82 : }
83 :
84 14 : ::rtl::OUString SAL_CALL SequenceOutputStreamService::getImplementationName_static()
85 : {
86 14 : return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.comp.SequenceOutputStreamService" ) );
87 : }
88 :
89 0 : ::sal_Bool SAL_CALL SequenceOutputStreamService::supportsService( ::rtl::OUString const & serviceName ) throw ( uno::RuntimeException )
90 : {
91 0 : uno::Sequence< ::rtl::OUString > serviceNames = getSupportedServiceNames();
92 0 : for ( ::sal_Int32 i = 0; i < serviceNames.getLength(); ++i ) {
93 0 : if ( serviceNames[i] == serviceName )
94 0 : return sal_True;
95 : }
96 0 : return sal_False;
97 : }
98 :
99 0 : uno::Sequence< ::rtl::OUString > SAL_CALL SequenceOutputStreamService::getSupportedServiceNames() throw ( uno::RuntimeException )
100 : {
101 0 : return getSupportedServiceNames_static();
102 : }
103 :
104 14 : uno::Sequence< ::rtl::OUString > SAL_CALL SequenceOutputStreamService::getSupportedServiceNames_static()
105 : {
106 14 : uno::Sequence< ::rtl::OUString > s( 1 );
107 14 : s[0] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.io.SequenceOutputStream" ) );
108 14 : return s;
109 : }
110 :
111 0 : uno::Reference< uno::XInterface > SAL_CALL SequenceOutputStreamService::Create(
112 : SAL_UNUSED_PARAMETER const uno::Reference< uno::XComponentContext >& )
113 : {
114 0 : return static_cast< ::cppu::OWeakObject * >( new SequenceOutputStreamService());
115 : }
116 :
117 : // ::com::sun::star::io::XOutputStream:
118 0 : void SAL_CALL SequenceOutputStreamService::writeBytes( const uno::Sequence< ::sal_Int8 > & aData ) throw ( uno::RuntimeException, io::NotConnectedException, io::BufferSizeExceededException, io::IOException )
119 : {
120 0 : ::osl::MutexGuard aGuard( m_aMutex );
121 0 : if ( !m_xOutputStream.is() )
122 0 : throw io::NotConnectedException();
123 :
124 0 : m_xOutputStream->writeBytes( aData );
125 0 : m_aSequence = aData;
126 0 : }
127 :
128 0 : void SAL_CALL SequenceOutputStreamService::flush() throw ( uno::RuntimeException, io::NotConnectedException, io::BufferSizeExceededException, io::IOException )
129 : {
130 0 : ::osl::MutexGuard aGuard( m_aMutex );
131 0 : if ( !m_xOutputStream.is() )
132 0 : throw io::NotConnectedException();
133 :
134 0 : m_xOutputStream->flush();
135 0 : };
136 :
137 0 : void SAL_CALL SequenceOutputStreamService::closeOutput() throw ( uno::RuntimeException, io::NotConnectedException, io::BufferSizeExceededException, io::IOException )
138 : {
139 0 : ::osl::MutexGuard aGuard( m_aMutex );
140 0 : if ( !m_xOutputStream.is() )
141 0 : throw io::NotConnectedException();
142 :
143 0 : m_xOutputStream->closeOutput();
144 0 : m_xOutputStream = uno::Reference< io::XOutputStream >();
145 0 : }
146 :
147 : // ::com::sun::star::io::XSequenceOutputStream:
148 0 : uno::Sequence< ::sal_Int8 > SAL_CALL SequenceOutputStreamService::getWrittenBytes() throw ( io::NotConnectedException, io::IOException, uno::RuntimeException)
149 : {
150 0 : ::osl::MutexGuard aGuard( m_aMutex );
151 0 : if ( !m_xOutputStream.is() )
152 0 : throw io::NotConnectedException();
153 :
154 0 : m_xOutputStream->flush();
155 0 : return m_aSequence;
156 : }
157 :
158 : } // anonymous namespace
159 :
160 14 : void createRegistryInfo_SequenceOutputStream()
161 : {
162 14 : static ::comphelper::module::OAutoRegistration< SequenceOutputStreamService > aAutoRegistration;
163 14 : }
164 :
165 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|