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