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 <sal/config.h>
22 #include <com/sun/star/io/BufferSizeExceededException.hpp>
23 #include <com/sun/star/io/NotConnectedException.hpp>
24 #include <o3tl/safeint.hxx>
25 #include <unotools/streamwrap.hxx>
26 #include <tools/stream.hxx>
31 using namespace ::com::sun::star::uno
;
32 using namespace ::com::sun::star::io
;
33 using namespace ::com::sun::star::lang
;
35 OInputStreamWrapper::OInputStreamWrapper( SvStream
& _rStream
)
36 :m_pSvStream(&_rStream
)
37 ,m_bSvStreamOwner(false)
41 OInputStreamWrapper::OInputStreamWrapper( SvStream
* pStream
, bool bOwner
)
42 :m_pSvStream( pStream
)
43 ,m_bSvStreamOwner( bOwner
)
47 OInputStreamWrapper::OInputStreamWrapper( std::unique_ptr
<SvStream
> pStream
)
48 :m_pSvStream( pStream
.release() )
49 ,m_bSvStreamOwner( true )
53 OInputStreamWrapper::~OInputStreamWrapper()
55 if( m_bSvStreamOwner
)
59 sal_Int32 SAL_CALL
OInputStreamWrapper::readBytes(css::uno::Sequence
< sal_Int8
>& aData
, sal_Int32 nBytesToRead
)
64 throw css::io::BufferSizeExceededException(OUString(), getXWeak());
66 std::scoped_lock
aGuard( m_aMutex
);
68 if (aData
.getLength() < nBytesToRead
)
69 aData
.realloc(nBytesToRead
);
71 sal_uInt32 nRead
= m_pSvStream
->ReadBytes(static_cast<void*>(aData
.getArray()), nBytesToRead
);
74 // If read characters < MaxLength, adjust css::uno::Sequence
75 if (nRead
< o3tl::make_unsigned(aData
.getLength()))
76 aData
.realloc( nRead
);
81 sal_Int32
OInputStreamWrapper::readSomeBytes(sal_Int8
* pData
, sal_Int32 nBytesToRead
)
86 throw css::io::BufferSizeExceededException(OUString(), getXWeak());
88 std::scoped_lock
aGuard( m_aMutex
);
90 sal_uInt32 nRead
= m_pSvStream
->ReadBytes(static_cast<void*>(pData
), nBytesToRead
);
96 sal_Int32 SAL_CALL
OInputStreamWrapper::readSomeBytes(css::uno::Sequence
< sal_Int8
>& aData
, sal_Int32 nMaxBytesToRead
)
100 if (nMaxBytesToRead
< 0)
101 throw css::io::BufferSizeExceededException(OUString(), getXWeak());
103 if (m_pSvStream
->eof())
109 return readBytes(aData
, nMaxBytesToRead
);
112 void SAL_CALL
OInputStreamWrapper::skipBytes(sal_Int32 nBytesToSkip
)
114 std::scoped_lock
aGuard( m_aMutex
);
117 m_pSvStream
->SeekRel(nBytesToSkip
);
121 sal_Int32 SAL_CALL
OInputStreamWrapper::available()
123 std::scoped_lock
aGuard( m_aMutex
);
126 sal_Int64 nAvailable
= m_pSvStream
->remainingSize();
129 return std::min
<sal_Int64
>(SAL_MAX_INT32
, nAvailable
);
132 void SAL_CALL
OInputStreamWrapper::closeInput()
134 std::scoped_lock
aGuard( m_aMutex
);
137 if (m_bSvStreamOwner
)
140 m_pSvStream
= nullptr;
144 void OInputStreamWrapper::checkConnected() const
147 throw css::io::NotConnectedException(OUString(), const_cast<OInputStreamWrapper
*>(this)->getXWeak());
150 void OInputStreamWrapper::checkError() const
154 auto const e
= m_pSvStream
->SvStream::GetError();
155 if (e
!= ERRCODE_NONE
)
156 // TODO: really evaluate the error
157 throw css::io::NotConnectedException("utl::OInputStreamWrapper error " + e
.toString(), const_cast<OInputStreamWrapper
*>(this)->getXWeak());
160 //= OSeekableInputStreamWrapper
162 OSeekableInputStreamWrapper::~OSeekableInputStreamWrapper() = default;
164 OSeekableInputStreamWrapper::OSeekableInputStreamWrapper(SvStream
& _rStream
)
166 SetStream( &_rStream
, false );
169 OSeekableInputStreamWrapper::OSeekableInputStreamWrapper(SvStream
* _pStream
, bool _bOwner
)
171 SetStream( _pStream
, _bOwner
);
174 void SAL_CALL
OSeekableInputStreamWrapper::seek( sal_Int64 _nLocation
)
176 std::scoped_lock
aGuard( m_aMutex
);
179 m_pSvStream
->Seek(static_cast<sal_uInt64
>(_nLocation
));
183 sal_Int64 SAL_CALL
OSeekableInputStreamWrapper::getPosition( )
185 std::scoped_lock
aGuard( m_aMutex
);
188 sal_uInt32 nPos
= m_pSvStream
->Tell();
190 return static_cast<sal_Int64
>(nPos
);
193 sal_Int64 SAL_CALL
OSeekableInputStreamWrapper::getLength( )
195 std::scoped_lock
aGuard( m_aMutex
);
200 sal_Int64 nEndPos
= m_pSvStream
->TellEnd();
205 //= OOutputStreamWrapper
207 OOutputStreamWrapper::OOutputStreamWrapper(SvStream
& _rStream
):
211 OOutputStreamWrapper::~OOutputStreamWrapper() {}
213 void SAL_CALL
OOutputStreamWrapper::writeBytes(const css::uno::Sequence
< sal_Int8
>& aData
)
215 sal_uInt32 nWritten
= rStream
.WriteBytes(aData
.getConstArray(), aData
.getLength());
216 ErrCode err
= rStream
.GetError();
217 if ( (ERRCODE_NONE
!= err
)
218 || (nWritten
!= static_cast<sal_uInt32
>(aData
.getLength()))
221 throw css::io::BufferSizeExceededException(OUString(), getXWeak());
225 void SAL_CALL
OOutputStreamWrapper::flush()
227 rStream
.FlushBuffer();
231 void SAL_CALL
OOutputStreamWrapper::closeOutput()
235 void OOutputStreamWrapper::checkError() const
237 if (rStream
.GetError() != ERRCODE_NONE
)
238 // TODO: really evaluate the error
239 throw css::io::NotConnectedException(OUString(), const_cast<OOutputStreamWrapper
*>(this)->getXWeak());
242 //= OSeekableOutputStreamWrapper
244 OSeekableOutputStreamWrapper::OSeekableOutputStreamWrapper(SvStream
& _rStream
)
245 :OOutputStreamWrapper(_rStream
)
249 OSeekableOutputStreamWrapper::~OSeekableOutputStreamWrapper() {}
251 Any SAL_CALL
OSeekableOutputStreamWrapper::queryInterface( const Type
& _rType
)
253 Any aReturn
= OOutputStreamWrapper::queryInterface(_rType
);
254 if (!aReturn
.hasValue())
255 aReturn
= OSeekableOutputStreamWrapper_Base::queryInterface(_rType
);
259 void SAL_CALL
OSeekableOutputStreamWrapper::seek( sal_Int64 _nLocation
)
261 rStream
.Seek(static_cast<sal_uInt32
>(_nLocation
));
265 sal_Int64 SAL_CALL
OSeekableOutputStreamWrapper::getPosition( )
267 sal_uInt32 nPos
= rStream
.Tell();
269 return static_cast<sal_Int64
>(nPos
);
272 sal_Int64 SAL_CALL
OSeekableOutputStreamWrapper::getLength( )
276 sal_Int64 nEndPos
= rStream
.TellEnd();
281 OStreamWrapper::~OStreamWrapper() = default;
283 OStreamWrapper::OStreamWrapper(SvStream
& _rStream
)
285 SetStream( &_rStream
, false );
288 OStreamWrapper::OStreamWrapper(std::unique_ptr
<SvStream
> pStream
)
290 SetStream( pStream
.release(), true );
293 OStreamWrapper::OStreamWrapper(SvStream
* pStream
, bool bOwner
)
295 SetStream( pStream
, bOwner
);
298 css::uno::Reference
< css::io::XInputStream
> SAL_CALL
OStreamWrapper::getInputStream( )
303 css::uno::Reference
< css::io::XOutputStream
> SAL_CALL
OStreamWrapper::getOutputStream( )
308 void SAL_CALL
OStreamWrapper::writeBytes(const css::uno::Sequence
< sal_Int8
>& aData
)
310 sal_uInt32 nWritten
= m_pSvStream
->WriteBytes(aData
.getConstArray(), aData
.getLength());
311 ErrCode err
= m_pSvStream
->GetError();
312 if ( (ERRCODE_NONE
!= err
)
313 || (nWritten
!= static_cast<sal_uInt32
>(aData
.getLength()))
316 throw css::io::BufferSizeExceededException(OUString(), getXWeak());
320 void SAL_CALL
OStreamWrapper::flush()
322 m_pSvStream
->FlushBuffer();
323 if (m_pSvStream
->GetError() != ERRCODE_NONE
)
324 throw css::io::NotConnectedException(OUString(), getXWeak());
327 void SAL_CALL
OStreamWrapper::closeOutput()
331 void SAL_CALL
OStreamWrapper::truncate()
333 m_pSvStream
->SetStreamSize(0);
338 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */