Bump version to 4.3-4
[LibreOffice.git] / unotools / source / streaming / streamwrap.cxx
blob81d2421daf487cca9e4bf3df1953292f65405543
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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>
24 namespace utl
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 )
46 delete m_pSvStream;
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 )
52 checkConnected();
54 if (nBytesToRead < 0)
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);
62 checkError();
64 // Wenn gelesene Zeichen < MaxLength, staruno::Sequence anpassen
65 if (nRead < (sal_uInt32)nBytesToRead)
66 aData.realloc( nRead );
68 return 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 )
73 checkError();
75 if (nMaxBytesToRead < 0)
76 throw stario::BufferSizeExceededException(OUString(),static_cast<staruno::XWeak*>(this));
78 if (m_pSvStream->IsEof())
80 aData.realloc(0);
81 return 0;
83 else
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 );
90 checkError();
92 m_pSvStream->SeekRel(nBytesToSkip);
93 checkError();
96 sal_Int32 SAL_CALL OInputStreamWrapper::available() throw( stario::NotConnectedException, staruno::RuntimeException, std::exception )
98 ::osl::MutexGuard aGuard( m_aMutex );
99 checkConnected();
101 sal_uInt32 nPos = m_pSvStream->Tell();
102 checkError();
104 m_pSvStream->Seek(STREAM_SEEK_TO_END);
105 checkError();
107 sal_Int32 nAvailable = (sal_Int32)m_pSvStream->Tell() - nPos;
108 m_pSvStream->Seek(nPos);
109 checkError();
111 return nAvailable;
114 void SAL_CALL OInputStreamWrapper::closeInput() throw( stario::NotConnectedException, staruno::RuntimeException, std::exception )
116 ::osl::MutexGuard aGuard( m_aMutex );
117 checkConnected();
119 if (m_bSvStreamOwner)
120 delete m_pSvStream;
122 m_pSvStream = NULL;
125 void OInputStreamWrapper::checkConnected() const
127 if (!m_pSvStream)
128 throw stario::NotConnectedException(OUString(), const_cast<staruno::XWeak*>(static_cast<const staruno::XWeak*>(this)));
131 void OInputStreamWrapper::checkError() const
133 checkConnected();
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 );
155 checkConnected();
157 m_pSvStream->Seek((sal_uInt32)_nLocation);
158 checkError();
161 sal_Int64 SAL_CALL OSeekableInputStreamWrapper::getPosition( ) throw (IOException, RuntimeException, std::exception)
163 ::osl::MutexGuard aGuard( m_aMutex );
164 checkConnected();
166 sal_uInt32 nPos = m_pSvStream->Tell();
167 checkError();
168 return (sal_Int64)nPos;
171 sal_Int64 SAL_CALL OSeekableInputStreamWrapper::getLength( ) throw (IOException, RuntimeException, std::exception)
173 ::osl::MutexGuard aGuard( m_aMutex );
174 checkConnected();
176 sal_uInt32 nCurrentPos = m_pSvStream->Tell();
177 checkError();
179 m_pSvStream->Seek(STREAM_SEEK_TO_END);
180 sal_uInt32 nEndPos = m_pSvStream->Tell();
181 m_pSvStream->Seek(nCurrentPos);
183 checkError();
185 return (sal_Int64)nEndPos;
188 //= OOutputStreamWrapper
190 OOutputStreamWrapper::OOutputStreamWrapper(SvStream& _rStream):
191 rStream(_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 )
210 rStream.Flush();
211 checkError();
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);
239 return aReturn;
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);
255 checkError();
258 sal_Int64 SAL_CALL OSeekableOutputStreamWrapper::getPosition( ) throw (IOException, RuntimeException, std::exception)
260 sal_uInt32 nPos = rStream.Tell();
261 checkError();
262 return (sal_Int64)nPos;
265 sal_Int64 SAL_CALL OSeekableOutputStreamWrapper::getLength( ) throw (IOException, RuntimeException, std::exception)
267 sal_uInt32 nCurrentPos = rStream.Tell();
268 checkError();
270 rStream.Seek(STREAM_SEEK_TO_END);
271 sal_uInt32 nEndPos = rStream.Tell();
272 rStream.Seek(nCurrentPos);
274 checkError();
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)
286 return this;
289 ::com::sun::star::uno::Reference< ::com::sun::star::io::XOutputStream > SAL_CALL OStreamWrapper::getOutputStream( ) throw (::com::sun::star::uno::RuntimeException, std::exception)
291 return this;
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);
322 } // namespace utl
324 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */