1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 22 23 24 // MARKER(update_precomp.py): autogen include statement, do not remove 25 #include "precompiled_comphelper.hxx" 26 #include <comphelper/seqstream.hxx> 27 28 #include <memory.h> // for memcpy 29 30 namespace comphelper 31 { 32 using namespace ::com::sun::star::lang; 33 using namespace ::com::sun::star::io; 34 using namespace ::com::sun::star::uno; 35 using namespace ::osl; 36 37 //--------------------------------------------------------------------------------------------- 38 // class SequenceInputStream 39 //--------------------------------------------------------------------------------------------- 40 41 //------------------------------------------------------------------ 42 SequenceInputStream::SequenceInputStream(const ByteSequence& rData) 43 : m_aData(rData) 44 , m_nPos(0) 45 { 46 } 47 48 // checks if closed, returns available size, not mutex-protected 49 //------------------------------------------------------------------ 50 inline sal_Int32 SequenceInputStream::avail() 51 { 52 if (m_nPos == -1) 53 throw NotConnectedException(::rtl::OUString(), *this); 54 55 return m_aData.getLength() - m_nPos; 56 } 57 58 // com::sun::star::io::XInputStream 59 //------------------------------------------------------------------ 60 sal_Int32 SAL_CALL SequenceInputStream::readBytes( Sequence<sal_Int8>& aData, sal_Int32 nBytesToRead ) 61 { 62 ::osl::MutexGuard aGuard( m_aMutex ); 63 64 sal_Int32 nAvail = avail(); 65 66 if (nBytesToRead < 0) 67 throw BufferSizeExceededException(::rtl::OUString(),*this); 68 69 if (nAvail < nBytesToRead) 70 nBytesToRead = nAvail; 71 72 aData.realloc(nBytesToRead); 73 memcpy(aData.getArray(), m_aData.getConstArray() + m_nPos, nBytesToRead); 74 m_nPos += nBytesToRead; 75 76 return nBytesToRead; 77 } 78 79 //------------------------------------------------------------------ 80 sal_Int32 SAL_CALL SequenceInputStream::readSomeBytes( Sequence<sal_Int8>& aData, sal_Int32 nMaxBytesToRead ) 81 { 82 // all data is available at once 83 return readBytes(aData, nMaxBytesToRead); 84 } 85 86 //------------------------------------------------------------------ 87 void SAL_CALL SequenceInputStream::skipBytes( sal_Int32 nBytesToSkip ) 88 { 89 ::osl::MutexGuard aGuard( m_aMutex ); 90 91 sal_Int32 nAvail = avail(); 92 93 if (nBytesToSkip < 0) 94 throw BufferSizeExceededException(::rtl::OUString(),*this); 95 96 if (nAvail < nBytesToSkip) 97 nBytesToSkip = nAvail; 98 99 m_nPos += nBytesToSkip; 100 } 101 102 //------------------------------------------------------------------ 103 sal_Int32 SAL_CALL SequenceInputStream::available( ) 104 { 105 ::osl::MutexGuard aGuard( m_aMutex ); 106 107 return avail(); 108 } 109 110 //------------------------------------------------------------------ 111 void SAL_CALL SequenceInputStream::closeInput( ) 112 { 113 if (m_nPos == -1) 114 throw NotConnectedException(::rtl::OUString(), *this); 115 116 m_nPos = -1; 117 } 118 119 void SAL_CALL SequenceInputStream::seek( sal_Int64 location ) 120 { 121 if ( location > m_aData.getLength() || location < 0 || location > SAL_MAX_INT32 ) 122 throw IllegalArgumentException(); 123 m_nPos = (sal_Int32) location; 124 } 125 126 sal_Int64 SAL_CALL SequenceInputStream::getPosition() 127 { 128 return m_nPos; 129 } 130 131 sal_Int64 SAL_CALL SequenceInputStream::getLength( ) 132 { 133 return m_aData.getLength(); 134 } 135 136 //-------------------------------------------------------------------------- 137 OSequenceOutputStream::OSequenceOutputStream(Sequence< sal_Int8 >& _rSeq, double _nResizeFactor, sal_Int32 _nMinimumResize, sal_Int32 _nMaximumResize) 138 :m_rSequence(_rSeq) 139 ,m_nResizeFactor(_nResizeFactor) 140 ,m_nMinimumResize(_nMinimumResize) 141 ,m_nMaximumResize(_nMaximumResize) 142 ,m_nSize(0) // starting at position 0 143 ,m_bConnected(sal_True) 144 { 145 OSL_ENSURE(m_nResizeFactor > 1, "OSequenceOutputStream::OSequenceOutputStream : invalid resize factor !"); 146 OSL_ENSURE((m_nMaximumResize < 0) || (m_nMaximumResize > m_nMinimumResize), 147 "OSequenceOutputStream::OSequenceOutputStream : these limits don't make any sense !"); 148 149 if (m_nResizeFactor <= 1) 150 m_nResizeFactor = 1.3; 151 if ((m_nMaximumResize >= 0) && (m_nMaximumResize <= m_nMinimumResize)) 152 m_nMaximumResize = m_nMinimumResize * 2; 153 // this heuristic is as good as any other ... supply better parameters if you don't like it :) 154 } 155 156 //-------------------------------------------------------------------------- 157 void SAL_CALL OSequenceOutputStream::writeBytes( const Sequence< sal_Int8 >& _rData ) 158 { 159 MutexGuard aGuard(m_aMutex); 160 if (!m_bConnected) 161 throw NotConnectedException(); 162 163 // ensure the sequence has enoungh space left 164 if (m_nSize + _rData.getLength() > m_rSequence.getLength()) 165 { 166 sal_Int32 nCurrentLength = m_rSequence.getLength(); 167 sal_Int32 nNewLength = static_cast< sal_Int32 >( 168 nCurrentLength * m_nResizeFactor); 169 170 if (m_nMinimumResize > nNewLength - nCurrentLength) 171 // we have a minimum so it's not too inefficient for small sequences and small write requests 172 nNewLength = nCurrentLength + m_nMinimumResize; 173 174 if ((m_nMaximumResize > 0) && (nNewLength - nCurrentLength > m_nMaximumResize)) 175 // such a large step is not allowed 176 nNewLength = nCurrentLength + m_nMaximumResize; 177 178 if (nNewLength < m_nSize + _rData.getLength()) 179 { // it's not enough .... the data would not fit 180 181 // let's take the double amount of the length of the data to be written, as the next write 182 // request could be as large as this one 183 sal_Int32 nNewGrowth = _rData.getLength() * 2; 184 if ((m_nMaximumResize > 0) && (nNewGrowth > m_nMaximumResize)) 185 { // we came to the limit, again ... 186 nNewGrowth = m_nMaximumResize; 187 if (nNewGrowth + nCurrentLength < m_nSize + _rData.getLength()) 188 // but it would not fit if we respect the limit 189 nNewGrowth = m_nSize + _rData.getLength() - nCurrentLength; 190 } 191 nNewLength = nCurrentLength + nNewGrowth; 192 } 193 194 // round it off to the next multiple of 4 ... 195 nNewLength = (nNewLength + 3) / 4 * 4; 196 197 m_rSequence.realloc(nNewLength); 198 } 199 200 OSL_ENSURE(m_rSequence.getLength() >= m_nSize + _rData.getLength(), 201 "ooops ... the realloc algorithm seems to be wrong :( !"); 202 203 memcpy(m_rSequence.getArray() + m_nSize, _rData.getConstArray(), _rData.getLength()); 204 m_nSize += _rData.getLength(); 205 } 206 207 //-------------------------------------------------------------------------- 208 void SAL_CALL OSequenceOutputStream::flush( ) 209 { 210 MutexGuard aGuard(m_aMutex); 211 if (!m_bConnected) 212 throw NotConnectedException(); 213 214 // cut the sequence to the real size 215 m_rSequence.realloc(m_nSize); 216 } 217 218 //-------------------------------------------------------------------------- 219 void SAL_CALL OSequenceOutputStream::closeOutput( ) 220 { 221 MutexGuard aGuard(m_aMutex); 222 if (!m_bConnected) 223 throw NotConnectedException(); 224 225 // cut the sequence to the real size 226 m_rSequence.realloc(m_nSize); 227 // and don't allow any further accesses 228 m_bConnected = sal_False; 229 } 230 231 } // namespace comphelper 232