lok: vcl: fix multiple floatwin removal case more robustly.
[LibreOffice.git] / unotools / source / streaming / streamwrap.cxx
blob97d05180b020dcaec57b1cbbe915b863b16d88b4
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 <sal/config.h>
22 #include <com/sun/star/io/BufferSizeExceededException.hpp>
23 #include <com/sun/star/io/NotConnectedException.hpp>
24 #include <unotools/streamwrap.hxx>
25 #include <tools/stream.hxx>
27 namespace utl
30 using namespace ::com::sun::star::uno;
31 using namespace ::com::sun::star::io;
32 using namespace ::com::sun::star::lang;
34 OInputStreamWrapper::OInputStreamWrapper( SvStream& _rStream )
35 :m_pSvStream(&_rStream)
36 ,m_bSvStreamOwner(false)
40 OInputStreamWrapper::OInputStreamWrapper( SvStream* pStream, bool bOwner )
41 :m_pSvStream( pStream )
42 ,m_bSvStreamOwner( bOwner )
46 OInputStreamWrapper::OInputStreamWrapper( std::unique_ptr<SvStream> pStream )
47 :m_pSvStream( pStream.release() )
48 ,m_bSvStreamOwner( true )
52 OInputStreamWrapper::~OInputStreamWrapper()
54 if( m_bSvStreamOwner )
55 delete m_pSvStream;
58 sal_Int32 SAL_CALL OInputStreamWrapper::readBytes(css::uno::Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead)
60 checkConnected();
62 if (nBytesToRead < 0)
63 throw css::io::BufferSizeExceededException(OUString(),static_cast<css::uno::XWeak*>(this));
65 ::osl::MutexGuard aGuard( m_aMutex );
67 if (aData.getLength() < nBytesToRead)
68 aData.realloc(nBytesToRead);
70 sal_uInt32 nRead = m_pSvStream->ReadBytes(static_cast<void*>(aData.getArray()), nBytesToRead);
71 checkError();
73 // If read characters < MaxLength, adjust css::uno::Sequence
74 if (nRead < static_cast<std::size_t>(aData.getLength()))
75 aData.realloc( nRead );
77 return nRead;
80 sal_Int32 SAL_CALL OInputStreamWrapper::readSomeBytes(css::uno::Sequence< sal_Int8 >& aData, sal_Int32 nMaxBytesToRead)
82 checkError();
84 if (nMaxBytesToRead < 0)
85 throw css::io::BufferSizeExceededException(OUString(),static_cast<css::uno::XWeak*>(this));
87 if (m_pSvStream->eof())
89 aData.realloc(0);
90 return 0;
92 else
93 return readBytes(aData, nMaxBytesToRead);
96 void SAL_CALL OInputStreamWrapper::skipBytes(sal_Int32 nBytesToSkip)
98 ::osl::MutexGuard aGuard( m_aMutex );
99 checkError();
101 m_pSvStream->SeekRel(nBytesToSkip);
102 checkError();
105 sal_Int32 SAL_CALL OInputStreamWrapper::available()
107 ::osl::MutexGuard aGuard( m_aMutex );
108 checkConnected();
110 sal_Int64 nAvailable = m_pSvStream->remainingSize();
111 checkError();
113 return std::min<sal_Int64>(SAL_MAX_INT32, nAvailable);
116 void SAL_CALL OInputStreamWrapper::closeInput()
118 ::osl::MutexGuard aGuard( m_aMutex );
119 checkConnected();
121 if (m_bSvStreamOwner)
122 delete m_pSvStream;
124 m_pSvStream = nullptr;
127 void OInputStreamWrapper::checkConnected() const
129 if (!m_pSvStream)
130 throw css::io::NotConnectedException(OUString(), const_cast<css::uno::XWeak*>(static_cast<const css::uno::XWeak*>(this)));
133 void OInputStreamWrapper::checkError() const
135 checkConnected();
137 if (m_pSvStream->SvStream::GetError() != ERRCODE_NONE)
138 // TODO: really evaluate the error
139 throw css::io::NotConnectedException(OUString(), const_cast<css::uno::XWeak*>(static_cast<const css::uno::XWeak*>(this)));
142 //= OSeekableInputStreamWrapper
144 OSeekableInputStreamWrapper::~OSeekableInputStreamWrapper() = default;
146 OSeekableInputStreamWrapper::OSeekableInputStreamWrapper(SvStream& _rStream)
148 SetStream( &_rStream, false );
151 OSeekableInputStreamWrapper::OSeekableInputStreamWrapper(SvStream* _pStream, bool _bOwner)
153 SetStream( _pStream, _bOwner );
156 void SAL_CALL OSeekableInputStreamWrapper::seek( sal_Int64 _nLocation )
158 ::osl::MutexGuard aGuard( m_aMutex );
159 checkConnected();
161 m_pSvStream->Seek(static_cast<sal_uInt32>(_nLocation));
162 checkError();
165 sal_Int64 SAL_CALL OSeekableInputStreamWrapper::getPosition( )
167 ::osl::MutexGuard aGuard( m_aMutex );
168 checkConnected();
170 sal_uInt32 nPos = m_pSvStream->Tell();
171 checkError();
172 return static_cast<sal_Int64>(nPos);
175 sal_Int64 SAL_CALL OSeekableInputStreamWrapper::getLength( )
177 ::osl::MutexGuard aGuard( m_aMutex );
178 checkConnected();
180 checkError();
182 sal_Int64 nEndPos = m_pSvStream->TellEnd();
184 return nEndPos;
187 //= OOutputStreamWrapper
189 OOutputStreamWrapper::OOutputStreamWrapper(SvStream& _rStream):
190 rStream(_rStream)
193 OOutputStreamWrapper::~OOutputStreamWrapper() {}
195 void SAL_CALL OOutputStreamWrapper::writeBytes(const css::uno::Sequence< sal_Int8 >& aData)
197 sal_uInt32 nWritten = rStream.WriteBytes(aData.getConstArray(), aData.getLength());
198 ErrCode err = rStream.GetError();
199 if ( (ERRCODE_NONE != err)
200 || (nWritten != static_cast<sal_uInt32>(aData.getLength()))
203 throw css::io::BufferSizeExceededException(OUString(),static_cast<css::uno::XWeak*>(this));
207 void SAL_CALL OOutputStreamWrapper::flush()
209 rStream.Flush();
210 checkError();
213 void SAL_CALL OOutputStreamWrapper::closeOutput()
217 void OOutputStreamWrapper::checkError() const
219 if (rStream.GetError() != ERRCODE_NONE)
220 // TODO: really evaluate the error
221 throw css::io::NotConnectedException(OUString(), const_cast<css::uno::XWeak*>(static_cast<const css::uno::XWeak*>(this)));
224 //= OSeekableOutputStreamWrapper
226 OSeekableOutputStreamWrapper::OSeekableOutputStreamWrapper(SvStream& _rStream)
227 :OOutputStreamWrapper(_rStream)
231 OSeekableOutputStreamWrapper::~OSeekableOutputStreamWrapper() {}
233 Any SAL_CALL OSeekableOutputStreamWrapper::queryInterface( const Type& _rType )
235 Any aReturn = OOutputStreamWrapper::queryInterface(_rType);
236 if (!aReturn.hasValue())
237 aReturn = OSeekableOutputStreamWrapper_Base::queryInterface(_rType);
238 return aReturn;
241 void SAL_CALL OSeekableOutputStreamWrapper::acquire( ) throw ()
243 OOutputStreamWrapper::acquire();
246 void SAL_CALL OSeekableOutputStreamWrapper::release( ) throw ()
248 OOutputStreamWrapper::release();
251 void SAL_CALL OSeekableOutputStreamWrapper::seek( sal_Int64 _nLocation )
253 rStream.Seek(static_cast<sal_uInt32>(_nLocation));
254 checkError();
257 sal_Int64 SAL_CALL OSeekableOutputStreamWrapper::getPosition( )
259 sal_uInt32 nPos = rStream.Tell();
260 checkError();
261 return static_cast<sal_Int64>(nPos);
264 sal_Int64 SAL_CALL OSeekableOutputStreamWrapper::getLength( )
266 checkError();
268 sal_Int64 nEndPos = rStream.TellEnd();
270 return nEndPos;
273 OStreamWrapper::~OStreamWrapper() = default;
275 OStreamWrapper::OStreamWrapper(SvStream& _rStream)
277 SetStream( &_rStream, false );
280 OStreamWrapper::OStreamWrapper(std::unique_ptr<SvStream> pStream)
282 SetStream( pStream.release(), true );
285 css::uno::Reference< css::io::XInputStream > SAL_CALL OStreamWrapper::getInputStream( )
287 return this;
290 css::uno::Reference< css::io::XOutputStream > SAL_CALL OStreamWrapper::getOutputStream( )
292 return this;
295 void SAL_CALL OStreamWrapper::writeBytes(const css::uno::Sequence< sal_Int8 >& aData)
297 sal_uInt32 nWritten = m_pSvStream->WriteBytes(aData.getConstArray(), aData.getLength());
298 ErrCode err = m_pSvStream->GetError();
299 if ( (ERRCODE_NONE != err)
300 || (nWritten != static_cast<sal_uInt32>(aData.getLength()))
303 throw css::io::BufferSizeExceededException(OUString(),static_cast<css::uno::XWeak*>(this));
307 void SAL_CALL OStreamWrapper::flush()
309 m_pSvStream->Flush();
310 if (m_pSvStream->GetError() != ERRCODE_NONE)
311 throw css::io::NotConnectedException(OUString(),static_cast<css::uno::XWeak*>(this));
314 void SAL_CALL OStreamWrapper::closeOutput()
318 void SAL_CALL OStreamWrapper::truncate()
320 m_pSvStream->SetStreamSize(0);
323 } // namespace utl
325 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */