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