Update ooo320-m1
[ooovba.git] / comphelper / source / streaming / seqoutputstreamserv.cxx
blobaff626ada2b057da6b8a8d82fd16e4ed0cfeba61
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: seqoutputstreamserv.cxx,v $
10 * $Revision: 1.3 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #include "precompiled_comphelper.hxx"
33 #include "comphelper_module.hxx"
35 #include <sal/config.h>
36 #include <osl/mutex.hxx>
37 #include <cppuhelper/factory.hxx>
38 #include <cppuhelper/implementationentry.hxx>
39 #include <cppuhelper/implbase2.hxx>
40 #include <comphelper/seqstream.hxx>
41 #include <com/sun/star/lang/XServiceInfo.hpp>
42 #include <com/sun/star/io/XSequenceOutputStream.hpp>
43 #include <com/sun/star/uno/XComponentContext.hpp>
45 using namespace ::com::sun::star;
48 namespace {
50 class SequenceOutputStreamService:
51 public ::cppu::WeakImplHelper2 < lang::XServiceInfo, io::XSequenceOutputStream >
53 public:
54 explicit SequenceOutputStreamService();
56 // ::com::sun::star::lang::XServiceInfo:
57 virtual ::rtl::OUString SAL_CALL getImplementationName() throw ( uno::RuntimeException );
58 virtual ::sal_Bool SAL_CALL supportsService( const ::rtl::OUString & ServiceName ) throw ( uno::RuntimeException );
59 virtual uno::Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames() throw ( uno::RuntimeException );
61 // XServiceInfo - static versions (used for component registration)
62 static ::rtl::OUString SAL_CALL getImplementationName_static();
63 static uno::Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames_static();
64 static uno::Reference< uno::XInterface > SAL_CALL Create( const uno::Reference< uno::XComponentContext >& );
66 // ::com::sun::star::io::XOutputStream:
67 virtual void SAL_CALL writeBytes( const uno::Sequence< ::sal_Int8 > & aData ) throw ( io::NotConnectedException, io::BufferSizeExceededException, io::IOException, uno::RuntimeException );
68 virtual void SAL_CALL flush() throw ( uno::RuntimeException, io::NotConnectedException, io::BufferSizeExceededException, io::IOException );
69 virtual void SAL_CALL closeOutput() throw ( uno::RuntimeException, io::NotConnectedException, io::BufferSizeExceededException, io::IOException );
71 // ::com::sun::star::io::XSequenceOutputStream:
72 virtual uno::Sequence< ::sal_Int8 > SAL_CALL getWrittenBytes( ) throw ( io::NotConnectedException, io::IOException, uno::RuntimeException);
74 private:
75 SequenceOutputStreamService( SequenceOutputStreamService & ); //not defined
76 void operator =( SequenceOutputStreamService & ); //not defined
78 virtual ~SequenceOutputStreamService() {};
81 ::osl::Mutex m_aMutex;
82 uno::Reference< io::XOutputStream > m_xOutputStream;
83 uno::Sequence< ::sal_Int8 > m_aSequence;
85 SequenceOutputStreamService::SequenceOutputStreamService()
87 m_xOutputStream.set( static_cast < ::cppu::OWeakObject* >( new ::comphelper::OSequenceOutputStream( m_aSequence ) ), uno::UNO_QUERY_THROW );
90 // com.sun.star.uno.XServiceInfo:
91 ::rtl::OUString SAL_CALL SequenceOutputStreamService::getImplementationName() throw ( uno::RuntimeException )
93 return getImplementationName_static();
96 ::rtl::OUString SAL_CALL SequenceOutputStreamService::getImplementationName_static()
98 return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.comp.SequenceOutputStreamService" ) );
101 ::sal_Bool SAL_CALL SequenceOutputStreamService::supportsService( ::rtl::OUString const & serviceName ) throw ( uno::RuntimeException )
103 uno::Sequence< ::rtl::OUString > serviceNames = getSupportedServiceNames();
104 for ( ::sal_Int32 i = 0; i < serviceNames.getLength(); ++i ) {
105 if ( serviceNames[i] == serviceName )
106 return sal_True;
108 return sal_False;
111 uno::Sequence< ::rtl::OUString > SAL_CALL SequenceOutputStreamService::getSupportedServiceNames() throw ( uno::RuntimeException )
113 return getSupportedServiceNames_static();
116 uno::Sequence< ::rtl::OUString > SAL_CALL SequenceOutputStreamService::getSupportedServiceNames_static()
118 uno::Sequence< ::rtl::OUString > s( 1 );
119 s[0] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.io.SequenceOutputStream" ) );
120 return s;
123 uno::Reference< uno::XInterface > SAL_CALL SequenceOutputStreamService::Create(
124 const uno::Reference< uno::XComponentContext >& )
126 return static_cast< ::cppu::OWeakObject * >( new SequenceOutputStreamService());
129 // ::com::sun::star::io::XOutputStream:
130 void SAL_CALL SequenceOutputStreamService::writeBytes( const uno::Sequence< ::sal_Int8 > & aData ) throw ( uno::RuntimeException, io::NotConnectedException, io::BufferSizeExceededException, io::IOException )
132 ::osl::MutexGuard aGuard( m_aMutex );
133 if ( !m_xOutputStream.is() )
134 throw io::NotConnectedException();
136 m_xOutputStream->writeBytes( aData );
137 m_aSequence = aData;
140 void SAL_CALL SequenceOutputStreamService::flush() throw ( uno::RuntimeException, io::NotConnectedException, io::BufferSizeExceededException, io::IOException )
142 ::osl::MutexGuard aGuard( m_aMutex );
143 if ( !m_xOutputStream.is() )
144 throw io::NotConnectedException();
146 m_xOutputStream->flush();
149 void SAL_CALL SequenceOutputStreamService::closeOutput() throw ( uno::RuntimeException, io::NotConnectedException, io::BufferSizeExceededException, io::IOException )
151 ::osl::MutexGuard aGuard( m_aMutex );
152 if ( !m_xOutputStream.is() )
153 throw io::NotConnectedException();
155 m_xOutputStream->closeOutput();
156 m_xOutputStream = uno::Reference< io::XOutputStream >();
159 // ::com::sun::star::io::XSequenceOutputStream:
160 uno::Sequence< ::sal_Int8 > SAL_CALL SequenceOutputStreamService::getWrittenBytes() throw ( io::NotConnectedException, io::IOException, uno::RuntimeException)
162 ::osl::MutexGuard aGuard( m_aMutex );
163 if ( !m_xOutputStream.is() )
164 throw io::NotConnectedException();
166 m_xOutputStream->flush();
167 return m_aSequence;
170 } // anonymous namespace
172 void createRegistryInfo_SequenceOutputStream()
174 static ::comphelper::module::OAutoRegistration< SequenceOutputStreamService > aAutoRegistration;