bump product version to 4.1.6.2
[LibreOffice.git] / unotools / source / streaming / streamwrap.cxx
blob4798af6d825fd8fd8f1a2c40ed2c3fb8c3f01544
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 //==================================================================
32 //= OInputStreamWrapper
33 //==================================================================
34 DBG_NAME(OInputStreamWrapper)
35 //------------------------------------------------------------------
36 OInputStreamWrapper::OInputStreamWrapper( SvStream& _rStream )
37 :m_pSvStream(&_rStream)
38 ,m_bSvStreamOwner(sal_False)
40 DBG_CTOR(OInputStreamWrapper,NULL);
44 //------------------------------------------------------------------
45 OInputStreamWrapper::OInputStreamWrapper( SvStream* pStream, sal_Bool bOwner )
46 :m_pSvStream( pStream )
47 ,m_bSvStreamOwner( bOwner )
49 DBG_CTOR(OInputStreamWrapper,NULL);
53 //------------------------------------------------------------------
54 OInputStreamWrapper::~OInputStreamWrapper()
56 if( m_bSvStreamOwner )
57 delete m_pSvStream;
59 DBG_DTOR(OInputStreamWrapper,NULL);
62 //------------------------------------------------------------------------------
63 sal_Int32 SAL_CALL OInputStreamWrapper::readBytes(staruno::Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead)
64 throw( stario::NotConnectedException, stario::BufferSizeExceededException, staruno::RuntimeException )
66 checkConnected();
68 if (nBytesToRead < 0)
69 throw stario::BufferSizeExceededException(OUString(),static_cast<staruno::XWeak*>(this));
71 ::osl::MutexGuard aGuard( m_aMutex );
73 aData.realloc(nBytesToRead);
75 sal_uInt32 nRead = m_pSvStream->Read((void*)aData.getArray(), nBytesToRead);
76 checkError();
78 // Wenn gelesene Zeichen < MaxLength, staruno::Sequence anpassen
79 if (nRead < (sal_uInt32)nBytesToRead)
80 aData.realloc( nRead );
82 return nRead;
85 //------------------------------------------------------------------------------
86 sal_Int32 SAL_CALL OInputStreamWrapper::readSomeBytes(staruno::Sequence< sal_Int8 >& aData, sal_Int32 nMaxBytesToRead) throw( stario::NotConnectedException, stario::BufferSizeExceededException, staruno::RuntimeException )
88 checkError();
90 if (nMaxBytesToRead < 0)
91 throw stario::BufferSizeExceededException(OUString(),static_cast<staruno::XWeak*>(this));
93 if (m_pSvStream->IsEof())
95 aData.realloc(0);
96 return 0;
98 else
99 return readBytes(aData, nMaxBytesToRead);
102 //------------------------------------------------------------------------------
103 void SAL_CALL OInputStreamWrapper::skipBytes(sal_Int32 nBytesToSkip) throw( stario::NotConnectedException, stario::BufferSizeExceededException, staruno::RuntimeException )
105 ::osl::MutexGuard aGuard( m_aMutex );
106 checkError();
108 m_pSvStream->SeekRel(nBytesToSkip);
109 checkError();
112 //------------------------------------------------------------------------------
113 sal_Int32 SAL_CALL OInputStreamWrapper::available() throw( stario::NotConnectedException, staruno::RuntimeException )
115 ::osl::MutexGuard aGuard( m_aMutex );
116 checkConnected();
118 sal_uInt32 nPos = m_pSvStream->Tell();
119 checkError();
121 m_pSvStream->Seek(STREAM_SEEK_TO_END);
122 checkError();
124 sal_Int32 nAvailable = (sal_Int32)m_pSvStream->Tell() - nPos;
125 m_pSvStream->Seek(nPos);
126 checkError();
128 return nAvailable;
131 //------------------------------------------------------------------------------
132 void SAL_CALL OInputStreamWrapper::closeInput() throw( stario::NotConnectedException, staruno::RuntimeException )
134 ::osl::MutexGuard aGuard( m_aMutex );
135 checkConnected();
137 if (m_bSvStreamOwner)
138 delete m_pSvStream;
140 m_pSvStream = NULL;
143 //------------------------------------------------------------------------------
144 void OInputStreamWrapper::checkConnected() const
146 if (!m_pSvStream)
147 throw stario::NotConnectedException(OUString(), const_cast<staruno::XWeak*>(static_cast<const staruno::XWeak*>(this)));
150 //------------------------------------------------------------------------------
151 void OInputStreamWrapper::checkError() const
153 checkConnected();
155 if (m_pSvStream->SvStream::GetError() != ERRCODE_NONE)
156 // TODO: really evaluate the error
157 throw stario::NotConnectedException(OUString(), const_cast<staruno::XWeak*>(static_cast<const staruno::XWeak*>(this)));
160 //==================================================================
161 //= OSeekableInputStreamWrapper
162 //==================================================================
163 //------------------------------------------------------------------------------
164 OSeekableInputStreamWrapper::OSeekableInputStreamWrapper(SvStream& _rStream)
166 SetStream( &_rStream, sal_False );
169 //------------------------------------------------------------------------------
170 OSeekableInputStreamWrapper::OSeekableInputStreamWrapper(SvStream* _pStream, sal_Bool _bOwner)
172 SetStream( _pStream, _bOwner );
175 //------------------------------------------------------------------------------
176 void SAL_CALL OSeekableInputStreamWrapper::seek( sal_Int64 _nLocation ) throw (IllegalArgumentException, IOException, RuntimeException)
178 ::osl::MutexGuard aGuard( m_aMutex );
179 checkConnected();
181 m_pSvStream->Seek((sal_uInt32)_nLocation);
182 checkError();
185 //------------------------------------------------------------------------------
186 sal_Int64 SAL_CALL OSeekableInputStreamWrapper::getPosition( ) throw (IOException, RuntimeException)
188 ::osl::MutexGuard aGuard( m_aMutex );
189 checkConnected();
191 sal_uInt32 nPos = m_pSvStream->Tell();
192 checkError();
193 return (sal_Int64)nPos;
196 //------------------------------------------------------------------------------
197 sal_Int64 SAL_CALL OSeekableInputStreamWrapper::getLength( ) throw (IOException, RuntimeException)
199 ::osl::MutexGuard aGuard( m_aMutex );
200 checkConnected();
202 sal_uInt32 nCurrentPos = m_pSvStream->Tell();
203 checkError();
205 m_pSvStream->Seek(STREAM_SEEK_TO_END);
206 sal_uInt32 nEndPos = m_pSvStream->Tell();
207 m_pSvStream->Seek(nCurrentPos);
209 checkError();
211 return (sal_Int64)nEndPos;
214 //==================================================================
215 //= OOutputStreamWrapper
216 //==================================================================
218 OOutputStreamWrapper::OOutputStreamWrapper(SvStream& _rStream):
219 rStream(_rStream)
222 OOutputStreamWrapper::~OOutputStreamWrapper() {}
224 void SAL_CALL OOutputStreamWrapper::writeBytes(const staruno::Sequence< sal_Int8 >& aData) throw( stario::NotConnectedException, stario::BufferSizeExceededException, staruno::RuntimeException )
226 sal_uInt32 nWritten = rStream.Write(aData.getConstArray(),aData.getLength());
227 ErrCode err = rStream.GetError();
228 if ( (ERRCODE_NONE != err)
229 || (nWritten != (sal_uInt32)aData.getLength())
232 throw stario::BufferSizeExceededException(OUString(),static_cast<staruno::XWeak*>(this));
236 //------------------------------------------------------------------
237 void SAL_CALL OOutputStreamWrapper::flush() throw( stario::NotConnectedException, stario::BufferSizeExceededException, staruno::RuntimeException )
239 rStream.Flush();
240 checkError();
243 //------------------------------------------------------------------
244 void SAL_CALL OOutputStreamWrapper::closeOutput() throw( stario::NotConnectedException, stario::BufferSizeExceededException, staruno::RuntimeException )
248 //------------------------------------------------------------------------------
249 void OOutputStreamWrapper::checkError() const
251 if (rStream.GetError() != ERRCODE_NONE)
252 // TODO: really evaluate the error
253 throw stario::NotConnectedException(OUString(), const_cast<staruno::XWeak*>(static_cast<const staruno::XWeak*>(this)));
256 //==================================================================
257 //= OSeekableOutputStreamWrapper
258 //==================================================================
259 //------------------------------------------------------------------------------
260 OSeekableOutputStreamWrapper::OSeekableOutputStreamWrapper(SvStream& _rStream)
261 :OOutputStreamWrapper(_rStream)
265 OSeekableOutputStreamWrapper::~OSeekableOutputStreamWrapper() {}
267 //------------------------------------------------------------------------------
268 Any SAL_CALL OSeekableOutputStreamWrapper::queryInterface( const Type& _rType ) throw (RuntimeException)
270 Any aReturn = OOutputStreamWrapper::queryInterface(_rType);
271 if (!aReturn.hasValue())
272 aReturn = OSeekableOutputStreamWrapper_Base::queryInterface(_rType);
273 return aReturn;
276 //------------------------------------------------------------------------------
277 void SAL_CALL OSeekableOutputStreamWrapper::acquire( ) throw ()
279 OOutputStreamWrapper::acquire();
282 //------------------------------------------------------------------------------
283 void SAL_CALL OSeekableOutputStreamWrapper::release( ) throw ()
285 OOutputStreamWrapper::release();
288 //------------------------------------------------------------------------------
289 void SAL_CALL OSeekableOutputStreamWrapper::seek( sal_Int64 _nLocation ) throw (IllegalArgumentException, IOException, RuntimeException)
291 rStream.Seek((sal_uInt32)_nLocation);
292 checkError();
295 //------------------------------------------------------------------------------
296 sal_Int64 SAL_CALL OSeekableOutputStreamWrapper::getPosition( ) throw (IOException, RuntimeException)
298 sal_uInt32 nPos = rStream.Tell();
299 checkError();
300 return (sal_Int64)nPos;
303 //------------------------------------------------------------------------------
304 sal_Int64 SAL_CALL OSeekableOutputStreamWrapper::getLength( ) throw (IOException, RuntimeException)
306 sal_uInt32 nCurrentPos = rStream.Tell();
307 checkError();
309 rStream.Seek(STREAM_SEEK_TO_END);
310 sal_uInt32 nEndPos = rStream.Tell();
311 rStream.Seek(nCurrentPos);
313 checkError();
315 return (sal_Int64)nEndPos;
318 //------------------------------------------------------------------------------
319 OStreamWrapper::OStreamWrapper(SvStream& _rStream)
321 SetStream( &_rStream, sal_False );
324 //------------------------------------------------------------------------------
325 ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream > SAL_CALL OStreamWrapper::getInputStream( ) throw (::com::sun::star::uno::RuntimeException)
327 return this;
330 //------------------------------------------------------------------------------
331 ::com::sun::star::uno::Reference< ::com::sun::star::io::XOutputStream > SAL_CALL OStreamWrapper::getOutputStream( ) throw (::com::sun::star::uno::RuntimeException)
333 return this;
336 //------------------------------------------------------------------------------
337 void SAL_CALL OStreamWrapper::writeBytes(const staruno::Sequence< sal_Int8 >& aData) throw(stario::NotConnectedException, stario::BufferSizeExceededException, staruno::RuntimeException)
339 sal_uInt32 nWritten = m_pSvStream->Write(aData.getConstArray(),aData.getLength());
340 ErrCode err = m_pSvStream->GetError();
341 if ( (ERRCODE_NONE != err)
342 || (nWritten != (sal_uInt32)aData.getLength())
345 throw stario::BufferSizeExceededException(OUString(),static_cast<staruno::XWeak*>(this));
349 //------------------------------------------------------------------------------
350 void SAL_CALL OStreamWrapper::flush() throw(stario::NotConnectedException, stario::BufferSizeExceededException, staruno::RuntimeException)
352 m_pSvStream->Flush();
353 if (m_pSvStream->GetError() != ERRCODE_NONE)
354 throw stario::NotConnectedException(OUString(),static_cast<staruno::XWeak*>(this));
357 //------------------------------------------------------------------------------
358 void SAL_CALL OStreamWrapper::closeOutput() throw(stario::NotConnectedException, stario::BufferSizeExceededException, staruno::RuntimeException)
362 //------------------------------------------------------------------------------
363 void SAL_CALL OStreamWrapper::truncate() throw(::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException)
365 m_pSvStream->SetStreamSize(0);
368 } // namespace utl
370 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */