LibreOffice Module unotools (master) 1
streamwrap.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#include <sal/config.h>
21
22#include <com/sun/star/io/BufferSizeExceededException.hpp>
23#include <com/sun/star/io/NotConnectedException.hpp>
24#include <o3tl/safeint.hxx>
26#include <tools/stream.hxx>
27
28namespace utl
29{
30
31using namespace ::com::sun::star::uno;
32using namespace ::com::sun::star::io;
33using namespace ::com::sun::star::lang;
34
36 :m_pSvStream(&_rStream)
37 ,m_bSvStreamOwner(false)
38{
39}
40
42 :m_pSvStream( pStream )
43 ,m_bSvStreamOwner( bOwner )
44{
45}
46
47OInputStreamWrapper::OInputStreamWrapper( std::unique_ptr<SvStream> pStream )
48 :m_pSvStream( pStream.release() )
49 ,m_bSvStreamOwner( true )
50{
51}
52
54{
56 delete m_pSvStream;
57}
58
59sal_Int32 SAL_CALL OInputStreamWrapper::readBytes(css::uno::Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead)
60{
62
63 if (nBytesToRead < 0)
64 throw css::io::BufferSizeExceededException(OUString(), getXWeak());
65
66 std::scoped_lock aGuard( m_aMutex );
67
68 if (aData.getLength() < nBytesToRead)
69 aData.realloc(nBytesToRead);
70
71 sal_uInt32 nRead = m_pSvStream->ReadBytes(static_cast<void*>(aData.getArray()), nBytesToRead);
72 checkError();
73
74 // If read characters < MaxLength, adjust css::uno::Sequence
75 if (nRead < o3tl::make_unsigned(aData.getLength()))
76 aData.realloc( nRead );
77
78 return nRead;
79}
80
81sal_Int32 OInputStreamWrapper::readSomeBytes(sal_Int8* pData, sal_Int32 nBytesToRead)
82{
84
85 if (nBytesToRead < 0)
86 throw css::io::BufferSizeExceededException(OUString(), getXWeak());
87
88 std::scoped_lock aGuard( m_aMutex );
89
90 sal_uInt32 nRead = m_pSvStream->ReadBytes(static_cast<void*>(pData), nBytesToRead);
91 checkError();
92
93 return nRead;
94}
95
96sal_Int32 SAL_CALL OInputStreamWrapper::readSomeBytes(css::uno::Sequence< sal_Int8 >& aData, sal_Int32 nMaxBytesToRead)
97{
98 checkError();
99
100 if (nMaxBytesToRead < 0)
101 throw css::io::BufferSizeExceededException(OUString(), getXWeak());
102
103 if (m_pSvStream->eof())
104 {
105 aData.realloc(0);
106 return 0;
107 }
108 else
109 return readBytes(aData, nMaxBytesToRead);
110}
111
112void SAL_CALL OInputStreamWrapper::skipBytes(sal_Int32 nBytesToSkip)
113{
114 std::scoped_lock aGuard( m_aMutex );
115 checkError();
116
117 m_pSvStream->SeekRel(nBytesToSkip);
118 checkError();
119}
120
122{
123 std::scoped_lock aGuard( m_aMutex );
125
126 sal_Int64 nAvailable = m_pSvStream->remainingSize();
127 checkError();
128
129 return std::min<sal_Int64>(SAL_MAX_INT32, nAvailable);
130}
131
133{
134 std::scoped_lock aGuard( m_aMutex );
135 if (m_pSvStream)
136 {
138 delete m_pSvStream;
139
140 m_pSvStream = nullptr;
141 }
142}
143
145{
146 if (!m_pSvStream)
147 throw css::io::NotConnectedException(OUString(), const_cast<OInputStreamWrapper*>(this)->getXWeak());
148}
149
151{
153
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());
158}
159
160//= OSeekableInputStreamWrapper
161
163
165{
166 SetStream( &_rStream, false );
167}
168
170{
171 SetStream( _pStream, _bOwner );
172}
173
174void SAL_CALL OSeekableInputStreamWrapper::seek( sal_Int64 _nLocation )
175{
176 std::scoped_lock aGuard( m_aMutex );
177 checkConnected();
178
179 m_pSvStream->Seek(static_cast<sal_uInt64>(_nLocation));
180 checkError();
181}
182
184{
185 std::scoped_lock aGuard( m_aMutex );
186 checkConnected();
187
188 sal_uInt32 nPos = m_pSvStream->Tell();
189 checkError();
190 return static_cast<sal_Int64>(nPos);
191}
192
194{
195 std::scoped_lock aGuard( m_aMutex );
196 checkConnected();
197
198 checkError();
199
200 sal_Int64 nEndPos = m_pSvStream->TellEnd();
201
202 return nEndPos;
203}
204
205//= OOutputStreamWrapper
206
208 rStream(_rStream)
209{}
210
212
213void SAL_CALL OOutputStreamWrapper::writeBytes(const css::uno::Sequence< sal_Int8 >& aData)
214{
215 sal_uInt32 nWritten = rStream.WriteBytes(aData.getConstArray(), aData.getLength());
217 if ( (ERRCODE_NONE != err)
218 || (nWritten != static_cast<sal_uInt32>(aData.getLength()))
219 )
220 {
221 throw css::io::BufferSizeExceededException(OUString(), getXWeak());
222 }
223}
224
226{
228 checkError();
229}
230
232{
233}
234
236{
238 // TODO: really evaluate the error
239 throw css::io::NotConnectedException(OUString(), const_cast<OOutputStreamWrapper*>(this)->getXWeak());
240}
241
242//= OSeekableOutputStreamWrapper
243
245 :OOutputStreamWrapper(_rStream)
246{
247}
248
250
252{
253 Any aReturn = OOutputStreamWrapper::queryInterface(_rType);
254 if (!aReturn.hasValue())
256 return aReturn;
257}
258
259void SAL_CALL OSeekableOutputStreamWrapper::seek( sal_Int64 _nLocation )
260{
261 rStream.Seek(static_cast<sal_uInt32>(_nLocation));
262 checkError();
263}
264
266{
267 sal_uInt32 nPos = rStream.Tell();
268 checkError();
269 return static_cast<sal_Int64>(nPos);
270}
271
273{
274 checkError();
275
276 sal_Int64 nEndPos = rStream.TellEnd();
277
278 return nEndPos;
279}
280
282
284{
285 SetStream( &_rStream, false );
286}
287
288OStreamWrapper::OStreamWrapper(std::unique_ptr<SvStream> pStream)
289{
290 SetStream( pStream.release(), true );
291}
292
294{
295 SetStream( pStream, bOwner );
296}
297
298css::uno::Reference< css::io::XInputStream > SAL_CALL OStreamWrapper::getInputStream( )
299{
300 return this;
301}
302
303css::uno::Reference< css::io::XOutputStream > SAL_CALL OStreamWrapper::getOutputStream( )
304{
305 return this;
306}
307
308void SAL_CALL OStreamWrapper::writeBytes(const css::uno::Sequence< sal_Int8 >& aData)
309{
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()))
314 )
315 {
316 throw css::io::BufferSizeExceededException(OUString(), getXWeak());
317 }
318}
319
321{
322 m_pSvStream->FlushBuffer();
323 if (m_pSvStream->GetError() != ERRCODE_NONE)
324 throw css::io::NotConnectedException(OUString(), getXWeak());
325}
326
328{
329}
330
332{
333 m_pSvStream->SetStreamSize(0);
334}
335
336} // namespace utl
337
338/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
sal_uInt64 Tell() const
virtual sal_uInt64 TellEnd()
std::size_t WriteBytes(const void *pData, std::size_t nSize)
bool eof() const
sal_uInt64 Seek(sal_uInt64 nPos)
std::size_t ReadBytes(void *pData, std::size_t nSize)
sal_uInt64 SeekRel(sal_Int64 nPos)
ErrCode GetError() const
void FlushBuffer()
sal_uInt64 remainingSize()
virtual css::uno::Any SAL_CALL queryInterface(css::uno::Type const &rType) SAL_OVERRIDE
helper class for wrapping an SvStream into a com.sun.star.io::XInputStream
Definition: streamwrap.hxx:46
virtual void SAL_CALL closeInput() override
Definition: streamwrap.cxx:132
virtual sal_Int32 SAL_CALL available() override
Definition: streamwrap.cxx:121
void checkError() const
throws an exception according to the error flag of m_pSvStream
Definition: streamwrap.cxx:150
virtual sal_Int32 SAL_CALL readBytes(css::uno::Sequence< sal_Int8 > &aData, sal_Int32 nBytesToRead) override
Definition: streamwrap.cxx:59
void checkConnected() const
throws a NotConnectedException if the object is not connected anymore
Definition: streamwrap.cxx:144
virtual sal_Int32 SAL_CALL readSomeBytes(css::uno::Sequence< sal_Int8 > &aData, sal_Int32 nMaxBytesToRead) override
Definition: streamwrap.cxx:96
virtual void SAL_CALL skipBytes(sal_Int32 nBytesToSkip) override
Definition: streamwrap.cxx:112
virtual ~OInputStreamWrapper() override
Definition: streamwrap.cxx:53
UNOTOOLS_DLLPUBLIC OOutputStreamWrapper(SvStream &_rStream)
Definition: streamwrap.cxx:207
void checkError() const
throws an exception according to the error flag of m_pSvStream
Definition: streamwrap.cxx:235
virtual void SAL_CALL closeOutput() override
Definition: streamwrap.cxx:231
virtual ~OOutputStreamWrapper() override
Definition: streamwrap.cxx:211
virtual void SAL_CALL flush() override
Definition: streamwrap.cxx:225
virtual void SAL_CALL writeBytes(const css::uno::Sequence< sal_Int8 > &aData) override
Definition: streamwrap.cxx:213
virtual void SAL_CALL seek(sal_Int64 _nLocation) override
Definition: streamwrap.cxx:174
virtual sal_Int64 SAL_CALL getLength() override
Definition: streamwrap.cxx:193
virtual sal_Int64 SAL_CALL getPosition() override
Definition: streamwrap.cxx:183
virtual ~OSeekableOutputStreamWrapper() override
Definition: streamwrap.cxx:249
OSeekableOutputStreamWrapper(SvStream &_rStream)
Definition: streamwrap.cxx:244
virtual sal_Int64 SAL_CALL getLength() override
Definition: streamwrap.cxx:272
virtual void SAL_CALL seek(sal_Int64 _nLocation) override
Definition: streamwrap.cxx:259
virtual css::uno::Any SAL_CALL queryInterface(const css::uno::Type &_rType) override
Definition: streamwrap.cxx:251
virtual sal_Int64 SAL_CALL getPosition() override
Definition: streamwrap.cxx:265
~OStreamWrapper() override
virtual void SAL_CALL writeBytes(const css::uno::Sequence< sal_Int8 > &aData) override
Definition: streamwrap.cxx:308
OStreamWrapper(SvStream &_rStream)
Definition: streamwrap.cxx:283
virtual css::uno::Reference< css::io::XInputStream > SAL_CALL getInputStream() override
Definition: streamwrap.cxx:298
virtual void SAL_CALL closeOutput() override
Definition: streamwrap.cxx:327
virtual css::uno::Reference< css::io::XOutputStream > SAL_CALL getOutputStream() override
Definition: streamwrap.cxx:303
virtual void SAL_CALL flush() override
Definition: streamwrap.cxx:320
virtual void SAL_CALL truncate() override
Definition: streamwrap.cxx:331
#define ERRCODE_NONE
sal_uInt16 nPos
std::unique_ptr< sal_Int32[]> pData
constexpr OUStringLiteral aData
err
Type
constexpr std::enable_if_t< std::is_signed_v< T >, std::make_unsigned_t< T > > make_unsigned(T value)
#define SAL_MAX_INT32
signed char sal_Int8
osl::Mutex m_aMutex