fdo#74697 Add Bluez 5 support for impress remote.
[LibreOffice.git] / comphelper / source / streaming / seqstream.cxx
blob242af3a39b4c071b686bed8576ec3dfe8836af9e
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> // for memcpy
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;
31 //---------------------------------------------------------------------------------------------
32 // class SequenceInputStream
33 //---------------------------------------------------------------------------------------------
35 //------------------------------------------------------------------
36 SequenceInputStream::SequenceInputStream(const ByteSequence& rData)
37 : m_aData(rData)
38 , m_nPos(0)
42 // checks if closed, returns available size, not mutex-protected
43 //------------------------------------------------------------------
44 inline sal_Int32 SequenceInputStream::avail()
46 if (m_nPos == -1)
47 throw NotConnectedException(OUString(), *this);
49 return m_aData.getLength() - m_nPos;
52 // com::sun::star::io::XInputStream
53 //------------------------------------------------------------------
54 sal_Int32 SAL_CALL SequenceInputStream::readBytes( Sequence<sal_Int8>& aData, sal_Int32 nBytesToRead )
55 throw(NotConnectedException, BufferSizeExceededException,
56 IOException, RuntimeException)
58 ::osl::MutexGuard aGuard( m_aMutex );
60 sal_Int32 nAvail = avail();
62 if (nBytesToRead < 0)
63 throw BufferSizeExceededException(OUString(),*this);
65 if (nAvail < nBytesToRead)
66 nBytesToRead = nAvail;
68 aData.realloc(nBytesToRead);
69 memcpy(aData.getArray(), m_aData.getConstArray() + m_nPos, nBytesToRead);
70 m_nPos += nBytesToRead;
72 return nBytesToRead;
75 //------------------------------------------------------------------
76 sal_Int32 SAL_CALL SequenceInputStream::readSomeBytes( Sequence<sal_Int8>& aData, sal_Int32 nMaxBytesToRead )
77 throw(NotConnectedException, BufferSizeExceededException,
78 IOException, RuntimeException)
80 // all data is available at once
81 return readBytes(aData, nMaxBytesToRead);
84 //------------------------------------------------------------------
85 void SAL_CALL SequenceInputStream::skipBytes( sal_Int32 nBytesToSkip )
86 throw(NotConnectedException, BufferSizeExceededException,
87 IOException, RuntimeException)
89 ::osl::MutexGuard aGuard( m_aMutex );
91 sal_Int32 nAvail = avail();
93 if (nBytesToSkip < 0)
94 throw BufferSizeExceededException(OUString(),*this);
96 if (nAvail < nBytesToSkip)
97 nBytesToSkip = nAvail;
99 m_nPos += nBytesToSkip;
102 //------------------------------------------------------------------
103 sal_Int32 SAL_CALL SequenceInputStream::available( )
104 throw(NotConnectedException, IOException, RuntimeException)
106 ::osl::MutexGuard aGuard( m_aMutex );
108 return avail();
111 //------------------------------------------------------------------
112 void SAL_CALL SequenceInputStream::closeInput( )
113 throw(NotConnectedException, IOException, RuntimeException)
115 if (m_nPos == -1)
116 throw NotConnectedException(OUString(), *this);
118 m_nPos = -1;
121 void SAL_CALL SequenceInputStream::seek( sal_Int64 location ) throw (IllegalArgumentException, IOException, RuntimeException)
123 if ( location > m_aData.getLength() || location < 0 || location > SAL_MAX_INT32 )
124 throw IllegalArgumentException();
125 m_nPos = (sal_Int32) location;
128 sal_Int64 SAL_CALL SequenceInputStream::getPosition() throw (IOException, RuntimeException)
130 return m_nPos;
133 sal_Int64 SAL_CALL SequenceInputStream::getLength( ) throw (IOException, RuntimeException)
135 return m_aData.getLength();
138 //--------------------------------------------------------------------------
139 OSequenceOutputStream::OSequenceOutputStream(Sequence< sal_Int8 >& _rSeq, double _nResizeFactor, sal_Int32 _nMinimumResize, sal_Int32 _nMaximumResize)
140 :m_rSequence(_rSeq)
141 ,m_nResizeFactor(_nResizeFactor)
142 ,m_nMinimumResize(_nMinimumResize)
143 ,m_nMaximumResize(_nMaximumResize)
144 ,m_nSize(0) // starting at position 0
145 ,m_bConnected(sal_True)
147 OSL_ENSURE(m_nResizeFactor > 1, "OSequenceOutputStream::OSequenceOutputStream : invalid resize factor !");
148 OSL_ENSURE((m_nMaximumResize < 0) || (m_nMaximumResize > m_nMinimumResize),
149 "OSequenceOutputStream::OSequenceOutputStream : these limits don't make any sense !");
151 if (m_nResizeFactor <= 1)
152 m_nResizeFactor = 1.3;
153 if ((m_nMaximumResize >= 0) && (m_nMaximumResize <= m_nMinimumResize))
154 m_nMaximumResize = m_nMinimumResize * 2;
155 // this heuristic is as good as any other ... supply better parameters if you don't like it :)
158 //--------------------------------------------------------------------------
159 void SAL_CALL OSequenceOutputStream::writeBytes( const Sequence< sal_Int8 >& _rData ) throw(NotConnectedException, BufferSizeExceededException, IOException, RuntimeException)
161 MutexGuard aGuard(m_aMutex);
162 if (!m_bConnected)
163 throw NotConnectedException();
165 // ensure the sequence has enoungh space left
166 if (m_nSize + _rData.getLength() > m_rSequence.getLength())
168 sal_Int32 nCurrentLength = m_rSequence.getLength();
169 sal_Int32 nNewLength = static_cast< sal_Int32 >(
170 nCurrentLength * m_nResizeFactor);
172 if (m_nMinimumResize > nNewLength - nCurrentLength)
173 // we have a minimum so it's not too inefficient for small sequences and small write requests
174 nNewLength = nCurrentLength + m_nMinimumResize;
176 if ((m_nMaximumResize > 0) && (nNewLength - nCurrentLength > m_nMaximumResize))
177 // such a large step is not allowed
178 nNewLength = nCurrentLength + m_nMaximumResize;
180 if (nNewLength < m_nSize + _rData.getLength())
181 { // it's not enough .... the data would not fit
183 // let's take the double amount of the length of the data to be written, as the next write
184 // request could be as large as this one
185 sal_Int32 nNewGrowth = _rData.getLength() * 2;
186 if ((m_nMaximumResize > 0) && (nNewGrowth > m_nMaximumResize))
187 { // we came to the limit, again ...
188 nNewGrowth = m_nMaximumResize;
189 if (nNewGrowth + nCurrentLength < m_nSize + _rData.getLength())
190 // but it would not fit if we respect the limit
191 nNewGrowth = m_nSize + _rData.getLength() - nCurrentLength;
193 nNewLength = nCurrentLength + nNewGrowth;
196 // round it off to the next multiple of 4 ...
197 nNewLength = (nNewLength + 3) / 4 * 4;
199 m_rSequence.realloc(nNewLength);
202 OSL_ENSURE(m_rSequence.getLength() >= m_nSize + _rData.getLength(),
203 "ooops ... the realloc algorithm seems to be wrong :( !");
205 memcpy(m_rSequence.getArray() + m_nSize, _rData.getConstArray(), _rData.getLength());
206 m_nSize += _rData.getLength();
209 //--------------------------------------------------------------------------
210 void SAL_CALL OSequenceOutputStream::flush( ) throw(NotConnectedException, BufferSizeExceededException, IOException, RuntimeException)
212 MutexGuard aGuard(m_aMutex);
213 if (!m_bConnected)
214 throw NotConnectedException();
216 // cut the sequence to the real size
217 m_rSequence.realloc(m_nSize);
220 //--------------------------------------------------------------------------
221 void SAL_CALL OSequenceOutputStream::closeOutput( ) throw(NotConnectedException, BufferSizeExceededException, IOException, RuntimeException)
223 MutexGuard aGuard(m_aMutex);
224 if (!m_bConnected)
225 throw NotConnectedException();
227 // cut the sequence to the real size
228 m_rSequence.realloc(m_nSize);
229 // and don't allow any further accesses
230 m_bConnected = sal_False;
233 } // namespace comphelper
235 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */