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 <unotools/streamwrap.hxx>
25 #include <tools/stream.hxx>
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
)
58 sal_Int32 SAL_CALL
OInputStreamWrapper::readBytes(css::uno::Sequence
< sal_Int8
>& aData
, sal_Int32 nBytesToRead
)
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
);
73 // If read characters < MaxLength, adjust css::uno::Sequence
74 if (nRead
< static_cast<std::size_t>(aData
.getLength()))
75 aData
.realloc( nRead
);
80 sal_Int32 SAL_CALL
OInputStreamWrapper::readSomeBytes(css::uno::Sequence
< sal_Int8
>& aData
, sal_Int32 nMaxBytesToRead
)
84 if (nMaxBytesToRead
< 0)
85 throw css::io::BufferSizeExceededException(OUString(),static_cast<css::uno::XWeak
*>(this));
87 if (m_pSvStream
->eof())
93 return readBytes(aData
, nMaxBytesToRead
);
96 void SAL_CALL
OInputStreamWrapper::skipBytes(sal_Int32 nBytesToSkip
)
98 ::osl::MutexGuard
aGuard( m_aMutex
);
101 m_pSvStream
->SeekRel(nBytesToSkip
);
105 sal_Int32 SAL_CALL
OInputStreamWrapper::available()
107 ::osl::MutexGuard
aGuard( m_aMutex
);
110 sal_Int64 nAvailable
= m_pSvStream
->remainingSize();
113 return std::min
<sal_Int64
>(SAL_MAX_INT32
, nAvailable
);
116 void SAL_CALL
OInputStreamWrapper::closeInput()
118 ::osl::MutexGuard
aGuard( m_aMutex
);
121 if (m_bSvStreamOwner
)
124 m_pSvStream
= nullptr;
127 void OInputStreamWrapper::checkConnected() const
130 throw css::io::NotConnectedException(OUString(), const_cast<css::uno::XWeak
*>(static_cast<const css::uno::XWeak
*>(this)));
133 void OInputStreamWrapper::checkError() const
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
);
161 m_pSvStream
->Seek(static_cast<sal_uInt32
>(_nLocation
));
165 sal_Int64 SAL_CALL
OSeekableInputStreamWrapper::getPosition( )
167 ::osl::MutexGuard
aGuard( m_aMutex
);
170 sal_uInt32 nPos
= m_pSvStream
->Tell();
172 return static_cast<sal_Int64
>(nPos
);
175 sal_Int64 SAL_CALL
OSeekableInputStreamWrapper::getLength( )
177 ::osl::MutexGuard
aGuard( m_aMutex
);
182 sal_Int64 nEndPos
= m_pSvStream
->TellEnd();
187 //= OOutputStreamWrapper
189 OOutputStreamWrapper::OOutputStreamWrapper(SvStream
& _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()
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
);
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
));
257 sal_Int64 SAL_CALL
OSeekableOutputStreamWrapper::getPosition( )
259 sal_uInt32 nPos
= rStream
.Tell();
261 return static_cast<sal_Int64
>(nPos
);
264 sal_Int64 SAL_CALL
OSeekableOutputStreamWrapper::getLength( )
268 sal_Int64 nEndPos
= rStream
.TellEnd();
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( )
290 css::uno::Reference
< css::io::XOutputStream
> SAL_CALL
OStreamWrapper::getOutputStream( )
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);
325 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */