Line data Source code
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 <comphelper/seqstream.hxx>
21 :
22 : #include <memory.h> // for memcpy
23 :
24 : namespace comphelper
25 : {
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;
30 :
31 : //---------------------------------------------------------------------------------------------
32 : // class SequenceInputStream
33 : //---------------------------------------------------------------------------------------------
34 :
35 : //------------------------------------------------------------------
36 1360 : SequenceInputStream::SequenceInputStream(const ByteSequence& rData)
37 : : m_aData(rData)
38 1360 : , m_nPos(0)
39 : {
40 1360 : }
41 :
42 : // checks if closed, returns available size, not mutex-protected
43 : //------------------------------------------------------------------
44 1552 : inline sal_Int32 SequenceInputStream::avail()
45 : {
46 1552 : if (m_nPos == -1)
47 0 : throw NotConnectedException(::rtl::OUString(), *this);
48 :
49 1552 : return m_aData.getLength() - m_nPos;
50 : }
51 :
52 : // com::sun::star::io::XInputStream
53 : //------------------------------------------------------------------
54 1552 : sal_Int32 SAL_CALL SequenceInputStream::readBytes( Sequence<sal_Int8>& aData, sal_Int32 nBytesToRead )
55 : throw(NotConnectedException, BufferSizeExceededException,
56 : IOException, RuntimeException)
57 : {
58 1552 : ::osl::MutexGuard aGuard( m_aMutex );
59 :
60 1552 : sal_Int32 nAvail = avail();
61 :
62 1552 : if (nBytesToRead < 0)
63 0 : throw BufferSizeExceededException(::rtl::OUString(),*this);
64 :
65 1552 : if (nAvail < nBytesToRead)
66 1360 : nBytesToRead = nAvail;
67 :
68 1552 : aData.realloc(nBytesToRead);
69 1552 : memcpy(aData.getArray(), m_aData.getConstArray() + m_nPos, nBytesToRead);
70 1552 : m_nPos += nBytesToRead;
71 :
72 1552 : return nBytesToRead;
73 : }
74 :
75 : //------------------------------------------------------------------
76 0 : sal_Int32 SAL_CALL SequenceInputStream::readSomeBytes( Sequence<sal_Int8>& aData, sal_Int32 nMaxBytesToRead )
77 : throw(NotConnectedException, BufferSizeExceededException,
78 : IOException, RuntimeException)
79 : {
80 : // all data is available at once
81 0 : return readBytes(aData, nMaxBytesToRead);
82 : }
83 :
84 : //------------------------------------------------------------------
85 0 : void SAL_CALL SequenceInputStream::skipBytes( sal_Int32 nBytesToSkip )
86 : throw(NotConnectedException, BufferSizeExceededException,
87 : IOException, RuntimeException)
88 : {
89 0 : ::osl::MutexGuard aGuard( m_aMutex );
90 :
91 0 : sal_Int32 nAvail = avail();
92 :
93 0 : if (nBytesToSkip < 0)
94 0 : throw BufferSizeExceededException(::rtl::OUString(),*this);
95 :
96 0 : if (nAvail < nBytesToSkip)
97 0 : nBytesToSkip = nAvail;
98 :
99 0 : m_nPos += nBytesToSkip;
100 0 : }
101 :
102 : //------------------------------------------------------------------
103 0 : sal_Int32 SAL_CALL SequenceInputStream::available( )
104 : throw(NotConnectedException, IOException, RuntimeException)
105 : {
106 0 : ::osl::MutexGuard aGuard( m_aMutex );
107 :
108 0 : return avail();
109 : }
110 :
111 : //------------------------------------------------------------------
112 0 : void SAL_CALL SequenceInputStream::closeInput( )
113 : throw(NotConnectedException, IOException, RuntimeException)
114 : {
115 0 : if (m_nPos == -1)
116 0 : throw NotConnectedException(::rtl::OUString(), *this);
117 :
118 0 : m_nPos = -1;
119 0 : }
120 :
121 1552 : void SAL_CALL SequenceInputStream::seek( sal_Int64 location ) throw (IllegalArgumentException, IOException, RuntimeException)
122 : {
123 1552 : if ( location > m_aData.getLength() || location < 0 || location > SAL_MAX_INT32 )
124 0 : throw IllegalArgumentException();
125 1552 : m_nPos = (sal_Int32) location;
126 1552 : }
127 :
128 0 : sal_Int64 SAL_CALL SequenceInputStream::getPosition() throw (IOException, RuntimeException)
129 : {
130 0 : return m_nPos;
131 : }
132 :
133 1360 : sal_Int64 SAL_CALL SequenceInputStream::getLength( ) throw (IOException, RuntimeException)
134 : {
135 1360 : return m_aData.getLength();
136 : }
137 :
138 : //--------------------------------------------------------------------------
139 0 : 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 0 : ,m_bConnected(sal_True)
146 : {
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 !");
150 :
151 0 : if (m_nResizeFactor <= 1)
152 0 : m_nResizeFactor = 1.3;
153 0 : if ((m_nMaximumResize >= 0) && (m_nMaximumResize <= m_nMinimumResize))
154 0 : m_nMaximumResize = m_nMinimumResize * 2;
155 : // this heuristic is as good as any other ... supply better parameters if you don't like it :)
156 0 : }
157 :
158 : //--------------------------------------------------------------------------
159 0 : void SAL_CALL OSequenceOutputStream::writeBytes( const Sequence< sal_Int8 >& _rData ) throw(NotConnectedException, BufferSizeExceededException, IOException, RuntimeException)
160 : {
161 0 : MutexGuard aGuard(m_aMutex);
162 0 : if (!m_bConnected)
163 0 : throw NotConnectedException();
164 :
165 : // ensure the sequence has enoungh space left
166 0 : if (m_nSize + _rData.getLength() > m_rSequence.getLength())
167 : {
168 0 : sal_Int32 nCurrentLength = m_rSequence.getLength();
169 : sal_Int32 nNewLength = static_cast< sal_Int32 >(
170 0 : nCurrentLength * m_nResizeFactor);
171 :
172 0 : if (m_nMinimumResize > nNewLength - nCurrentLength)
173 : // we have a minimum so it's not too inefficient for small sequences and small write requests
174 0 : nNewLength = nCurrentLength + m_nMinimumResize;
175 :
176 0 : if ((m_nMaximumResize > 0) && (nNewLength - nCurrentLength > m_nMaximumResize))
177 : // such a large step is not allowed
178 0 : nNewLength = nCurrentLength + m_nMaximumResize;
179 :
180 0 : if (nNewLength < m_nSize + _rData.getLength())
181 : { // it's not enough .... the data would not fit
182 :
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 0 : sal_Int32 nNewGrowth = _rData.getLength() * 2;
186 0 : if ((m_nMaximumResize > 0) && (nNewGrowth > m_nMaximumResize))
187 : { // we came to the limit, again ...
188 0 : nNewGrowth = m_nMaximumResize;
189 0 : if (nNewGrowth + nCurrentLength < m_nSize + _rData.getLength())
190 : // but it would not fit if we respect the limit
191 0 : nNewGrowth = m_nSize + _rData.getLength() - nCurrentLength;
192 : }
193 0 : nNewLength = nCurrentLength + nNewGrowth;
194 : }
195 :
196 : // round it off to the next multiple of 4 ...
197 0 : nNewLength = (nNewLength + 3) / 4 * 4;
198 :
199 0 : m_rSequence.realloc(nNewLength);
200 : }
201 :
202 : OSL_ENSURE(m_rSequence.getLength() >= m_nSize + _rData.getLength(),
203 : "ooops ... the realloc algorithm seems to be wrong :( !");
204 :
205 0 : memcpy(m_rSequence.getArray() + m_nSize, _rData.getConstArray(), _rData.getLength());
206 0 : m_nSize += _rData.getLength();
207 0 : }
208 :
209 : //--------------------------------------------------------------------------
210 0 : void SAL_CALL OSequenceOutputStream::flush( ) throw(NotConnectedException, BufferSizeExceededException, IOException, RuntimeException)
211 : {
212 0 : MutexGuard aGuard(m_aMutex);
213 0 : if (!m_bConnected)
214 0 : throw NotConnectedException();
215 :
216 : // cut the sequence to the real size
217 0 : m_rSequence.realloc(m_nSize);
218 0 : }
219 :
220 : //--------------------------------------------------------------------------
221 0 : void SAL_CALL OSequenceOutputStream::closeOutput( ) throw(NotConnectedException, BufferSizeExceededException, IOException, RuntimeException)
222 : {
223 0 : MutexGuard aGuard(m_aMutex);
224 0 : if (!m_bConnected)
225 0 : throw NotConnectedException();
226 :
227 : // cut the sequence to the real size
228 0 : m_rSequence.realloc(m_nSize);
229 : // and don't allow any further accesses
230 0 : m_bConnected = sal_False;
231 0 : }
232 :
233 : } // namespace comphelper
234 :
235 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|