1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #include "precompiled_comphelper.hxx"
25 
26 #include "comphelper_module.hxx"
27 
28 #include <sal/config.h>
29 #include <osl/mutex.hxx>
30 #include <cppuhelper/factory.hxx>
31 #include <cppuhelper/implementationentry.hxx>
32 #include <cppuhelper/implbase2.hxx>
33 #include <comphelper/seqstream.hxx>
34 #include <com/sun/star/lang/XServiceInfo.hpp>
35 #include <com/sun/star/io/XSequenceOutputStream.hpp>
36 #include <com/sun/star/uno/XComponentContext.hpp>
37 
38 using namespace ::com::sun::star;
39 
40 
41 namespace {
42 
43 class SequenceOutputStreamService:
44 public ::cppu::WeakImplHelper2 < lang::XServiceInfo, io::XSequenceOutputStream >
45 {
46 public:
47     explicit SequenceOutputStreamService();
48 
49     // ::com::sun::star::lang::XServiceInfo:
50     virtual ::rtl::OUString SAL_CALL getImplementationName() throw ( uno::RuntimeException );
51     virtual ::sal_Bool SAL_CALL supportsService( const ::rtl::OUString & ServiceName ) throw ( uno::RuntimeException );
52     virtual uno::Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames() throw ( uno::RuntimeException );
53 
54     // XServiceInfo - static versions (used for component registration)
55     static ::rtl::OUString SAL_CALL getImplementationName_static();
56     static uno::Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames_static();
57     static uno::Reference< uno::XInterface > SAL_CALL Create( const uno::Reference< uno::XComponentContext >& );
58 
59     // ::com::sun::star::io::XOutputStream:
60     virtual void SAL_CALL writeBytes( const uno::Sequence< ::sal_Int8 > & aData ) throw ( io::NotConnectedException, io::BufferSizeExceededException, io::IOException, uno::RuntimeException );
61     virtual void SAL_CALL flush() throw ( uno::RuntimeException, io::NotConnectedException, io::BufferSizeExceededException, io::IOException );
62     virtual void SAL_CALL closeOutput() throw ( uno::RuntimeException, io::NotConnectedException, io::BufferSizeExceededException, io::IOException );
63 
64     // ::com::sun::star::io::XSequenceOutputStream:
65     virtual uno::Sequence< ::sal_Int8 > SAL_CALL getWrittenBytes(  ) throw ( io::NotConnectedException, io::IOException, uno::RuntimeException);
66 
67 private:
68     SequenceOutputStreamService( SequenceOutputStreamService & ); //not defined
69     void operator =( SequenceOutputStreamService & ); //not defined
70 
~SequenceOutputStreamService()71     virtual ~SequenceOutputStreamService() {};
72 
73 
74     ::osl::Mutex m_aMutex;
75     uno::Reference< io::XOutputStream > m_xOutputStream;
76     uno::Sequence< ::sal_Int8 > m_aSequence;
77 };
SequenceOutputStreamService()78 SequenceOutputStreamService::SequenceOutputStreamService()
79 {
80     m_xOutputStream.set( static_cast < ::cppu::OWeakObject* >( new ::comphelper::OSequenceOutputStream( m_aSequence ) ), uno::UNO_QUERY_THROW );
81 }
82 
83 // com.sun.star.uno.XServiceInfo:
getImplementationName()84 ::rtl::OUString SAL_CALL SequenceOutputStreamService::getImplementationName() throw ( uno::RuntimeException )
85 {
86     return getImplementationName_static();
87 }
88 
getImplementationName_static()89 ::rtl::OUString SAL_CALL SequenceOutputStreamService::getImplementationName_static()
90 {
91     return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.comp.SequenceOutputStreamService" ) );
92 }
93 
supportsService(::rtl::OUString const & serviceName)94 ::sal_Bool SAL_CALL SequenceOutputStreamService::supportsService( ::rtl::OUString const & serviceName ) throw ( uno::RuntimeException )
95 {
96     uno::Sequence< ::rtl::OUString > serviceNames = getSupportedServiceNames();
97     for ( ::sal_Int32 i = 0; i < serviceNames.getLength(); ++i ) {
98         if ( serviceNames[i] == serviceName )
99             return sal_True;
100     }
101     return sal_False;
102 }
103 
getSupportedServiceNames()104 uno::Sequence< ::rtl::OUString > SAL_CALL SequenceOutputStreamService::getSupportedServiceNames() throw ( uno::RuntimeException )
105 {
106     return getSupportedServiceNames_static();
107 }
108 
getSupportedServiceNames_static()109 uno::Sequence< ::rtl::OUString > SAL_CALL SequenceOutputStreamService::getSupportedServiceNames_static()
110 {
111     uno::Sequence< ::rtl::OUString > s( 1 );
112     s[0] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.io.SequenceOutputStream" ) );
113     return s;
114 }
115 
Create(const uno::Reference<uno::XComponentContext> &)116 uno::Reference< uno::XInterface > SAL_CALL SequenceOutputStreamService::Create(
117     const uno::Reference< uno::XComponentContext >& )
118 {
119     return static_cast< ::cppu::OWeakObject * >( new SequenceOutputStreamService());
120 }
121 
122 // ::com::sun::star::io::XOutputStream:
writeBytes(const uno::Sequence<::sal_Int8> & aData)123 void SAL_CALL SequenceOutputStreamService::writeBytes( const uno::Sequence< ::sal_Int8 > & aData ) throw ( uno::RuntimeException, io::NotConnectedException, io::BufferSizeExceededException, io::IOException )
124 {
125     ::osl::MutexGuard aGuard( m_aMutex );
126     if ( !m_xOutputStream.is() )
127         throw io::NotConnectedException();
128 
129     m_xOutputStream->writeBytes( aData );
130     m_aSequence = aData;
131 }
132 
flush()133 void SAL_CALL SequenceOutputStreamService::flush() throw ( uno::RuntimeException, io::NotConnectedException, io::BufferSizeExceededException, io::IOException )
134 {
135     ::osl::MutexGuard aGuard( m_aMutex );
136     if ( !m_xOutputStream.is() )
137         throw io::NotConnectedException();
138 
139     m_xOutputStream->flush();
140 };
141 
closeOutput()142 void SAL_CALL SequenceOutputStreamService::closeOutput() throw ( uno::RuntimeException, io::NotConnectedException, io::BufferSizeExceededException, io::IOException )
143 {
144     ::osl::MutexGuard aGuard( m_aMutex );
145     if ( !m_xOutputStream.is() )
146         throw io::NotConnectedException();
147 
148     m_xOutputStream->closeOutput();
149     m_xOutputStream = uno::Reference< io::XOutputStream >();
150 }
151 
152 // ::com::sun::star::io::XSequenceOutputStream:
getWrittenBytes()153 uno::Sequence< ::sal_Int8 > SAL_CALL SequenceOutputStreamService::getWrittenBytes() throw ( io::NotConnectedException, io::IOException, uno::RuntimeException)
154 {
155     ::osl::MutexGuard aGuard( m_aMutex );
156     if ( !m_xOutputStream.is() )
157         throw io::NotConnectedException();
158 
159     m_xOutputStream->flush();
160     return m_aSequence;
161 }
162 
163 } // anonymous namespace
164 
createRegistryInfo_SequenceOutputStream()165 void createRegistryInfo_SequenceOutputStream()
166 {
167     static ::comphelper::module::OAutoRegistration< SequenceOutputStreamService > aAutoRegistration;
168 }
169