/src/libreoffice/svl/source/fsstor/oinputstreamcontainer.cxx
Line | Count | Source |
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 | | |
21 | | #include "oinputstreamcontainer.hxx" |
22 | | #include <cppuhelper/typeprovider.hxx> |
23 | | #include <cppuhelper/queryinterface.hxx> |
24 | | |
25 | | using namespace ::com::sun::star; |
26 | | |
27 | | OFSInputStreamContainer::OFSInputStreamContainer( const uno::Reference< io::XInputStream >& xStream ) |
28 | 0 | : m_xInputStream( xStream ) |
29 | 0 | , m_xSeekable( xStream, uno::UNO_QUERY ) |
30 | 0 | , m_bSeekable( false ) |
31 | 0 | , m_bDisposed( false ) |
32 | 0 | { |
33 | 0 | m_bSeekable = m_xSeekable.is(); |
34 | 0 | } |
35 | | |
36 | | OFSInputStreamContainer::~OFSInputStreamContainer() |
37 | 0 | { |
38 | 0 | } |
39 | | |
40 | | uno::Sequence< uno::Type > SAL_CALL OFSInputStreamContainer::getTypes() |
41 | 0 | { |
42 | 0 | if (m_bSeekable) |
43 | 0 | { |
44 | 0 | static cppu::OTypeCollection aTypeCollection(cppu::UnoType<io::XStream>::get(), |
45 | 0 | cppu::UnoType<io::XInputStream>::get(), |
46 | 0 | cppu::UnoType<io::XSeekable>::get()); |
47 | |
|
48 | 0 | return aTypeCollection.getTypes(); |
49 | 0 | } |
50 | 0 | else |
51 | 0 | { |
52 | 0 | static cppu::OTypeCollection aTypeCollection(cppu::UnoType<io::XStream>::get(), |
53 | 0 | cppu::UnoType<io::XInputStream>::get()); |
54 | |
|
55 | 0 | return aTypeCollection.getTypes(); |
56 | 0 | } |
57 | 0 | } |
58 | | |
59 | | uno::Any SAL_CALL OFSInputStreamContainer::queryInterface( const uno::Type& rType ) |
60 | 0 | { |
61 | | // Attention: |
62 | | // Don't use mutex or guard in this method!!! Is a method of XInterface. |
63 | |
|
64 | 0 | uno::Any aReturn; |
65 | 0 | if ( m_bSeekable ) |
66 | 0 | aReturn = ::cppu::queryInterface( rType, |
67 | 0 | static_cast< io::XStream* >( this ), |
68 | 0 | static_cast< io::XInputStream* >( this ), |
69 | 0 | static_cast< io::XSeekable* >( this ) ); |
70 | 0 | else |
71 | 0 | aReturn = ::cppu::queryInterface( rType, |
72 | 0 | static_cast< io::XStream* >( this ), |
73 | 0 | static_cast< io::XInputStream* >( this ) ); |
74 | |
|
75 | 0 | if ( aReturn.hasValue() ) |
76 | 0 | return aReturn ; |
77 | | |
78 | 0 | return ::cppu::OWeakObject::queryInterface( rType ) ; |
79 | 0 | } |
80 | | |
81 | | void SAL_CALL OFSInputStreamContainer::acquire() |
82 | | noexcept |
83 | 0 | { |
84 | 0 | ::cppu::OWeakObject::acquire(); |
85 | 0 | } |
86 | | |
87 | | void SAL_CALL OFSInputStreamContainer::release() |
88 | | noexcept |
89 | 0 | { |
90 | 0 | ::cppu::OWeakObject::release(); |
91 | 0 | } |
92 | | |
93 | | sal_Int32 SAL_CALL OFSInputStreamContainer::readBytes( uno::Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead ) |
94 | 0 | { |
95 | 0 | std::scoped_lock aGuard( m_aMutex ); |
96 | |
|
97 | 0 | if ( m_bDisposed ) |
98 | 0 | throw lang::DisposedException(); |
99 | | |
100 | 0 | if ( !m_xInputStream.is() ) |
101 | 0 | throw uno::RuntimeException(); |
102 | | |
103 | 0 | return m_xInputStream->readBytes( aData, nBytesToRead ); |
104 | 0 | } |
105 | | |
106 | | sal_Int32 SAL_CALL OFSInputStreamContainer::readSomeBytes( uno::Sequence< sal_Int8 >& aData, sal_Int32 nMaxBytesToRead ) |
107 | 0 | { |
108 | 0 | std::scoped_lock aGuard( m_aMutex ); |
109 | |
|
110 | 0 | if ( m_bDisposed ) |
111 | 0 | throw lang::DisposedException(); |
112 | | |
113 | 0 | if ( !m_xInputStream.is() ) |
114 | 0 | throw uno::RuntimeException(); |
115 | | |
116 | 0 | return m_xInputStream->readSomeBytes( aData, nMaxBytesToRead ); |
117 | 0 | } |
118 | | |
119 | | void SAL_CALL OFSInputStreamContainer::skipBytes( sal_Int32 nBytesToSkip ) |
120 | 0 | { |
121 | 0 | std::scoped_lock aGuard( m_aMutex ); |
122 | |
|
123 | 0 | if ( m_bDisposed ) |
124 | 0 | throw lang::DisposedException(); |
125 | | |
126 | 0 | if ( !m_xInputStream.is() ) |
127 | 0 | throw uno::RuntimeException(); |
128 | | |
129 | 0 | m_xInputStream->skipBytes( nBytesToSkip ); |
130 | 0 | } |
131 | | |
132 | | sal_Int32 SAL_CALL OFSInputStreamContainer::available( ) |
133 | 0 | { |
134 | 0 | std::scoped_lock aGuard( m_aMutex ); |
135 | |
|
136 | 0 | if ( m_bDisposed ) |
137 | 0 | throw lang::DisposedException(); |
138 | | |
139 | 0 | if ( !m_xInputStream.is() ) |
140 | 0 | throw uno::RuntimeException(); |
141 | | |
142 | 0 | return m_xInputStream->available(); |
143 | 0 | } |
144 | | |
145 | | void SAL_CALL OFSInputStreamContainer::closeInput( ) |
146 | 0 | { |
147 | 0 | { |
148 | 0 | std::scoped_lock aGuard( m_aMutex ); |
149 | |
|
150 | 0 | if ( m_bDisposed ) |
151 | 0 | throw lang::DisposedException(); |
152 | | |
153 | 0 | if ( !m_xInputStream.is() ) |
154 | 0 | throw uno::RuntimeException(); |
155 | 0 | } |
156 | 0 | dispose(); |
157 | 0 | } |
158 | | |
159 | | uno::Reference< io::XInputStream > SAL_CALL OFSInputStreamContainer::getInputStream() |
160 | 0 | { |
161 | 0 | std::scoped_lock aGuard( m_aMutex ); |
162 | |
|
163 | 0 | if ( m_bDisposed ) |
164 | 0 | throw lang::DisposedException(); |
165 | | |
166 | 0 | if ( !m_xInputStream.is() ) |
167 | 0 | return uno::Reference< io::XInputStream >(); |
168 | | |
169 | 0 | return this; |
170 | 0 | } |
171 | | |
172 | | uno::Reference< io::XOutputStream > SAL_CALL OFSInputStreamContainer::getOutputStream() |
173 | 0 | { |
174 | 0 | std::scoped_lock aGuard( m_aMutex ); |
175 | |
|
176 | 0 | if ( m_bDisposed ) |
177 | 0 | throw lang::DisposedException(); |
178 | | |
179 | 0 | return uno::Reference< io::XOutputStream >(); |
180 | 0 | } |
181 | | |
182 | | void SAL_CALL OFSInputStreamContainer::seek( sal_Int64 location ) |
183 | 0 | { |
184 | 0 | std::scoped_lock aGuard( m_aMutex ); |
185 | |
|
186 | 0 | if ( m_bDisposed ) |
187 | 0 | throw lang::DisposedException(); |
188 | | |
189 | 0 | if ( !m_xSeekable.is() ) |
190 | 0 | throw uno::RuntimeException(); |
191 | | |
192 | 0 | m_xSeekable->seek( location ); |
193 | 0 | } |
194 | | |
195 | | sal_Int64 SAL_CALL OFSInputStreamContainer::getPosition() |
196 | 0 | { |
197 | 0 | std::scoped_lock aGuard( m_aMutex ); |
198 | |
|
199 | 0 | if ( m_bDisposed ) |
200 | 0 | throw lang::DisposedException(); |
201 | | |
202 | 0 | if ( !m_xSeekable.is() ) |
203 | 0 | throw uno::RuntimeException(); |
204 | | |
205 | 0 | return m_xSeekable->getPosition(); |
206 | 0 | } |
207 | | |
208 | | sal_Int64 SAL_CALL OFSInputStreamContainer::getLength() |
209 | 0 | { |
210 | 0 | std::scoped_lock aGuard( m_aMutex ); |
211 | |
|
212 | 0 | if ( m_bDisposed ) |
213 | 0 | throw lang::DisposedException(); |
214 | | |
215 | 0 | if ( !m_xSeekable.is() ) |
216 | 0 | throw uno::RuntimeException(); |
217 | | |
218 | 0 | return m_xSeekable->getLength(); |
219 | 0 | } |
220 | | |
221 | | void SAL_CALL OFSInputStreamContainer::dispose( ) |
222 | 0 | { |
223 | 0 | std::unique_lock aGuard( m_aMutex ); |
224 | |
|
225 | 0 | if ( m_bDisposed ) |
226 | 0 | return; |
227 | | |
228 | 0 | if ( !m_xInputStream.is() ) |
229 | 0 | throw uno::RuntimeException(); |
230 | | |
231 | 0 | m_xInputStream->closeInput(); |
232 | |
|
233 | 0 | lang::EventObject aSource( getXWeak() ); |
234 | 0 | m_aListenersContainer.disposeAndClear( aGuard, aSource ); |
235 | |
|
236 | 0 | m_bDisposed = true; |
237 | 0 | } |
238 | | |
239 | | void SAL_CALL OFSInputStreamContainer::addEventListener( const uno::Reference< lang::XEventListener >& xListener ) |
240 | 0 | { |
241 | 0 | std::unique_lock aGuard( m_aMutex ); |
242 | |
|
243 | 0 | if ( m_bDisposed ) |
244 | 0 | throw lang::DisposedException(); |
245 | | |
246 | 0 | m_aListenersContainer.addInterface( aGuard, xListener ); |
247 | 0 | } |
248 | | |
249 | | void SAL_CALL OFSInputStreamContainer::removeEventListener( const uno::Reference< lang::XEventListener >& xListener ) |
250 | 0 | { |
251 | 0 | std::unique_lock aGuard( m_aMutex ); |
252 | |
|
253 | 0 | if ( m_bDisposed ) |
254 | 0 | throw lang::DisposedException(); |
255 | | |
256 | 0 | m_aListenersContainer.removeInterface( aGuard, xListener ); |
257 | 0 | } |
258 | | |
259 | | |
260 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |