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