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 4 : 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 2 : SequenceOutputStreamService::SequenceOutputStreamService()
75 : {
76 2 : m_xOutputStream.set( static_cast < ::cppu::OWeakObject* >( new ::comphelper::OSequenceOutputStream( m_aSequence ) ), uno::UNO_QUERY_THROW );
77 2 : }
78 :
79 : // com.sun.star.uno.XServiceInfo:
80 1 : OUString SAL_CALL SequenceOutputStreamService::getImplementationName() throw ( uno::RuntimeException, std::exception )
81 : {
82 1 : return getImplementationName_static();
83 : }
84 :
85 192 : OUString SAL_CALL SequenceOutputStreamService::getImplementationName_static()
86 : {
87 192 : 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 1 : uno::Sequence< OUString > SAL_CALL SequenceOutputStreamService::getSupportedServiceNames() throw ( uno::RuntimeException, std::exception )
96 : {
97 1 : return getSupportedServiceNames_static();
98 : }
99 :
100 192 : uno::Sequence< OUString > SAL_CALL SequenceOutputStreamService::getSupportedServiceNames_static()
101 : {
102 192 : uno::Sequence< OUString > s( 1 );
103 192 : s[0] = "com.sun.star.io.SequenceOutputStream";
104 192 : return s;
105 : }
106 :
107 2 : uno::Reference< uno::XInterface > SAL_CALL SequenceOutputStreamService::Create(
108 : SAL_UNUSED_PARAMETER const uno::Reference< uno::XComponentContext >& )
109 : {
110 2 : return static_cast< ::cppu::OWeakObject * >( new SequenceOutputStreamService());
111 : }
112 :
113 : // ::com::sun::star::io::XOutputStream:
114 1 : void SAL_CALL SequenceOutputStreamService::writeBytes( const uno::Sequence< ::sal_Int8 > & aData ) throw ( uno::RuntimeException, io::NotConnectedException, io::BufferSizeExceededException, io::IOException, std::exception )
115 : {
116 1 : ::osl::MutexGuard aGuard( m_aMutex );
117 1 : if ( !m_xOutputStream.is() )
118 0 : throw io::NotConnectedException();
119 :
120 1 : m_xOutputStream->writeBytes( aData );
121 1 : m_aSequence = aData;
122 1 : }
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 1 : void SAL_CALL SequenceOutputStreamService::closeOutput() throw ( uno::RuntimeException, io::NotConnectedException, io::BufferSizeExceededException, io::IOException, std::exception )
134 : {
135 1 : ::osl::MutexGuard aGuard( m_aMutex );
136 1 : if ( !m_xOutputStream.is() )
137 0 : throw io::NotConnectedException();
138 :
139 1 : m_xOutputStream->closeOutput();
140 1 : m_xOutputStream = uno::Reference< io::XOutputStream >();
141 1 : }
142 :
143 : // ::com::sun::star::io::XSequenceOutputStream:
144 1 : uno::Sequence< ::sal_Int8 > SAL_CALL SequenceOutputStreamService::getWrittenBytes() throw ( io::NotConnectedException, io::IOException, uno::RuntimeException, std::exception)
145 : {
146 1 : ::osl::MutexGuard aGuard( m_aMutex );
147 1 : if ( !m_xOutputStream.is() )
148 0 : throw io::NotConnectedException();
149 :
150 1 : m_xOutputStream->flush();
151 1 : return m_aSequence;
152 : }
153 :
154 : } // anonymous namespace
155 :
156 191 : void createRegistryInfo_SequenceOutputStream()
157 : {
158 191 : static ::comphelper::module::OAutoRegistration< SequenceOutputStreamService > aAutoRegistration;
159 191 : }
160 :
161 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|