1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: streamwrap.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_unotools.hxx"
33 #include <unotools/streamwrap.hxx>
34 #include <tools/stream.hxx>
35 #include <tools/debug.hxx>
40 using namespace ::com::sun::star::uno
;
41 using namespace ::com::sun::star::io
;
42 using namespace ::com::sun::star::lang
;
44 //==================================================================
45 //= OInputStreamWrapper
46 //==================================================================
47 DBG_NAME(OInputStreamWrapper
)
48 //------------------------------------------------------------------
49 OInputStreamWrapper::OInputStreamWrapper( SvStream
& _rStream
)
50 :m_pSvStream(&_rStream
)
51 ,m_bSvStreamOwner(sal_False
)
53 DBG_CTOR(OInputStreamWrapper
,NULL
);
57 //------------------------------------------------------------------
58 OInputStreamWrapper::OInputStreamWrapper( SvStream
* pStream
, sal_Bool bOwner
)
59 :m_pSvStream( pStream
)
60 ,m_bSvStreamOwner( bOwner
)
62 DBG_CTOR(OInputStreamWrapper
,NULL
);
66 //------------------------------------------------------------------
67 OInputStreamWrapper::~OInputStreamWrapper()
69 if( m_bSvStreamOwner
)
72 DBG_DTOR(OInputStreamWrapper
,NULL
);
75 //------------------------------------------------------------------------------
76 sal_Int32 SAL_CALL
OInputStreamWrapper::readBytes(staruno::Sequence
< sal_Int8
>& aData
, sal_Int32 nBytesToRead
)
77 throw( stario::NotConnectedException
, stario::BufferSizeExceededException
, staruno::RuntimeException
)
82 throw stario::BufferSizeExceededException(::rtl::OUString(),static_cast<staruno::XWeak
*>(this));
84 ::osl::MutexGuard
aGuard( m_aMutex
);
86 aData
.realloc(nBytesToRead
);
88 sal_uInt32 nRead
= m_pSvStream
->Read((void*)aData
.getArray(), nBytesToRead
);
91 // Wenn gelesene Zeichen < MaxLength, staruno::Sequence anpassen
92 if (nRead
< (sal_uInt32
)nBytesToRead
)
93 aData
.realloc( nRead
);
98 //------------------------------------------------------------------------------
99 sal_Int32 SAL_CALL
OInputStreamWrapper::readSomeBytes(staruno::Sequence
< sal_Int8
>& aData
, sal_Int32 nMaxBytesToRead
) throw( stario::NotConnectedException
, stario::BufferSizeExceededException
, staruno::RuntimeException
)
103 if (nMaxBytesToRead
< 0)
104 throw stario::BufferSizeExceededException(::rtl::OUString(),static_cast<staruno::XWeak
*>(this));
106 if (m_pSvStream
->IsEof())
112 return readBytes(aData
, nMaxBytesToRead
);
115 //------------------------------------------------------------------------------
116 void SAL_CALL
OInputStreamWrapper::skipBytes(sal_Int32 nBytesToSkip
) throw( stario::NotConnectedException
, stario::BufferSizeExceededException
, staruno::RuntimeException
)
118 ::osl::MutexGuard
aGuard( m_aMutex
);
122 sal_uInt32 nCurrentPos
= m_pSvStream
->Tell();
125 m_pSvStream
->SeekRel(nBytesToSkip
);
129 nCurrentPos
= m_pSvStream
->Tell();
133 //------------------------------------------------------------------------------
134 sal_Int32 SAL_CALL
OInputStreamWrapper::available() throw( stario::NotConnectedException
, staruno::RuntimeException
)
136 ::osl::MutexGuard
aGuard( m_aMutex
);
139 sal_uInt32 nPos
= m_pSvStream
->Tell();
142 m_pSvStream
->Seek(STREAM_SEEK_TO_END
);
145 sal_Int32 nAvailable
= (sal_Int32
)m_pSvStream
->Tell() - nPos
;
146 m_pSvStream
->Seek(nPos
);
152 //------------------------------------------------------------------------------
153 void SAL_CALL
OInputStreamWrapper::closeInput() throw( stario::NotConnectedException
, staruno::RuntimeException
)
155 ::osl::MutexGuard
aGuard( m_aMutex
);
158 if (m_bSvStreamOwner
)
164 //------------------------------------------------------------------------------
165 void OInputStreamWrapper::checkConnected() const
168 throw stario::NotConnectedException(::rtl::OUString(), const_cast<staruno::XWeak
*>(static_cast<const staruno::XWeak
*>(this)));
171 //------------------------------------------------------------------------------
172 void OInputStreamWrapper::checkError() const
176 if (m_pSvStream
->SvStream::GetError() != ERRCODE_NONE
)
177 // TODO: really evaluate the error
178 throw stario::NotConnectedException(::rtl::OUString(), const_cast<staruno::XWeak
*>(static_cast<const staruno::XWeak
*>(this)));
181 //==================================================================
182 //= OSeekableInputStreamWrapper
183 //==================================================================
184 //------------------------------------------------------------------------------
185 OSeekableInputStreamWrapper::OSeekableInputStreamWrapper(SvStream
& _rStream
)
187 SetStream( &_rStream
, FALSE
);
190 //------------------------------------------------------------------------------
191 OSeekableInputStreamWrapper::OSeekableInputStreamWrapper(SvStream
* _pStream
, sal_Bool _bOwner
)
193 SetStream( _pStream
, _bOwner
);
196 //------------------------------------------------------------------------------
197 void SAL_CALL
OSeekableInputStreamWrapper::seek( sal_Int64 _nLocation
) throw (IllegalArgumentException
, IOException
, RuntimeException
)
199 ::osl::MutexGuard
aGuard( m_aMutex
);
202 m_pSvStream
->Seek((sal_uInt32
)_nLocation
);
206 //------------------------------------------------------------------------------
207 sal_Int64 SAL_CALL
OSeekableInputStreamWrapper::getPosition( ) throw (IOException
, RuntimeException
)
209 ::osl::MutexGuard
aGuard( m_aMutex
);
212 sal_uInt32 nPos
= m_pSvStream
->Tell();
214 return (sal_Int64
)nPos
;
217 //------------------------------------------------------------------------------
218 sal_Int64 SAL_CALL
OSeekableInputStreamWrapper::getLength( ) throw (IOException
, RuntimeException
)
220 ::osl::MutexGuard
aGuard( m_aMutex
);
223 sal_uInt32 nCurrentPos
= m_pSvStream
->Tell();
226 m_pSvStream
->Seek(STREAM_SEEK_TO_END
);
227 sal_uInt32 nEndPos
= m_pSvStream
->Tell();
228 m_pSvStream
->Seek(nCurrentPos
);
232 return (sal_Int64
)nEndPos
;
235 //==================================================================
236 //= OOutputStreamWrapper
237 //==================================================================
238 //------------------------------------------------------------------------------
239 void SAL_CALL
OOutputStreamWrapper::writeBytes(const staruno::Sequence
< sal_Int8
>& aData
) throw( stario::NotConnectedException
, stario::BufferSizeExceededException
, staruno::RuntimeException
)
241 sal_uInt32 nWritten
= rStream
.Write(aData
.getConstArray(),aData
.getLength());
242 ErrCode err
= rStream
.GetError();
243 if ( (ERRCODE_NONE
!= err
)
244 || (nWritten
!= (sal_uInt32
)aData
.getLength())
247 throw stario::BufferSizeExceededException(::rtl::OUString(),static_cast<staruno::XWeak
*>(this));
251 //------------------------------------------------------------------
252 void SAL_CALL
OOutputStreamWrapper::flush() throw( stario::NotConnectedException
, stario::BufferSizeExceededException
, staruno::RuntimeException
)
258 //------------------------------------------------------------------
259 void SAL_CALL
OOutputStreamWrapper::closeOutput() throw( stario::NotConnectedException
, stario::BufferSizeExceededException
, staruno::RuntimeException
)
263 //------------------------------------------------------------------------------
264 void OOutputStreamWrapper::checkError() const
266 if (rStream
.GetError() != ERRCODE_NONE
)
267 // TODO: really evaluate the error
268 throw stario::NotConnectedException(::rtl::OUString(), const_cast<staruno::XWeak
*>(static_cast<const staruno::XWeak
*>(this)));
271 //==================================================================
272 //= OSeekableOutputStreamWrapper
273 //==================================================================
274 //------------------------------------------------------------------------------
275 OSeekableOutputStreamWrapper::OSeekableOutputStreamWrapper(SvStream
& _rStream
)
276 :OOutputStreamWrapper(_rStream
)
280 //------------------------------------------------------------------------------
281 Any SAL_CALL
OSeekableOutputStreamWrapper::queryInterface( const Type
& _rType
) throw (RuntimeException
)
283 Any aReturn
= OOutputStreamWrapper::queryInterface(_rType
);
284 if (!aReturn
.hasValue())
285 aReturn
= OSeekableOutputStreamWrapper_Base::queryInterface(_rType
);
289 //------------------------------------------------------------------------------
290 void SAL_CALL
OSeekableOutputStreamWrapper::acquire( ) throw ()
292 OOutputStreamWrapper::acquire();
295 //------------------------------------------------------------------------------
296 void SAL_CALL
OSeekableOutputStreamWrapper::release( ) throw ()
298 OOutputStreamWrapper::release();
301 //------------------------------------------------------------------------------
302 void SAL_CALL
OSeekableOutputStreamWrapper::seek( sal_Int64 _nLocation
) throw (IllegalArgumentException
, IOException
, RuntimeException
)
304 rStream
.Seek((sal_uInt32
)_nLocation
);
308 //------------------------------------------------------------------------------
309 sal_Int64 SAL_CALL
OSeekableOutputStreamWrapper::getPosition( ) throw (IOException
, RuntimeException
)
311 sal_uInt32 nPos
= rStream
.Tell();
313 return (sal_Int64
)nPos
;
316 //------------------------------------------------------------------------------
317 sal_Int64 SAL_CALL
OSeekableOutputStreamWrapper::getLength( ) throw (IOException
, RuntimeException
)
319 sal_uInt32 nCurrentPos
= rStream
.Tell();
322 rStream
.Seek(STREAM_SEEK_TO_END
);
323 sal_uInt32 nEndPos
= rStream
.Tell();
324 rStream
.Seek(nCurrentPos
);
328 return (sal_Int64
)nEndPos
;
331 //------------------------------------------------------------------------------
332 OStreamWrapper::OStreamWrapper(SvStream
& _rStream
)
334 SetStream( &_rStream
, FALSE
);
337 //------------------------------------------------------------------------------
338 ::com::sun::star::uno::Reference
< ::com::sun::star::io::XInputStream
> SAL_CALL
OStreamWrapper::getInputStream( ) throw (::com::sun::star::uno::RuntimeException
)
343 //------------------------------------------------------------------------------
344 ::com::sun::star::uno::Reference
< ::com::sun::star::io::XOutputStream
> SAL_CALL
OStreamWrapper::getOutputStream( ) throw (::com::sun::star::uno::RuntimeException
)
349 //------------------------------------------------------------------------------
350 void SAL_CALL
OStreamWrapper::writeBytes(const staruno::Sequence
< sal_Int8
>& aData
) throw(stario::NotConnectedException
, stario::BufferSizeExceededException
, staruno::RuntimeException
)
352 sal_uInt32 nWritten
= m_pSvStream
->Write(aData
.getConstArray(),aData
.getLength());
353 ErrCode err
= m_pSvStream
->GetError();
354 if ( (ERRCODE_NONE
!= err
)
355 || (nWritten
!= (sal_uInt32
)aData
.getLength())
358 throw stario::BufferSizeExceededException(::rtl::OUString(),static_cast<staruno::XWeak
*>(this));
362 //------------------------------------------------------------------------------
363 void SAL_CALL
OStreamWrapper::flush() throw(stario::NotConnectedException
, stario::BufferSizeExceededException
, staruno::RuntimeException
)
365 m_pSvStream
->Flush();
366 if (m_pSvStream
->GetError() != ERRCODE_NONE
)
367 throw stario::NotConnectedException(::rtl::OUString(),static_cast<staruno::XWeak
*>(this));
370 //------------------------------------------------------------------------------
371 void SAL_CALL
OStreamWrapper::closeOutput() throw(stario::NotConnectedException
, stario::BufferSizeExceededException
, staruno::RuntimeException
)
375 //------------------------------------------------------------------------------
376 void SAL_CALL
OStreamWrapper::truncate() throw(::com::sun::star::io::IOException
, ::com::sun::star::uno::RuntimeException
)
378 m_pSvStream
->SetStreamSize(0);