1 /*************************************************************************
2 *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
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.
14  *
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).
20  *
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.
25  *
26 ************************************************************************/
27 
28 #include "precompiled_comphelper.hxx"
29 
30 #include "comphelper_module.hxx"
31 
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>
41 
42 using namespace ::com::sun::star;
43 
44 
45 namespace {
46 
47 class SequenceOutputStreamService:
48 public ::cppu::WeakImplHelper2 < lang::XServiceInfo, io::XSequenceOutputStream >
49 {
50 public:
51     explicit SequenceOutputStreamService();
52 
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 );
57 
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 >& );
62 
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 );
67 
68     // ::com::sun::star::io::XSequenceOutputStream:
69     virtual uno::Sequence< ::sal_Int8 > SAL_CALL getWrittenBytes(  ) throw ( io::NotConnectedException, io::IOException, uno::RuntimeException);
70 
71 private:
72     SequenceOutputStreamService( SequenceOutputStreamService & ); //not defined
73     void operator =( SequenceOutputStreamService & ); //not defined
74 
75     virtual ~SequenceOutputStreamService() {};
76 
77 
78     ::osl::Mutex m_aMutex;
79     uno::Reference< io::XOutputStream > m_xOutputStream;
80     uno::Sequence< ::sal_Int8 > m_aSequence;
81 };
82 SequenceOutputStreamService::SequenceOutputStreamService()
83 {
84     m_xOutputStream.set( static_cast < ::cppu::OWeakObject* >( new ::comphelper::OSequenceOutputStream( m_aSequence ) ), uno::UNO_QUERY_THROW );
85 }
86 
87 // com.sun.star.uno.XServiceInfo:
88 ::rtl::OUString SAL_CALL SequenceOutputStreamService::getImplementationName() throw ( uno::RuntimeException )
89 {
90     return getImplementationName_static();
91 }
92 
93 ::rtl::OUString SAL_CALL SequenceOutputStreamService::getImplementationName_static()
94 {
95     return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.comp.SequenceOutputStreamService" ) );
96 }
97 
98 ::sal_Bool SAL_CALL SequenceOutputStreamService::supportsService( ::rtl::OUString const & serviceName ) throw ( uno::RuntimeException )
99 {
100     uno::Sequence< ::rtl::OUString > serviceNames = getSupportedServiceNames();
101     for ( ::sal_Int32 i = 0; i < serviceNames.getLength(); ++i ) {
102         if ( serviceNames[i] == serviceName )
103             return sal_True;
104     }
105     return sal_False;
106 }
107 
108 uno::Sequence< ::rtl::OUString > SAL_CALL SequenceOutputStreamService::getSupportedServiceNames() throw ( uno::RuntimeException )
109 {
110     return getSupportedServiceNames_static();
111 }
112 
113 uno::Sequence< ::rtl::OUString > SAL_CALL SequenceOutputStreamService::getSupportedServiceNames_static()
114 {
115     uno::Sequence< ::rtl::OUString > s( 1 );
116     s[0] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.io.SequenceOutputStream" ) );
117     return s;
118 }
119 
120 uno::Reference< uno::XInterface > SAL_CALL SequenceOutputStreamService::Create(
121     const uno::Reference< uno::XComponentContext >& )
122 {
123     return static_cast< ::cppu::OWeakObject * >( new SequenceOutputStreamService());
124 }
125 
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 )
128 {
129     ::osl::MutexGuard aGuard( m_aMutex );
130     if ( !m_xOutputStream.is() )
131         throw io::NotConnectedException();
132 
133     m_xOutputStream->writeBytes( aData );
134     m_aSequence = aData;
135 }
136 
137 void SAL_CALL SequenceOutputStreamService::flush() throw ( uno::RuntimeException, io::NotConnectedException, io::BufferSizeExceededException, io::IOException )
138 {
139     ::osl::MutexGuard aGuard( m_aMutex );
140     if ( !m_xOutputStream.is() )
141         throw io::NotConnectedException();
142 
143     m_xOutputStream->flush();
144 };
145 
146 void SAL_CALL SequenceOutputStreamService::closeOutput() throw ( uno::RuntimeException, io::NotConnectedException, io::BufferSizeExceededException, io::IOException )
147 {
148     ::osl::MutexGuard aGuard( m_aMutex );
149     if ( !m_xOutputStream.is() )
150         throw io::NotConnectedException();
151 
152     m_xOutputStream->closeOutput();
153     m_xOutputStream = uno::Reference< io::XOutputStream >();
154 }
155 
156 // ::com::sun::star::io::XSequenceOutputStream:
157 uno::Sequence< ::sal_Int8 > SAL_CALL SequenceOutputStreamService::getWrittenBytes() throw ( io::NotConnectedException, io::IOException, uno::RuntimeException)
158 {
159     ::osl::MutexGuard aGuard( m_aMutex );
160     if ( !m_xOutputStream.is() )
161         throw io::NotConnectedException();
162 
163     m_xOutputStream->flush();
164     return m_aSequence;
165 }
166 
167 } // anonymous namespace
168 
169 void createRegistryInfo_SequenceOutputStream()
170 {
171     static ::comphelper::module::OAutoRegistration< SequenceOutputStreamService > aAutoRegistration;
172 }
173