Bump version to 4.3-4
[LibreOffice.git] / comphelper / source / streaming / seqstream.cxx
blob5bc44a52e8776d6c00ea39a9cb7e234b5af75de5
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 <comphelper/seqstream.hxx>
22 #include <memory.h>
24 namespace comphelper
26 using namespace ::com::sun::star::lang;
27 using namespace ::com::sun::star::io;
28 using namespace ::com::sun::star::uno;
29 using namespace ::osl;
32 // class SequenceInputStream
36 SequenceInputStream::SequenceInputStream(
37 css::uno::Sequence<sal_Int8> const & rData)
38 : m_aData(rData)
39 , m_nPos(0)
43 // checks if closed, returns available size, not mutex-protected
45 inline sal_Int32 SequenceInputStream::avail()
47 if (m_nPos == -1)
48 throw NotConnectedException(OUString(), *this);
50 return m_aData.getLength() - m_nPos;
53 // com::sun::star::io::XInputStream
55 sal_Int32 SAL_CALL SequenceInputStream::readBytes( Sequence<sal_Int8>& aData, sal_Int32 nBytesToRead )
56 throw(NotConnectedException, BufferSizeExceededException,
57 IOException, RuntimeException, std::exception)
59 ::osl::MutexGuard aGuard( m_aMutex );
61 sal_Int32 nAvail = avail();
63 if (nBytesToRead < 0)
64 throw BufferSizeExceededException(OUString(),*this);
66 if (nAvail < nBytesToRead)
67 nBytesToRead = nAvail;
69 aData.realloc(nBytesToRead);
70 memcpy(aData.getArray(), m_aData.getConstArray() + m_nPos, nBytesToRead);
71 m_nPos += nBytesToRead;
73 return nBytesToRead;
77 sal_Int32 SAL_CALL SequenceInputStream::readSomeBytes( Sequence<sal_Int8>& aData, sal_Int32 nMaxBytesToRead )
78 throw(NotConnectedException, BufferSizeExceededException,
79 IOException, RuntimeException, std::exception)
81 // all data is available at once
82 return readBytes(aData, nMaxBytesToRead);
86 void SAL_CALL SequenceInputStream::skipBytes( sal_Int32 nBytesToSkip )
87 throw(NotConnectedException, BufferSizeExceededException,
88 IOException, RuntimeException, std::exception)
90 ::osl::MutexGuard aGuard( m_aMutex );
92 sal_Int32 nAvail = avail();
94 if (nBytesToSkip < 0)
95 throw BufferSizeExceededException(OUString(),*this);
97 if (nAvail < nBytesToSkip)
98 nBytesToSkip = nAvail;
100 m_nPos += nBytesToSkip;
104 sal_Int32 SAL_CALL SequenceInputStream::available( )
105 throw(NotConnectedException, IOException, RuntimeException, std::exception)
107 ::osl::MutexGuard aGuard( m_aMutex );
109 return avail();
113 void SAL_CALL SequenceInputStream::closeInput( )
114 throw(NotConnectedException, IOException, RuntimeException, std::exception)
116 if (m_nPos == -1)
117 throw NotConnectedException(OUString(), *this);
119 m_nPos = -1;
122 void SAL_CALL SequenceInputStream::seek( sal_Int64 location ) throw (IllegalArgumentException, IOException, RuntimeException, std::exception)
124 if ( location > m_aData.getLength() || location < 0 || location > SAL_MAX_INT32 )
125 throw IllegalArgumentException();
126 m_nPos = (sal_Int32) location;
129 sal_Int64 SAL_CALL SequenceInputStream::getPosition() throw (IOException, RuntimeException, std::exception)
131 return m_nPos;
134 sal_Int64 SAL_CALL SequenceInputStream::getLength( ) throw (IOException, RuntimeException, std::exception)
136 return m_aData.getLength();
140 OSequenceOutputStream::OSequenceOutputStream(Sequence< sal_Int8 >& _rSeq, double _nResizeFactor, sal_Int32 _nMinimumResize, sal_Int32 _nMaximumResize)
141 :m_rSequence(_rSeq)
142 ,m_nResizeFactor(_nResizeFactor)
143 ,m_nMinimumResize(_nMinimumResize)
144 ,m_nMaximumResize(_nMaximumResize)
145 ,m_nSize(0) // starting at position 0
146 ,m_bConnected(true)
148 OSL_ENSURE(m_nResizeFactor > 1, "OSequenceOutputStream::OSequenceOutputStream : invalid resize factor !");
149 OSL_ENSURE((m_nMaximumResize < 0) || (m_nMaximumResize > m_nMinimumResize),
150 "OSequenceOutputStream::OSequenceOutputStream : these limits don't make any sense !");
152 if (m_nResizeFactor <= 1)
153 m_nResizeFactor = 1.3;
154 if ((m_nMaximumResize >= 0) && (m_nMaximumResize <= m_nMinimumResize))
155 m_nMaximumResize = m_nMinimumResize * 2;
156 // this heuristic is as good as any other ... supply better parameters if you don't like it :)
160 void SAL_CALL OSequenceOutputStream::writeBytes( const Sequence< sal_Int8 >& _rData ) throw(NotConnectedException, BufferSizeExceededException, IOException, RuntimeException, std::exception)
162 MutexGuard aGuard(m_aMutex);
163 if (!m_bConnected)
164 throw NotConnectedException();
166 // ensure the sequence has enough space left
167 if (m_nSize + _rData.getLength() > m_rSequence.getLength())
169 sal_Int32 nCurrentLength = m_rSequence.getLength();
170 sal_Int32 nNewLength = static_cast< sal_Int32 >(
171 nCurrentLength * m_nResizeFactor);
173 if (m_nMinimumResize > nNewLength - nCurrentLength)
174 // we have a minimum so it's not too inefficient for small sequences and small write requests
175 nNewLength = nCurrentLength + m_nMinimumResize;
177 if ((m_nMaximumResize > 0) && (nNewLength - nCurrentLength > m_nMaximumResize))
178 // such a large step is not allowed
179 nNewLength = nCurrentLength + m_nMaximumResize;
181 if (nNewLength < m_nSize + _rData.getLength())
182 { // it's not enough .... the data would not fit
184 // let's take the double amount of the length of the data to be written, as the next write
185 // request could be as large as this one
186 sal_Int32 nNewGrowth = _rData.getLength() * 2;
187 if ((m_nMaximumResize > 0) && (nNewGrowth > m_nMaximumResize))
188 { // we came to the limit, again ...
189 nNewGrowth = m_nMaximumResize;
190 if (nNewGrowth + nCurrentLength < m_nSize + _rData.getLength())
191 // but it would not fit if we respect the limit
192 nNewGrowth = m_nSize + _rData.getLength() - nCurrentLength;
194 nNewLength = nCurrentLength + nNewGrowth;
197 // round it off to the next multiple of 4 ...
198 nNewLength = (nNewLength + 3) / 4 * 4;
200 m_rSequence.realloc(nNewLength);
203 OSL_ENSURE(m_rSequence.getLength() >= m_nSize + _rData.getLength(),
204 "ooops ... the realloc algorithm seems to be wrong :( !");
206 memcpy(m_rSequence.getArray() + m_nSize, _rData.getConstArray(), _rData.getLength());
207 m_nSize += _rData.getLength();
211 void SAL_CALL OSequenceOutputStream::flush( ) throw(NotConnectedException, BufferSizeExceededException, IOException, RuntimeException, std::exception)
213 MutexGuard aGuard(m_aMutex);
214 if (!m_bConnected)
215 throw NotConnectedException();
217 // cut the sequence to the real size
218 m_rSequence.realloc(m_nSize);
222 void SAL_CALL OSequenceOutputStream::closeOutput( ) throw(NotConnectedException, BufferSizeExceededException, IOException, RuntimeException, std::exception)
224 MutexGuard aGuard(m_aMutex);
225 if (!m_bConnected)
226 throw NotConnectedException();
228 // cut the sequence to the real size
229 m_rSequence.realloc(m_nSize);
230 // and don't allow any further accesses
231 m_bConnected = false;
234 } // namespace comphelper
236 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */