Branch data 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 <unotools/streamwrap.hxx>
21 : : #include <tools/stream.hxx>
22 : : #include <tools/debug.hxx>
23 : :
24 : : namespace utl
25 : : {
26 : :
27 : : using namespace ::com::sun::star::uno;
28 : : using namespace ::com::sun::star::io;
29 : : using namespace ::com::sun::star::lang;
30 : :
31 : : //==================================================================
32 : : //= OInputStreamWrapper
33 : : //==================================================================
34 : : DBG_NAME(OInputStreamWrapper)
35 : : //------------------------------------------------------------------
36 : 2416 : OInputStreamWrapper::OInputStreamWrapper( SvStream& _rStream )
37 : : :m_pSvStream(&_rStream)
38 [ + - ]: 2416 : ,m_bSvStreamOwner(sal_False)
39 : : {
40 : : DBG_CTOR(OInputStreamWrapper,NULL);
41 : :
42 : 2416 : }
43 : :
44 : : //------------------------------------------------------------------
45 : 19 : OInputStreamWrapper::OInputStreamWrapper( SvStream* pStream, sal_Bool bOwner )
46 : : :m_pSvStream( pStream )
47 [ + - ]: 19 : ,m_bSvStreamOwner( bOwner )
48 : : {
49 : : DBG_CTOR(OInputStreamWrapper,NULL);
50 : :
51 : 19 : }
52 : :
53 : : //------------------------------------------------------------------
54 [ + - ]: 3154 : OInputStreamWrapper::~OInputStreamWrapper()
55 : : {
56 [ + + ]: 3154 : if( m_bSvStreamOwner )
57 [ + - ][ + - ]: 4 : delete m_pSvStream;
58 : :
59 : : DBG_DTOR(OInputStreamWrapper,NULL);
60 [ - + ]: 5589 : }
61 : :
62 : : //------------------------------------------------------------------------------
63 : 6571 : sal_Int32 SAL_CALL OInputStreamWrapper::readBytes(staruno::Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead)
64 : : throw( stario::NotConnectedException, stario::BufferSizeExceededException, staruno::RuntimeException )
65 : : {
66 [ + - ]: 6571 : checkConnected();
67 : :
68 [ - + ]: 6571 : if (nBytesToRead < 0)
69 [ # # ][ # # ]: 0 : throw stario::BufferSizeExceededException(::rtl::OUString(),static_cast<staruno::XWeak*>(this));
70 : :
71 [ + - ]: 6571 : ::osl::MutexGuard aGuard( m_aMutex );
72 : :
73 [ + - ]: 6571 : aData.realloc(nBytesToRead);
74 : :
75 [ + - ][ + - ]: 6571 : sal_uInt32 nRead = m_pSvStream->Read((void*)aData.getArray(), nBytesToRead);
76 [ + - ]: 6571 : checkError();
77 : :
78 : : // Wenn gelesene Zeichen < MaxLength, staruno::Sequence anpassen
79 [ + + ]: 6571 : if (nRead < (sal_uInt32)nBytesToRead)
80 [ + - ]: 766 : aData.realloc( nRead );
81 : :
82 [ + - ]: 6571 : return nRead;
83 : : }
84 : :
85 : : //------------------------------------------------------------------------------
86 : 8 : sal_Int32 SAL_CALL OInputStreamWrapper::readSomeBytes(staruno::Sequence< sal_Int8 >& aData, sal_Int32 nMaxBytesToRead) throw( stario::NotConnectedException, stario::BufferSizeExceededException, staruno::RuntimeException )
87 : : {
88 : 8 : checkError();
89 : :
90 [ - + ]: 8 : if (nMaxBytesToRead < 0)
91 [ # # ][ # # ]: 0 : throw stario::BufferSizeExceededException(::rtl::OUString(),static_cast<staruno::XWeak*>(this));
92 : :
93 [ + + ]: 8 : if (m_pSvStream->IsEof())
94 : : {
95 : 4 : aData.realloc(0);
96 : 4 : return 0;
97 : : }
98 : : else
99 : 8 : return readBytes(aData, nMaxBytesToRead);
100 : : }
101 : :
102 : : //------------------------------------------------------------------------------
103 : 294 : void SAL_CALL OInputStreamWrapper::skipBytes(sal_Int32 nBytesToSkip) throw( stario::NotConnectedException, stario::BufferSizeExceededException, staruno::RuntimeException )
104 : : {
105 [ + - ]: 294 : ::osl::MutexGuard aGuard( m_aMutex );
106 [ + - ]: 294 : checkError();
107 : :
108 [ + - ]: 294 : m_pSvStream->SeekRel(nBytesToSkip);
109 [ + - ][ + - ]: 294 : checkError();
110 : 294 : }
111 : :
112 : : //------------------------------------------------------------------------------
113 : 90 : sal_Int32 SAL_CALL OInputStreamWrapper::available() throw( stario::NotConnectedException, staruno::RuntimeException )
114 : : {
115 [ + - ]: 90 : ::osl::MutexGuard aGuard( m_aMutex );
116 [ + - ]: 90 : checkConnected();
117 : :
118 : 90 : sal_uInt32 nPos = m_pSvStream->Tell();
119 [ + - ]: 90 : checkError();
120 : :
121 [ + - ]: 90 : m_pSvStream->Seek(STREAM_SEEK_TO_END);
122 [ + - ]: 90 : checkError();
123 : :
124 : 90 : sal_Int32 nAvailable = (sal_Int32)m_pSvStream->Tell() - nPos;
125 [ + - ]: 90 : m_pSvStream->Seek(nPos);
126 [ + - ]: 90 : checkError();
127 : :
128 [ + - ]: 90 : return nAvailable;
129 : : }
130 : :
131 : : //------------------------------------------------------------------------------
132 : 110 : void SAL_CALL OInputStreamWrapper::closeInput() throw( stario::NotConnectedException, staruno::RuntimeException )
133 : : {
134 [ + - ]: 110 : ::osl::MutexGuard aGuard( m_aMutex );
135 [ + - ]: 110 : checkConnected();
136 : :
137 [ - + ]: 110 : if (m_bSvStreamOwner)
138 [ # # ][ # # ]: 0 : delete m_pSvStream;
139 : :
140 [ + - ]: 110 : m_pSvStream = NULL;
141 : 110 : }
142 : :
143 : : //------------------------------------------------------------------------------
144 : 23682 : void OInputStreamWrapper::checkConnected() const
145 : : {
146 [ - + ]: 23682 : if (!m_pSvStream)
147 [ # # ][ # # ]: 0 : throw stario::NotConnectedException(::rtl::OUString(), const_cast<staruno::XWeak*>(static_cast<const staruno::XWeak*>(this)));
148 : 23682 : }
149 : :
150 : : //------------------------------------------------------------------------------
151 : 12680 : void OInputStreamWrapper::checkError() const
152 : : {
153 : 12680 : checkConnected();
154 : :
155 [ - + ]: 12680 : if (m_pSvStream->SvStream::GetError() != ERRCODE_NONE)
156 : : // TODO: really evaluate the error
157 [ # # ][ # # ]: 0 : throw stario::NotConnectedException(::rtl::OUString(), const_cast<staruno::XWeak*>(static_cast<const staruno::XWeak*>(this)));
158 : 12680 : }
159 : :
160 : : //==================================================================
161 : : //= OSeekableInputStreamWrapper
162 : : //==================================================================
163 : : //------------------------------------------------------------------------------
164 : 497 : OSeekableInputStreamWrapper::OSeekableInputStreamWrapper(SvStream& _rStream)
165 : : {
166 : 497 : SetStream( &_rStream, sal_False );
167 : 497 : }
168 : :
169 : : //------------------------------------------------------------------------------
170 : 0 : OSeekableInputStreamWrapper::OSeekableInputStreamWrapper(SvStream* _pStream, sal_Bool _bOwner)
171 : : {
172 : 0 : SetStream( _pStream, _bOwner );
173 : 0 : }
174 : :
175 : : //------------------------------------------------------------------------------
176 : 2156 : void SAL_CALL OSeekableInputStreamWrapper::seek( sal_Int64 _nLocation ) throw (IllegalArgumentException, IOException, RuntimeException)
177 : : {
178 [ + - ]: 2156 : ::osl::MutexGuard aGuard( m_aMutex );
179 [ + - ]: 2156 : checkConnected();
180 : :
181 [ + - ]: 2156 : m_pSvStream->Seek((sal_uInt32)_nLocation);
182 [ + - ][ + - ]: 2156 : checkError();
183 : 2156 : }
184 : :
185 : : //------------------------------------------------------------------------------
186 : 1063 : sal_Int64 SAL_CALL OSeekableInputStreamWrapper::getPosition( ) throw (IOException, RuntimeException)
187 : : {
188 [ + - ]: 1063 : ::osl::MutexGuard aGuard( m_aMutex );
189 [ + - ]: 1063 : checkConnected();
190 : :
191 : 1063 : sal_uInt32 nPos = m_pSvStream->Tell();
192 [ + - ]: 1063 : checkError();
193 [ + - ]: 1063 : return (sal_Int64)nPos;
194 : : }
195 : :
196 : : //------------------------------------------------------------------------------
197 : 1012 : sal_Int64 SAL_CALL OSeekableInputStreamWrapper::getLength( ) throw (IOException, RuntimeException)
198 : : {
199 [ + - ]: 1012 : ::osl::MutexGuard aGuard( m_aMutex );
200 [ + - ]: 1012 : checkConnected();
201 : :
202 : 1012 : sal_uInt32 nCurrentPos = m_pSvStream->Tell();
203 [ + - ]: 1012 : checkError();
204 : :
205 [ + - ]: 1012 : m_pSvStream->Seek(STREAM_SEEK_TO_END);
206 : 1012 : sal_uInt32 nEndPos = m_pSvStream->Tell();
207 [ + - ]: 1012 : m_pSvStream->Seek(nCurrentPos);
208 : :
209 [ + - ]: 1012 : checkError();
210 : :
211 [ + - ]: 1012 : return (sal_Int64)nEndPos;
212 : : }
213 : :
214 : : //==================================================================
215 : : //= OOutputStreamWrapper
216 : : //==================================================================
217 : :
218 : 144 : OOutputStreamWrapper::OOutputStreamWrapper(SvStream& _rStream):
219 : 144 : rStream(_rStream)
220 : 144 : {}
221 : :
222 [ - + ]: 279 : OOutputStreamWrapper::~OOutputStreamWrapper() {}
223 : :
224 : 183 : void SAL_CALL OOutputStreamWrapper::writeBytes(const staruno::Sequence< sal_Int8 >& aData) throw( stario::NotConnectedException, stario::BufferSizeExceededException, staruno::RuntimeException )
225 : : {
226 : 183 : sal_uInt32 nWritten = rStream.Write(aData.getConstArray(),aData.getLength());
227 : 183 : ErrCode err = rStream.GetError();
228 [ - + ]: 366 : if ( (ERRCODE_NONE != err)
[ + - - + ]
229 : 183 : || (nWritten != (sal_uInt32)aData.getLength())
230 : : )
231 : : {
232 [ # # ][ # # ]: 0 : throw stario::BufferSizeExceededException(::rtl::OUString(),static_cast<staruno::XWeak*>(this));
233 : : }
234 : 183 : }
235 : :
236 : : //------------------------------------------------------------------
237 : 9 : void SAL_CALL OOutputStreamWrapper::flush() throw( stario::NotConnectedException, stario::BufferSizeExceededException, staruno::RuntimeException )
238 : : {
239 : 9 : rStream.Flush();
240 : 9 : checkError();
241 : 9 : }
242 : :
243 : : //------------------------------------------------------------------
244 : 9 : void SAL_CALL OOutputStreamWrapper::closeOutput() throw( stario::NotConnectedException, stario::BufferSizeExceededException, staruno::RuntimeException )
245 : : {
246 : 9 : }
247 : :
248 : : //------------------------------------------------------------------------------
249 : 240 : void OOutputStreamWrapper::checkError() const
250 : : {
251 [ - + ]: 240 : if (rStream.GetError() != ERRCODE_NONE)
252 : : // TODO: really evaluate the error
253 [ # # ][ # # ]: 0 : throw stario::NotConnectedException(::rtl::OUString(), const_cast<staruno::XWeak*>(static_cast<const staruno::XWeak*>(this)));
254 : 240 : }
255 : :
256 : : //==================================================================
257 : : //= OSeekableOutputStreamWrapper
258 : : //==================================================================
259 : : //------------------------------------------------------------------------------
260 : 9 : OSeekableOutputStreamWrapper::OSeekableOutputStreamWrapper(SvStream& _rStream)
261 : 9 : :OOutputStreamWrapper(_rStream)
262 : : {
263 : 9 : }
264 : :
265 [ - + ]: 18 : OSeekableOutputStreamWrapper::~OSeekableOutputStreamWrapper() {}
266 : :
267 : : //------------------------------------------------------------------------------
268 : 18 : Any SAL_CALL OSeekableOutputStreamWrapper::queryInterface( const Type& _rType ) throw (RuntimeException)
269 : : {
270 : 18 : Any aReturn = OOutputStreamWrapper::queryInterface(_rType);
271 [ + - ]: 18 : if (!aReturn.hasValue())
272 [ + - ]: 18 : aReturn = OSeekableOutputStreamWrapper_Base::queryInterface(_rType);
273 : 18 : return aReturn;
274 : : }
275 : :
276 : : //------------------------------------------------------------------------------
277 : 63 : void SAL_CALL OSeekableOutputStreamWrapper::acquire( ) throw ()
278 : : {
279 : 63 : OOutputStreamWrapper::acquire();
280 : 63 : }
281 : :
282 : : //------------------------------------------------------------------------------
283 : 63 : void SAL_CALL OSeekableOutputStreamWrapper::release( ) throw ()
284 : : {
285 : 63 : OOutputStreamWrapper::release();
286 : 63 : }
287 : :
288 : : //------------------------------------------------------------------------------
289 : 12 : void SAL_CALL OSeekableOutputStreamWrapper::seek( sal_Int64 _nLocation ) throw (IllegalArgumentException, IOException, RuntimeException)
290 : : {
291 : 12 : rStream.Seek((sal_uInt32)_nLocation);
292 : 12 : checkError();
293 : 12 : }
294 : :
295 : : //------------------------------------------------------------------------------
296 : 207 : sal_Int64 SAL_CALL OSeekableOutputStreamWrapper::getPosition( ) throw (IOException, RuntimeException)
297 : : {
298 : 207 : sal_uInt32 nPos = rStream.Tell();
299 : 207 : checkError();
300 : 207 : return (sal_Int64)nPos;
301 : : }
302 : :
303 : : //------------------------------------------------------------------------------
304 : 6 : sal_Int64 SAL_CALL OSeekableOutputStreamWrapper::getLength( ) throw (IOException, RuntimeException)
305 : : {
306 : 6 : sal_uInt32 nCurrentPos = rStream.Tell();
307 : 6 : checkError();
308 : :
309 : 6 : rStream.Seek(STREAM_SEEK_TO_END);
310 : 6 : sal_uInt32 nEndPos = rStream.Tell();
311 : 6 : rStream.Seek(nCurrentPos);
312 : :
313 : 6 : checkError();
314 : :
315 : 6 : return (sal_Int64)nEndPos;
316 : : }
317 : :
318 : : //------------------------------------------------------------------------------
319 : 225 : OStreamWrapper::OStreamWrapper(SvStream& _rStream)
320 : : {
321 : 225 : SetStream( &_rStream, sal_False );
322 : 225 : }
323 : :
324 : : //------------------------------------------------------------------------------
325 : 249 : ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream > SAL_CALL OStreamWrapper::getInputStream( ) throw (::com::sun::star::uno::RuntimeException)
326 : : {
327 : 249 : return this;
328 : : }
329 : :
330 : : //------------------------------------------------------------------------------
331 : 270 : ::com::sun::star::uno::Reference< ::com::sun::star::io::XOutputStream > SAL_CALL OStreamWrapper::getOutputStream( ) throw (::com::sun::star::uno::RuntimeException)
332 : : {
333 : 270 : return this;
334 : : }
335 : :
336 : : //------------------------------------------------------------------------------
337 : 18214 : void SAL_CALL OStreamWrapper::writeBytes(const staruno::Sequence< sal_Int8 >& aData) throw(stario::NotConnectedException, stario::BufferSizeExceededException, staruno::RuntimeException)
338 : : {
339 : 18214 : sal_uInt32 nWritten = m_pSvStream->Write(aData.getConstArray(),aData.getLength());
340 : 18214 : ErrCode err = m_pSvStream->GetError();
341 [ - + ]: 36428 : if ( (ERRCODE_NONE != err)
[ + - - + ]
342 : 18214 : || (nWritten != (sal_uInt32)aData.getLength())
343 : : )
344 : : {
345 [ # # ][ # # ]: 0 : throw stario::BufferSizeExceededException(::rtl::OUString(),static_cast<staruno::XWeak*>(this));
346 : : }
347 : 18214 : }
348 : :
349 : : //------------------------------------------------------------------------------
350 : 282 : void SAL_CALL OStreamWrapper::flush() throw(stario::NotConnectedException, stario::BufferSizeExceededException, staruno::RuntimeException)
351 : : {
352 : 282 : m_pSvStream->Flush();
353 [ - + ]: 282 : if (m_pSvStream->GetError() != ERRCODE_NONE)
354 [ # # ][ # # ]: 0 : throw stario::NotConnectedException(::rtl::OUString(),static_cast<staruno::XWeak*>(this));
355 : 282 : }
356 : :
357 : : //------------------------------------------------------------------------------
358 : 180 : void SAL_CALL OStreamWrapper::closeOutput() throw(stario::NotConnectedException, stario::BufferSizeExceededException, staruno::RuntimeException)
359 : : {
360 : 180 : }
361 : :
362 : : //------------------------------------------------------------------------------
363 : 0 : void SAL_CALL OStreamWrapper::truncate() throw(::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException)
364 : : {
365 : 0 : m_pSvStream->SetStreamSize(0);
366 : 0 : }
367 : :
368 : : } // namespace utl
369 : :
370 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|