1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <unotools/streamwrap.hxx>
21 #include <tools/stream.hxx>
22 #include <tools/debug.hxx>
27 using namespace ::com::sun::star::uno
;
28 using namespace ::com::sun::star::io
;
29 using namespace ::com::sun::star::lang
;
31 OInputStreamWrapper::OInputStreamWrapper( SvStream
& _rStream
)
32 :m_pSvStream(&_rStream
)
33 ,m_bSvStreamOwner(false)
37 OInputStreamWrapper::OInputStreamWrapper( SvStream
* pStream
, bool bOwner
)
38 :m_pSvStream( pStream
)
39 ,m_bSvStreamOwner( bOwner
)
43 OInputStreamWrapper::~OInputStreamWrapper()
45 if( m_bSvStreamOwner
)
49 sal_Int32 SAL_CALL
OInputStreamWrapper::readBytes(staruno::Sequence
< sal_Int8
>& aData
, sal_Int32 nBytesToRead
)
50 throw( stario::NotConnectedException
, stario::BufferSizeExceededException
, staruno::RuntimeException
, std::exception
)
55 throw stario::BufferSizeExceededException(OUString(),static_cast<staruno::XWeak
*>(this));
57 ::osl::MutexGuard
aGuard( m_aMutex
);
59 aData
.realloc(nBytesToRead
);
61 sal_uInt32 nRead
= m_pSvStream
->Read((void*)aData
.getArray(), nBytesToRead
);
64 // Wenn gelesene Zeichen < MaxLength, staruno::Sequence anpassen
65 if (nRead
< (sal_uInt32
)nBytesToRead
)
66 aData
.realloc( nRead
);
71 sal_Int32 SAL_CALL
OInputStreamWrapper::readSomeBytes(staruno::Sequence
< sal_Int8
>& aData
, sal_Int32 nMaxBytesToRead
) throw( stario::NotConnectedException
, stario::BufferSizeExceededException
, staruno::RuntimeException
, std::exception
)
75 if (nMaxBytesToRead
< 0)
76 throw stario::BufferSizeExceededException(OUString(),static_cast<staruno::XWeak
*>(this));
78 if (m_pSvStream
->IsEof())
84 return readBytes(aData
, nMaxBytesToRead
);
87 void SAL_CALL
OInputStreamWrapper::skipBytes(sal_Int32 nBytesToSkip
) throw( stario::NotConnectedException
, stario::BufferSizeExceededException
, staruno::RuntimeException
, std::exception
)
89 ::osl::MutexGuard
aGuard( m_aMutex
);
92 m_pSvStream
->SeekRel(nBytesToSkip
);
96 sal_Int32 SAL_CALL
OInputStreamWrapper::available() throw( stario::NotConnectedException
, staruno::RuntimeException
, std::exception
)
98 ::osl::MutexGuard
aGuard( m_aMutex
);
101 sal_uInt32 nPos
= m_pSvStream
->Tell();
104 m_pSvStream
->Seek(STREAM_SEEK_TO_END
);
107 sal_Int32 nAvailable
= (sal_Int32
)m_pSvStream
->Tell() - nPos
;
108 m_pSvStream
->Seek(nPos
);
114 void SAL_CALL
OInputStreamWrapper::closeInput() throw( stario::NotConnectedException
, staruno::RuntimeException
, std::exception
)
116 ::osl::MutexGuard
aGuard( m_aMutex
);
119 if (m_bSvStreamOwner
)
125 void OInputStreamWrapper::checkConnected() const
128 throw stario::NotConnectedException(OUString(), const_cast<staruno::XWeak
*>(static_cast<const staruno::XWeak
*>(this)));
131 void OInputStreamWrapper::checkError() const
135 if (m_pSvStream
->SvStream::GetError() != ERRCODE_NONE
)
136 // TODO: really evaluate the error
137 throw stario::NotConnectedException(OUString(), const_cast<staruno::XWeak
*>(static_cast<const staruno::XWeak
*>(this)));
140 //= OSeekableInputStreamWrapper
142 OSeekableInputStreamWrapper::OSeekableInputStreamWrapper(SvStream
& _rStream
)
144 SetStream( &_rStream
, false );
147 OSeekableInputStreamWrapper::OSeekableInputStreamWrapper(SvStream
* _pStream
, bool _bOwner
)
149 SetStream( _pStream
, _bOwner
);
152 void SAL_CALL
OSeekableInputStreamWrapper::seek( sal_Int64 _nLocation
) throw (IllegalArgumentException
, IOException
, RuntimeException
, std::exception
)
154 ::osl::MutexGuard
aGuard( m_aMutex
);
157 m_pSvStream
->Seek((sal_uInt32
)_nLocation
);
161 sal_Int64 SAL_CALL
OSeekableInputStreamWrapper::getPosition( ) throw (IOException
, RuntimeException
, std::exception
)
163 ::osl::MutexGuard
aGuard( m_aMutex
);
166 sal_uInt32 nPos
= m_pSvStream
->Tell();
168 return (sal_Int64
)nPos
;
171 sal_Int64 SAL_CALL
OSeekableInputStreamWrapper::getLength( ) throw (IOException
, RuntimeException
, std::exception
)
173 ::osl::MutexGuard
aGuard( m_aMutex
);
176 sal_uInt32 nCurrentPos
= m_pSvStream
->Tell();
179 m_pSvStream
->Seek(STREAM_SEEK_TO_END
);
180 sal_uInt32 nEndPos
= m_pSvStream
->Tell();
181 m_pSvStream
->Seek(nCurrentPos
);
185 return (sal_Int64
)nEndPos
;
188 //= OOutputStreamWrapper
190 OOutputStreamWrapper::OOutputStreamWrapper(SvStream
& _rStream
):
194 OOutputStreamWrapper::~OOutputStreamWrapper() {}
196 void SAL_CALL
OOutputStreamWrapper::writeBytes(const staruno::Sequence
< sal_Int8
>& aData
) throw( stario::NotConnectedException
, stario::BufferSizeExceededException
, staruno::RuntimeException
, std::exception
)
198 sal_uInt32 nWritten
= rStream
.Write(aData
.getConstArray(),aData
.getLength());
199 ErrCode err
= rStream
.GetError();
200 if ( (ERRCODE_NONE
!= err
)
201 || (nWritten
!= (sal_uInt32
)aData
.getLength())
204 throw stario::BufferSizeExceededException(OUString(),static_cast<staruno::XWeak
*>(this));
208 void SAL_CALL
OOutputStreamWrapper::flush() throw( stario::NotConnectedException
, stario::BufferSizeExceededException
, staruno::RuntimeException
, std::exception
)
214 void SAL_CALL
OOutputStreamWrapper::closeOutput() throw( stario::NotConnectedException
, stario::BufferSizeExceededException
, staruno::RuntimeException
, std::exception
)
218 void OOutputStreamWrapper::checkError() const
220 if (rStream
.GetError() != ERRCODE_NONE
)
221 // TODO: really evaluate the error
222 throw stario::NotConnectedException(OUString(), const_cast<staruno::XWeak
*>(static_cast<const staruno::XWeak
*>(this)));
225 //= OSeekableOutputStreamWrapper
227 OSeekableOutputStreamWrapper::OSeekableOutputStreamWrapper(SvStream
& _rStream
)
228 :OOutputStreamWrapper(_rStream
)
232 OSeekableOutputStreamWrapper::~OSeekableOutputStreamWrapper() {}
234 Any SAL_CALL
OSeekableOutputStreamWrapper::queryInterface( const Type
& _rType
) throw (RuntimeException
, std::exception
)
236 Any aReturn
= OOutputStreamWrapper::queryInterface(_rType
);
237 if (!aReturn
.hasValue())
238 aReturn
= OSeekableOutputStreamWrapper_Base::queryInterface(_rType
);
242 void SAL_CALL
OSeekableOutputStreamWrapper::acquire( ) throw ()
244 OOutputStreamWrapper::acquire();
247 void SAL_CALL
OSeekableOutputStreamWrapper::release( ) throw ()
249 OOutputStreamWrapper::release();
252 void SAL_CALL
OSeekableOutputStreamWrapper::seek( sal_Int64 _nLocation
) throw (IllegalArgumentException
, IOException
, RuntimeException
, std::exception
)
254 rStream
.Seek((sal_uInt32
)_nLocation
);
258 sal_Int64 SAL_CALL
OSeekableOutputStreamWrapper::getPosition( ) throw (IOException
, RuntimeException
, std::exception
)
260 sal_uInt32 nPos
= rStream
.Tell();
262 return (sal_Int64
)nPos
;
265 sal_Int64 SAL_CALL
OSeekableOutputStreamWrapper::getLength( ) throw (IOException
, RuntimeException
, std::exception
)
267 sal_uInt32 nCurrentPos
= rStream
.Tell();
270 rStream
.Seek(STREAM_SEEK_TO_END
);
271 sal_uInt32 nEndPos
= rStream
.Tell();
272 rStream
.Seek(nCurrentPos
);
276 return (sal_Int64
)nEndPos
;
279 OStreamWrapper::OStreamWrapper(SvStream
& _rStream
)
281 SetStream( &_rStream
, false );
284 ::com::sun::star::uno::Reference
< ::com::sun::star::io::XInputStream
> SAL_CALL
OStreamWrapper::getInputStream( ) throw (::com::sun::star::uno::RuntimeException
, std::exception
)
289 ::com::sun::star::uno::Reference
< ::com::sun::star::io::XOutputStream
> SAL_CALL
OStreamWrapper::getOutputStream( ) throw (::com::sun::star::uno::RuntimeException
, std::exception
)
294 void SAL_CALL
OStreamWrapper::writeBytes(const staruno::Sequence
< sal_Int8
>& aData
) throw(stario::NotConnectedException
, stario::BufferSizeExceededException
, staruno::RuntimeException
, std::exception
)
296 sal_uInt32 nWritten
= m_pSvStream
->Write(aData
.getConstArray(),aData
.getLength());
297 ErrCode err
= m_pSvStream
->GetError();
298 if ( (ERRCODE_NONE
!= err
)
299 || (nWritten
!= (sal_uInt32
)aData
.getLength())
302 throw stario::BufferSizeExceededException(OUString(),static_cast<staruno::XWeak
*>(this));
306 void SAL_CALL
OStreamWrapper::flush() throw(stario::NotConnectedException
, stario::BufferSizeExceededException
, staruno::RuntimeException
, std::exception
)
308 m_pSvStream
->Flush();
309 if (m_pSvStream
->GetError() != ERRCODE_NONE
)
310 throw stario::NotConnectedException(OUString(),static_cast<staruno::XWeak
*>(this));
313 void SAL_CALL
OStreamWrapper::closeOutput() throw(stario::NotConnectedException
, stario::BufferSizeExceededException
, staruno::RuntimeException
, std::exception
)
317 void SAL_CALL
OStreamWrapper::truncate() throw(::com::sun::star::io::IOException
, ::com::sun::star::uno::RuntimeException
, std::exception
)
319 m_pSvStream
->SetStreamSize(0);
324 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */