Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include <sal/config.h>
21 :
22 : #include "comphelper_module.hxx"
23 : #include "comphelper_services.hxx"
24 :
25 : #include <boost/noncopyable.hpp>
26 : #include <osl/mutex.hxx>
27 : #include <cppuhelper/factory.hxx>
28 : #include <cppuhelper/implementationentry.hxx>
29 : #include <cppuhelper/implbase2.hxx>
30 : #include <cppuhelper/supportsservice.hxx>
31 : #include <comphelper/seqstream.hxx>
32 : #include <com/sun/star/lang/XServiceInfo.hpp>
33 : #include <com/sun/star/io/XSequenceOutputStream.hpp>
34 : #include <com/sun/star/uno/XComponentContext.hpp>
35 :
36 : using namespace ::com::sun::star;
37 :
38 :
39 : namespace {
40 :
41 : class SequenceOutputStreamService:
42 : public cppu::WeakImplHelper2<lang::XServiceInfo, io::XSequenceOutputStream>,
43 : private boost::noncopyable
44 : {
45 : public:
46 : explicit SequenceOutputStreamService();
47 :
48 : // ::com::sun::star::lang::XServiceInfo:
49 : virtual OUString SAL_CALL getImplementationName() throw ( uno::RuntimeException, std::exception ) SAL_OVERRIDE;
50 : virtual sal_Bool SAL_CALL supportsService( const OUString & ServiceName ) throw ( uno::RuntimeException, std::exception ) SAL_OVERRIDE;
51 : virtual uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() throw ( uno::RuntimeException, std::exception ) SAL_OVERRIDE;
52 :
53 : // XServiceInfo - static versions (used for component registration)
54 : static OUString SAL_CALL getImplementationName_static();
55 : static uno::Sequence< OUString > SAL_CALL getSupportedServiceNames_static();
56 : static uno::Reference< uno::XInterface > SAL_CALL Create( const uno::Reference< uno::XComponentContext >& );
57 :
58 : // ::com::sun::star::io::XOutputStream:
59 : virtual void SAL_CALL writeBytes( const uno::Sequence< ::sal_Int8 > & aData ) throw ( io::NotConnectedException, io::BufferSizeExceededException, io::IOException, uno::RuntimeException, std::exception ) SAL_OVERRIDE;
60 : virtual void SAL_CALL flush() throw ( uno::RuntimeException, io::NotConnectedException, io::BufferSizeExceededException, io::IOException, std::exception ) SAL_OVERRIDE;
61 : virtual void SAL_CALL closeOutput() throw ( uno::RuntimeException, io::NotConnectedException, io::BufferSizeExceededException, io::IOException, std::exception ) SAL_OVERRIDE;
62 :
63 : // ::com::sun::star::io::XSequenceOutputStream:
64 : virtual uno::Sequence< ::sal_Int8 > SAL_CALL getWrittenBytes( ) throw ( io::NotConnectedException, io::IOException, uno::RuntimeException, std::exception) SAL_OVERRIDE;
65 :
66 : private:
67 0 : virtual ~SequenceOutputStreamService() {};
68 :
69 :
70 : ::osl::Mutex m_aMutex;
71 : uno::Reference< io::XOutputStream > m_xOutputStream;
72 : uno::Sequence< ::sal_Int8 > m_aSequence;
73 : };
74 0 : SequenceOutputStreamService::SequenceOutputStreamService()
75 : {
76 0 : m_xOutputStream.set( static_cast < ::cppu::OWeakObject* >( new ::comphelper::OSequenceOutputStream( m_aSequence ) ), uno::UNO_QUERY_THROW );
77 0 : }
78 :
79 : // com.sun.star.uno.XServiceInfo:
80 0 : OUString SAL_CALL SequenceOutputStreamService::getImplementationName() throw ( uno::RuntimeException, std::exception )
81 : {
82 0 : return getImplementationName_static();
83 : }
84 :
85 0 : OUString SAL_CALL SequenceOutputStreamService::getImplementationName_static()
86 : {
87 0 : return OUString("com.sun.star.comp.SequenceOutputStreamService");
88 : }
89 :
90 0 : sal_Bool SAL_CALL SequenceOutputStreamService::supportsService( OUString const & serviceName ) throw ( uno::RuntimeException, std::exception )
91 : {
92 0 : return cppu::supportsService(this, serviceName);
93 : }
94 :
95 0 : uno::Sequence< OUString > SAL_CALL SequenceOutputStreamService::getSupportedServiceNames() throw ( uno::RuntimeException, std::exception )
96 : {
97 0 : return getSupportedServiceNames_static();
98 : }
99 :
100 0 : uno::Sequence< OUString > SAL_CALL SequenceOutputStreamService::getSupportedServiceNames_static()
101 : {
102 0 : uno::Sequence< OUString > s( 1 );
103 0 : s[0] = "com.sun.star.io.SequenceOutputStream";
104 0 : return s;
105 : }
106 :
107 0 : uno::Reference< uno::XInterface > SAL_CALL SequenceOutputStreamService::Create(
108 : SAL_UNUSED_PARAMETER const uno::Reference< uno::XComponentContext >& )
109 : {
110 0 : return static_cast< ::cppu::OWeakObject * >( new SequenceOutputStreamService());
111 : }
112 :
113 : // ::com::sun::star::io::XOutputStream:
114 0 : void SAL_CALL SequenceOutputStreamService::writeBytes( const uno::Sequence< ::sal_Int8 > & aData ) throw ( uno::RuntimeException, io::NotConnectedException, io::BufferSizeExceededException, io::IOException, std::exception )
115 : {
116 0 : ::osl::MutexGuard aGuard( m_aMutex );
117 0 : if ( !m_xOutputStream.is() )
118 0 : throw io::NotConnectedException();
119 :
120 0 : m_xOutputStream->writeBytes( aData );
121 0 : m_aSequence = aData;
122 0 : }
123 :
124 0 : void SAL_CALL SequenceOutputStreamService::flush() throw ( uno::RuntimeException, io::NotConnectedException, io::BufferSizeExceededException, io::IOException, std::exception )
125 : {
126 0 : ::osl::MutexGuard aGuard( m_aMutex );
127 0 : if ( !m_xOutputStream.is() )
128 0 : throw io::NotConnectedException();
129 :
130 0 : m_xOutputStream->flush();
131 0 : };
132 :
133 0 : void SAL_CALL SequenceOutputStreamService::closeOutput() throw ( uno::RuntimeException, io::NotConnectedException, io::BufferSizeExceededException, io::IOException, std::exception )
134 : {
135 0 : ::osl::MutexGuard aGuard( m_aMutex );
136 0 : if ( !m_xOutputStream.is() )
137 0 : throw io::NotConnectedException();
138 :
139 0 : m_xOutputStream->closeOutput();
140 0 : m_xOutputStream = uno::Reference< io::XOutputStream >();
141 0 : }
142 :
143 : // ::com::sun::star::io::XSequenceOutputStream:
144 0 : uno::Sequence< ::sal_Int8 > SAL_CALL SequenceOutputStreamService::getWrittenBytes() throw ( io::NotConnectedException, io::IOException, uno::RuntimeException, std::exception)
145 : {
146 0 : ::osl::MutexGuard aGuard( m_aMutex );
147 0 : if ( !m_xOutputStream.is() )
148 0 : throw io::NotConnectedException();
149 :
150 0 : m_xOutputStream->flush();
151 0 : return m_aSequence;
152 : }
153 :
154 : } // anonymous namespace
155 :
156 0 : void createRegistryInfo_SequenceOutputStream()
157 : {
158 0 : static ::comphelper::module::OAutoRegistration< SequenceOutputStreamService > aAutoRegistration;
159 0 : }
160 :
161 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|