xref: /AOO41X/main/unotools/source/streaming/streamwrap.cxx (revision 1ecadb572e7010ff3b3382ad9bf179dbc6efadbb)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_unotools.hxx"
30 #include <unotools/streamwrap.hxx>
31 #include <tools/stream.hxx>
32 #include <tools/debug.hxx>
33 
34 namespace utl
35 {
36 
37 using namespace ::com::sun::star::uno;
38 using namespace ::com::sun::star::io;
39 using namespace ::com::sun::star::lang;
40 
41 //==================================================================
42 //= OInputStreamWrapper
43 //==================================================================
44 DBG_NAME(OInputStreamWrapper)
45 //------------------------------------------------------------------
46 OInputStreamWrapper::OInputStreamWrapper( SvStream& _rStream )
47 				 :m_pSvStream(&_rStream)
48 				 ,m_bSvStreamOwner(sal_False)
49 {
50 	DBG_CTOR(OInputStreamWrapper,NULL);
51 
52 }
53 
54 //------------------------------------------------------------------
55 OInputStreamWrapper::OInputStreamWrapper( SvStream* pStream, sal_Bool bOwner )
56 				 :m_pSvStream( pStream )
57 				 ,m_bSvStreamOwner( bOwner )
58 {
59 	DBG_CTOR(OInputStreamWrapper,NULL);
60 
61 }
62 
63 //------------------------------------------------------------------
64 OInputStreamWrapper::~OInputStreamWrapper()
65 {
66 	if( m_bSvStreamOwner )
67 		delete m_pSvStream;
68 
69 	DBG_DTOR(OInputStreamWrapper,NULL);
70 }
71 
72 //------------------------------------------------------------------------------
73 sal_Int32 SAL_CALL OInputStreamWrapper::readBytes(staruno::Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead)
74 				throw( stario::NotConnectedException, stario::BufferSizeExceededException, staruno::RuntimeException )
75 {
76 	checkConnected();
77 
78 	if (nBytesToRead < 0)
79 		throw stario::BufferSizeExceededException(::rtl::OUString(),static_cast<staruno::XWeak*>(this));
80 
81 	::osl::MutexGuard aGuard( m_aMutex );
82 
83 	aData.realloc(nBytesToRead);
84 
85 	sal_uInt32 nRead = m_pSvStream->Read((void*)aData.getArray(), nBytesToRead);
86 	checkError();
87 
88 	// Wenn gelesene Zeichen < MaxLength, staruno::Sequence anpassen
89 	if (nRead < (sal_uInt32)nBytesToRead)
90 		aData.realloc( nRead );
91 
92 	return nRead;
93 }
94 
95 //------------------------------------------------------------------------------
96 sal_Int32 SAL_CALL OInputStreamWrapper::readSomeBytes(staruno::Sequence< sal_Int8 >& aData, sal_Int32 nMaxBytesToRead) throw( stario::NotConnectedException, stario::BufferSizeExceededException, staruno::RuntimeException )
97 {
98 	checkError();
99 
100 	if (nMaxBytesToRead < 0)
101 		throw stario::BufferSizeExceededException(::rtl::OUString(),static_cast<staruno::XWeak*>(this));
102 
103 	if (m_pSvStream->IsEof())
104 	{
105 		aData.realloc(0);
106 		return 0;
107 	}
108 	else
109 		return readBytes(aData, nMaxBytesToRead);
110 }
111 
112 //------------------------------------------------------------------------------
113 void SAL_CALL OInputStreamWrapper::skipBytes(sal_Int32 nBytesToSkip) throw( stario::NotConnectedException, stario::BufferSizeExceededException, staruno::RuntimeException )
114 {
115 	::osl::MutexGuard aGuard( m_aMutex );
116 	checkError();
117 
118 #ifdef DBG_UTIL
119 	sal_uInt32 nCurrentPos = m_pSvStream->Tell();
120 #endif
121 
122 	m_pSvStream->SeekRel(nBytesToSkip);
123 	checkError();
124 
125 #ifdef DBG_UTIL
126 	nCurrentPos = m_pSvStream->Tell();
127 #endif
128 }
129 
130 //------------------------------------------------------------------------------
131 sal_Int32 SAL_CALL OInputStreamWrapper::available() throw( stario::NotConnectedException, staruno::RuntimeException )
132 {
133 	::osl::MutexGuard aGuard( m_aMutex );
134 	checkConnected();
135 
136 	sal_uInt32 nPos = m_pSvStream->Tell();
137 	checkError();
138 
139 	m_pSvStream->Seek(STREAM_SEEK_TO_END);
140 	checkError();
141 
142 	sal_Int32 nAvailable = (sal_Int32)m_pSvStream->Tell() - nPos;
143 	m_pSvStream->Seek(nPos);
144 	checkError();
145 
146 	return nAvailable;
147 }
148 
149 //------------------------------------------------------------------------------
150 void SAL_CALL OInputStreamWrapper::closeInput() throw( stario::NotConnectedException, staruno::RuntimeException )
151 {
152 	::osl::MutexGuard aGuard( m_aMutex );
153 	checkConnected();
154 
155 	if (m_bSvStreamOwner)
156 		delete m_pSvStream;
157 
158 	m_pSvStream = NULL;
159 }
160 
161 //------------------------------------------------------------------------------
162 void OInputStreamWrapper::checkConnected() const
163 {
164 	if (!m_pSvStream)
165 		throw stario::NotConnectedException(::rtl::OUString(), const_cast<staruno::XWeak*>(static_cast<const staruno::XWeak*>(this)));
166 }
167 
168 //------------------------------------------------------------------------------
169 void OInputStreamWrapper::checkError() const
170 {
171 	checkConnected();
172 
173 	if (m_pSvStream->SvStream::GetError() != ERRCODE_NONE)
174 		// TODO: really evaluate the error
175 		throw stario::NotConnectedException(::rtl::OUString(), const_cast<staruno::XWeak*>(static_cast<const staruno::XWeak*>(this)));
176 }
177 
178 //==================================================================
179 //= OSeekableInputStreamWrapper
180 //==================================================================
181 //------------------------------------------------------------------------------
182 OSeekableInputStreamWrapper::OSeekableInputStreamWrapper(SvStream& _rStream)
183 {
184     SetStream( &_rStream, sal_False );
185 }
186 
187 //------------------------------------------------------------------------------
188 OSeekableInputStreamWrapper::OSeekableInputStreamWrapper(SvStream* _pStream, sal_Bool _bOwner)
189 {
190     SetStream( _pStream, _bOwner );
191 }
192 
193 //------------------------------------------------------------------------------
194 void SAL_CALL OSeekableInputStreamWrapper::seek( sal_Int64 _nLocation ) throw (IllegalArgumentException, IOException, RuntimeException)
195 {
196 	::osl::MutexGuard aGuard( m_aMutex );
197 	checkConnected();
198 
199 	m_pSvStream->Seek((sal_uInt32)_nLocation);
200 	checkError();
201 }
202 
203 //------------------------------------------------------------------------------
204 sal_Int64 SAL_CALL OSeekableInputStreamWrapper::getPosition(  ) throw (IOException, RuntimeException)
205 {
206 	::osl::MutexGuard aGuard( m_aMutex );
207 	checkConnected();
208 
209 	sal_uInt32 nPos = m_pSvStream->Tell();
210 	checkError();
211 	return (sal_Int64)nPos;
212 }
213 
214 //------------------------------------------------------------------------------
215 sal_Int64 SAL_CALL OSeekableInputStreamWrapper::getLength(  ) throw (IOException, RuntimeException)
216 {
217 	::osl::MutexGuard aGuard( m_aMutex );
218 	checkConnected();
219 
220 	sal_uInt32 nCurrentPos = m_pSvStream->Tell();
221 	checkError();
222 
223 	m_pSvStream->Seek(STREAM_SEEK_TO_END);
224 	sal_uInt32 nEndPos = m_pSvStream->Tell();
225 	m_pSvStream->Seek(nCurrentPos);
226 
227 	checkError();
228 
229 	return (sal_Int64)nEndPos;
230 }
231 
232 //==================================================================
233 //= OOutputStreamWrapper
234 //==================================================================
235 //------------------------------------------------------------------------------
236 void SAL_CALL OOutputStreamWrapper::writeBytes(const staruno::Sequence< sal_Int8 >& aData) throw( stario::NotConnectedException, stario::BufferSizeExceededException, staruno::RuntimeException )
237 {
238 	sal_uInt32 nWritten = rStream.Write(aData.getConstArray(),aData.getLength());
239 	ErrCode err = rStream.GetError();
240 	if	(	(ERRCODE_NONE != err)
241 		||	(nWritten != (sal_uInt32)aData.getLength())
242 		)
243 	{
244 		throw stario::BufferSizeExceededException(::rtl::OUString(),static_cast<staruno::XWeak*>(this));
245 	}
246 }
247 
248 //------------------------------------------------------------------
249 void SAL_CALL OOutputStreamWrapper::flush() throw( stario::NotConnectedException, stario::BufferSizeExceededException, staruno::RuntimeException )
250 {
251 	rStream.Flush();
252     checkError();
253 }
254 
255 //------------------------------------------------------------------
256 void SAL_CALL OOutputStreamWrapper::closeOutput() throw( stario::NotConnectedException, stario::BufferSizeExceededException, staruno::RuntimeException )
257 {
258 }
259 
260 //------------------------------------------------------------------------------
261 void OOutputStreamWrapper::checkError() const
262 {
263 	if (rStream.GetError() != ERRCODE_NONE)
264 		// TODO: really evaluate the error
265 		throw stario::NotConnectedException(::rtl::OUString(), const_cast<staruno::XWeak*>(static_cast<const staruno::XWeak*>(this)));
266 }
267 
268 //==================================================================
269 //= OSeekableOutputStreamWrapper
270 //==================================================================
271 //------------------------------------------------------------------------------
272 OSeekableOutputStreamWrapper::OSeekableOutputStreamWrapper(SvStream& _rStream)
273 	:OOutputStreamWrapper(_rStream)
274 {
275 }
276 
277 //------------------------------------------------------------------------------
278 Any SAL_CALL OSeekableOutputStreamWrapper::queryInterface( const Type& _rType ) throw (RuntimeException)
279 {
280 	Any aReturn = OOutputStreamWrapper::queryInterface(_rType);
281 	if (!aReturn.hasValue())
282 		aReturn = OSeekableOutputStreamWrapper_Base::queryInterface(_rType);
283 	return aReturn;
284 }
285 
286 //------------------------------------------------------------------------------
287 void SAL_CALL OSeekableOutputStreamWrapper::acquire(  ) throw ()
288 {
289 	OOutputStreamWrapper::acquire();
290 }
291 
292 //------------------------------------------------------------------------------
293 void SAL_CALL OSeekableOutputStreamWrapper::release(  ) throw ()
294 {
295 	OOutputStreamWrapper::release();
296 }
297 
298 //------------------------------------------------------------------------------
299 void SAL_CALL OSeekableOutputStreamWrapper::seek( sal_Int64 _nLocation ) throw (IllegalArgumentException, IOException, RuntimeException)
300 {
301 	rStream.Seek((sal_uInt32)_nLocation);
302 	checkError();
303 }
304 
305 //------------------------------------------------------------------------------
306 sal_Int64 SAL_CALL OSeekableOutputStreamWrapper::getPosition(  ) throw (IOException, RuntimeException)
307 {
308 	sal_uInt32 nPos = rStream.Tell();
309 	checkError();
310 	return (sal_Int64)nPos;
311 }
312 
313 //------------------------------------------------------------------------------
314 sal_Int64 SAL_CALL OSeekableOutputStreamWrapper::getLength(  ) throw (IOException, RuntimeException)
315 {
316 	sal_uInt32 nCurrentPos = rStream.Tell();
317 	checkError();
318 
319 	rStream.Seek(STREAM_SEEK_TO_END);
320 	sal_uInt32 nEndPos = rStream.Tell();
321 	rStream.Seek(nCurrentPos);
322 
323 	checkError();
324 
325 	return (sal_Int64)nEndPos;
326 }
327 
328 //------------------------------------------------------------------------------
329 OStreamWrapper::OStreamWrapper(SvStream& _rStream)
330 {
331     SetStream( &_rStream, sal_False );
332 }
333 
334 //------------------------------------------------------------------------------
335 ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream > SAL_CALL OStreamWrapper::getInputStream(  ) throw (::com::sun::star::uno::RuntimeException)
336 {
337     return this;
338 }
339 
340 //------------------------------------------------------------------------------
341 ::com::sun::star::uno::Reference< ::com::sun::star::io::XOutputStream > SAL_CALL OStreamWrapper::getOutputStream(  ) throw (::com::sun::star::uno::RuntimeException)
342 {
343     return this;
344 }
345 
346 //------------------------------------------------------------------------------
347 void SAL_CALL OStreamWrapper::writeBytes(const staruno::Sequence< sal_Int8 >& aData) throw(stario::NotConnectedException, stario::BufferSizeExceededException, staruno::RuntimeException)
348 {
349     sal_uInt32 nWritten = m_pSvStream->Write(aData.getConstArray(),aData.getLength());
350     ErrCode err = m_pSvStream->GetError();
351 	if	(	(ERRCODE_NONE != err)
352 		||	(nWritten != (sal_uInt32)aData.getLength())
353 		)
354 	{
355 		throw stario::BufferSizeExceededException(::rtl::OUString(),static_cast<staruno::XWeak*>(this));
356 	}
357 }
358 
359 //------------------------------------------------------------------------------
360 void SAL_CALL OStreamWrapper::flush() throw(stario::NotConnectedException, stario::BufferSizeExceededException, staruno::RuntimeException)
361 {
362     m_pSvStream->Flush();
363     if (m_pSvStream->GetError() != ERRCODE_NONE)
364 		throw stario::NotConnectedException(::rtl::OUString(),static_cast<staruno::XWeak*>(this));
365 }
366 
367 //------------------------------------------------------------------------------
368 void SAL_CALL OStreamWrapper::closeOutput() throw(stario::NotConnectedException, stario::BufferSizeExceededException, staruno::RuntimeException)
369 {
370 }
371 
372 //------------------------------------------------------------------------------
373 void SAL_CALL OStreamWrapper::truncate() throw(::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException)
374 {
375     m_pSvStream->SetStreamSize(0);
376 }
377 
378 } // namespace utl
379 
380