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