LibreOffice Module comphelper (master) 1
seqstream.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 <com/sun/star/lang/IllegalArgumentException.hpp>
26
27#include <osl/diagnose.h>
28
29namespace comphelper
30{
31using namespace ::com::sun::star::lang;
32using namespace ::com::sun::star::io;
33using namespace ::com::sun::star::uno;
34using namespace ::osl;
35
36
37
38
40 const sal_Int8* pData, sal_Int32 nDataLength)
41: m_pMemoryData(pData)
42, m_nMemoryDataLength(nDataLength)
43, m_nPos(0)
44{
45}
46
47// checks if closed, returns available size, not mutex-protected
48
49inline sal_Int32 MemoryInputStream::avail()
50{
51 if (m_nPos == -1)
52 throw NotConnectedException(OUString(), *this);
53
55}
56
57// css::io::XInputStream
58
59sal_Int32 SAL_CALL MemoryInputStream::readBytes( Sequence<sal_Int8>& aData, sal_Int32 nBytesToRead )
60{
61 if (nBytesToRead < 0)
62 throw BufferSizeExceededException(OUString(),*this);
63
64 std::scoped_lock aGuard( m_aMutex );
65
66 sal_Int32 nAvail = avail();
67
68 if (nAvail < nBytesToRead)
69 nBytesToRead = nAvail;
70
71 aData.realloc(nBytesToRead);
72 memcpy(aData.getArray(), m_pMemoryData + m_nPos, nBytesToRead);
73 m_nPos += nBytesToRead;
74
75 return nBytesToRead;
76}
77
78sal_Int32 MemoryInputStream::readSomeBytes( sal_Int8* pData, sal_Int32 nBytesToRead )
79{
80 if (nBytesToRead < 0)
81 throw BufferSizeExceededException(OUString(),*this);
82
83 std::scoped_lock aGuard( m_aMutex );
84
85 sal_Int32 nAvail = avail();
86
87 if (nAvail < nBytesToRead)
88 nBytesToRead = nAvail;
89
90 memcpy(pData, m_pMemoryData + m_nPos, nBytesToRead);
91 m_nPos += nBytesToRead;
92
93 return nBytesToRead;
94}
95
96sal_Int32 SAL_CALL MemoryInputStream::readSomeBytes( Sequence<sal_Int8>& aData, sal_Int32 nMaxBytesToRead )
97{
98 // all data is available at once
99 return readBytes(aData, nMaxBytesToRead);
100}
101
102
103void SAL_CALL MemoryInputStream::skipBytes( sal_Int32 nBytesToSkip )
104{
105 if (nBytesToSkip < 0)
106 throw BufferSizeExceededException(OUString(),*this);
107
108 std::scoped_lock aGuard( m_aMutex );
109
110 sal_Int32 nAvail = avail();
111
112 if (nAvail < nBytesToSkip)
113 nBytesToSkip = nAvail;
114
115 m_nPos += nBytesToSkip;
116}
117
118
119sal_Int32 SAL_CALL MemoryInputStream::available( )
120{
121 std::scoped_lock aGuard( m_aMutex );
122
123 return avail();
124}
125
126
128{
129 std::scoped_lock aGuard( m_aMutex );
130
131 if (m_nPos == -1)
132 throw NotConnectedException(OUString(), *this);
133
134 m_nPos = -1;
135}
136
137void SAL_CALL MemoryInputStream::seek( sal_Int64 location )
138{
139 if ( location > m_nMemoryDataLength || location < 0 || location > SAL_MAX_INT32 )
140 throw IllegalArgumentException("bad location", static_cast<cppu::OWeakObject*>(this), 1);
141 std::scoped_lock aGuard( m_aMutex );
142 m_nPos = static_cast<sal_Int32>(location);
143}
144
146{
147 std::scoped_lock aGuard( m_aMutex );
148 return m_nPos;
149}
150
151sal_Int64 SAL_CALL MemoryInputStream::getLength( )
152{
153 std::scoped_lock aGuard( m_aMutex );
154 return m_nMemoryDataLength;
155}
156
157
159 css::uno::Sequence<sal_Int8> const & rData)
160: MemoryInputStream(rData.getConstArray(), rData.getLength())
161, m_aData(rData)
162{
163}
164
165
166OSequenceOutputStream::OSequenceOutputStream(Sequence< sal_Int8 >& _rSeq, double _nResizeFactor, sal_Int32 _nMinimumResize)
167 :m_rSequence(_rSeq)
168 ,m_nResizeFactor(_nResizeFactor)
169 ,m_nMinimumResize(_nMinimumResize)
170 ,m_nSize(0) // starting at position 0
171 ,m_bConnected(true)
172{
173 OSL_ENSURE(m_nResizeFactor > 1, "OSequenceOutputStream::OSequenceOutputStream : invalid resize factor !");
174
175 if (m_nResizeFactor <= 1)
176 m_nResizeFactor = 1.3;
177}
178
179
180void SAL_CALL OSequenceOutputStream::writeBytes( const Sequence< sal_Int8 >& _rData )
181{
182 std::scoped_lock aGuard(m_aMutex);
183 if (!m_bConnected)
184 throw NotConnectedException();
185
186 // ensure the sequence has enough space left
187 if (m_nSize + _rData.getLength() > m_rSequence.getLength())
188 {
189 sal_Int32 nCurrentLength = m_rSequence.getLength();
190 sal_Int32 nNewLength = static_cast< sal_Int32 >(
191 nCurrentLength * m_nResizeFactor);
192
193 if (m_nMinimumResize > nNewLength - nCurrentLength)
194 // we have a minimum so it's not too inefficient for small sequences and small write requests
195 nNewLength = nCurrentLength + m_nMinimumResize;
196
197 if (nNewLength < m_nSize + _rData.getLength())
198 { // it's not enough... the data would not fit
199
200 // let's take the double amount of the length of the data to be written, as the next write
201 // request could be as large as this one
202 sal_Int32 nNewGrowth = _rData.getLength() * 2;
203 nNewLength = nCurrentLength + nNewGrowth;
204 }
205
206 // round it off to the next multiple of 4...
207 nNewLength = (nNewLength + 3) / 4 * 4;
208
209 m_rSequence.realloc(nNewLength);
210 }
211
212 OSL_ENSURE(m_rSequence.getLength() >= m_nSize + _rData.getLength(),
213 "ooops ... the realloc algorithm seems to be wrong :( !");
214
215 memcpy(m_rSequence.getArray() + m_nSize, _rData.getConstArray(), _rData.getLength());
216 m_nSize += _rData.getLength();
217}
218
219
220void SAL_CALL OSequenceOutputStream::flush( )
221{
222 std::scoped_lock aGuard(m_aMutex);
223 if (!m_bConnected)
224 throw NotConnectedException();
225
226 // cut the sequence to the real size
227 m_rSequence.realloc(m_nSize);
228}
229
230void OSequenceOutputStream::finalizeOutput()
231{
232 // cut the sequence to the real size
233 m_rSequence.realloc(m_nSize);
234 // and don't allow any further accesses
235 m_bConnected = false;
236}
237
238void SAL_CALL OSequenceOutputStream::closeOutput()
239{
240 std::scoped_lock aGuard(m_aMutex);
241 if (!m_bConnected)
242 throw NotConnectedException();
243
244 finalizeOutput();
245}
246
247} // namespace comphelper
248
249/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Base class for wrappers around memory data that want to be exposed as an XInputStream.
Definition: seqstream.hxx:40
virtual void SAL_CALL seek(sal_Int64 location) override
Definition: seqstream.cxx:137
virtual sal_Int32 SAL_CALL readSomeBytes(css::uno::Sequence< sal_Int8 > &aData, sal_Int32 nMaxBytesToRead) override
const sal_Int8 * m_pMemoryData
Definition: seqstream.hxx:42
virtual void SAL_CALL skipBytes(sal_Int32 nBytesToSkip) override
Definition: seqstream.cxx:103
MemoryInputStream(const sal_Int8 *pData, sal_Int32 nDataLength)
Definition: seqstream.cxx:39
virtual void SAL_CALL closeInput() override
Definition: seqstream.cxx:127
virtual sal_Int32 SAL_CALL readBytes(css::uno::Sequence< sal_Int8 > &aData, sal_Int32 nBytesToRead) override
Definition: seqstream.cxx:59
virtual sal_Int32 SAL_CALL available() override
Definition: seqstream.cxx:119
virtual sal_Int64 SAL_CALL getLength() override
Definition: seqstream.cxx:151
virtual sal_Int64 SAL_CALL getPosition() override
Definition: seqstream.cxx:145
SequenceInputStream(css::uno::Sequence< sal_Int8 > const &rData)
Definition: seqstream.cxx:158
MapData m_aData
::osl::Mutex m_aMutex
size_t m_nPos
sal_uInt32 m_nSize
std::unique_ptr< sal_Int32[]> pData
constexpr OUStringLiteral aData
double getLength(const B2DPolygon &rCandidate)
#define SAL_MAX_INT32
signed char sal_Int8