1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
28 #include "precompiled_comphelper.hxx"
30 #include "comphelper_module.hxx"
32 #include <sal/config.h>
33 #include <osl/mutex.hxx>
34 #include <cppuhelper/factory.hxx>
35 #include <cppuhelper/implementationentry.hxx>
36 #include <cppuhelper/implbase2.hxx>
37 #include <comphelper/seqstream.hxx>
38 #include <com/sun/star/lang/XServiceInfo.hpp>
39 #include <com/sun/star/io/XSequenceOutputStream.hpp>
40 #include <com/sun/star/uno/XComponentContext.hpp>
42 using namespace ::com::sun::star
;
47 class SequenceOutputStreamService
:
48 public ::cppu::WeakImplHelper2
< lang::XServiceInfo
, io::XSequenceOutputStream
>
51 explicit SequenceOutputStreamService();
53 // ::com::sun::star::lang::XServiceInfo:
54 virtual ::rtl::OUString SAL_CALL
getImplementationName() throw ( uno::RuntimeException
);
55 virtual ::sal_Bool SAL_CALL
supportsService( const ::rtl::OUString
& ServiceName
) throw ( uno::RuntimeException
);
56 virtual uno::Sequence
< ::rtl::OUString
> SAL_CALL
getSupportedServiceNames() throw ( uno::RuntimeException
);
58 // XServiceInfo - static versions (used for component registration)
59 static ::rtl::OUString SAL_CALL
getImplementationName_static();
60 static uno::Sequence
< ::rtl::OUString
> SAL_CALL
getSupportedServiceNames_static();
61 static uno::Reference
< uno::XInterface
> SAL_CALL
Create( const uno::Reference
< uno::XComponentContext
>& );
63 // ::com::sun::star::io::XOutputStream:
64 virtual void SAL_CALL
writeBytes( const uno::Sequence
< ::sal_Int8
> & aData
) throw ( io::NotConnectedException
, io::BufferSizeExceededException
, io::IOException
, uno::RuntimeException
);
65 virtual void SAL_CALL
flush() throw ( uno::RuntimeException
, io::NotConnectedException
, io::BufferSizeExceededException
, io::IOException
);
66 virtual void SAL_CALL
closeOutput() throw ( uno::RuntimeException
, io::NotConnectedException
, io::BufferSizeExceededException
, io::IOException
);
68 // ::com::sun::star::io::XSequenceOutputStream:
69 virtual uno::Sequence
< ::sal_Int8
> SAL_CALL
getWrittenBytes( ) throw ( io::NotConnectedException
, io::IOException
, uno::RuntimeException
);
72 SequenceOutputStreamService( SequenceOutputStreamService
& ); //not defined
73 void operator =( SequenceOutputStreamService
& ); //not defined
75 virtual ~SequenceOutputStreamService() {};
78 ::osl::Mutex m_aMutex
;
79 uno::Reference
< io::XOutputStream
> m_xOutputStream
;
80 uno::Sequence
< ::sal_Int8
> m_aSequence
;
82 SequenceOutputStreamService::SequenceOutputStreamService()
84 m_xOutputStream
.set( static_cast < ::cppu::OWeakObject
* >( new ::comphelper::OSequenceOutputStream( m_aSequence
) ), uno::UNO_QUERY_THROW
);
87 // com.sun.star.uno.XServiceInfo:
88 ::rtl::OUString SAL_CALL
SequenceOutputStreamService::getImplementationName() throw ( uno::RuntimeException
)
90 return getImplementationName_static();
93 ::rtl::OUString SAL_CALL
SequenceOutputStreamService::getImplementationName_static()
95 return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.comp.SequenceOutputStreamService" ) );
98 ::sal_Bool SAL_CALL
SequenceOutputStreamService::supportsService( ::rtl::OUString
const & serviceName
) throw ( uno::RuntimeException
)
100 uno::Sequence
< ::rtl::OUString
> serviceNames
= getSupportedServiceNames();
101 for ( ::sal_Int32 i
= 0; i
< serviceNames
.getLength(); ++i
) {
102 if ( serviceNames
[i
] == serviceName
)
108 uno::Sequence
< ::rtl::OUString
> SAL_CALL
SequenceOutputStreamService::getSupportedServiceNames() throw ( uno::RuntimeException
)
110 return getSupportedServiceNames_static();
113 uno::Sequence
< ::rtl::OUString
> SAL_CALL
SequenceOutputStreamService::getSupportedServiceNames_static()
115 uno::Sequence
< ::rtl::OUString
> s( 1 );
116 s
[0] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.io.SequenceOutputStream" ) );
120 uno::Reference
< uno::XInterface
> SAL_CALL
SequenceOutputStreamService::Create(
121 const uno::Reference
< uno::XComponentContext
>& )
123 return static_cast< ::cppu::OWeakObject
* >( new SequenceOutputStreamService());
126 // ::com::sun::star::io::XOutputStream:
127 void SAL_CALL
SequenceOutputStreamService::writeBytes( const uno::Sequence
< ::sal_Int8
> & aData
) throw ( uno::RuntimeException
, io::NotConnectedException
, io::BufferSizeExceededException
, io::IOException
)
129 ::osl::MutexGuard
aGuard( m_aMutex
);
130 if ( !m_xOutputStream
.is() )
131 throw io::NotConnectedException();
133 m_xOutputStream
->writeBytes( aData
);
137 void SAL_CALL
SequenceOutputStreamService::flush() throw ( uno::RuntimeException
, io::NotConnectedException
, io::BufferSizeExceededException
, io::IOException
)
139 ::osl::MutexGuard
aGuard( m_aMutex
);
140 if ( !m_xOutputStream
.is() )
141 throw io::NotConnectedException();
143 m_xOutputStream
->flush();
146 void SAL_CALL
SequenceOutputStreamService::closeOutput() throw ( uno::RuntimeException
, io::NotConnectedException
, io::BufferSizeExceededException
, io::IOException
)
148 ::osl::MutexGuard
aGuard( m_aMutex
);
149 if ( !m_xOutputStream
.is() )
150 throw io::NotConnectedException();
152 m_xOutputStream
->closeOutput();
153 m_xOutputStream
= uno::Reference
< io::XOutputStream
>();
156 // ::com::sun::star::io::XSequenceOutputStream:
157 uno::Sequence
< ::sal_Int8
> SAL_CALL
SequenceOutputStreamService::getWrittenBytes() throw ( io::NotConnectedException
, io::IOException
, uno::RuntimeException
)
159 ::osl::MutexGuard
aGuard( m_aMutex
);
160 if ( !m_xOutputStream
.is() )
161 throw io::NotConnectedException();
163 m_xOutputStream
->flush();
167 } // anonymous namespace
169 void createRegistryInfo_SequenceOutputStream()
171 static ::comphelper::module::OAutoRegistration
< SequenceOutputStreamService
> aAutoRegistration
;