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 throw(NotConnectedException, BufferSizeExceededException, 62 IOException, RuntimeException) 63 { 64 ::osl::MutexGuard aGuard( m_aMutex ); 65 66 sal_Int32 nAvail = avail(); 67 68 if (nBytesToRead < 0) 69 throw BufferSizeExceededException(::rtl::OUString(),*this); 70 71 if (nAvail < nBytesToRead) 72 nBytesToRead = nAvail; 73 74 aData.realloc(nBytesToRead); 75 memcpy(aData.getArray(), m_aData.getConstArray() + m_nPos, nBytesToRead); 76 m_nPos += nBytesToRead; 77 78 return nBytesToRead; 79 } 80 81 //------------------------------------------------------------------ 82 sal_Int32 SAL_CALL SequenceInputStream::readSomeBytes( Sequence<sal_Int8>& aData, sal_Int32 nMaxBytesToRead ) 83 throw(NotConnectedException, BufferSizeExceededException, 84 IOException, RuntimeException) 85 { 86 // all data is available at once 87 return readBytes(aData, nMaxBytesToRead); 88 } 89 90 //------------------------------------------------------------------ 91 void SAL_CALL SequenceInputStream::skipBytes( sal_Int32 nBytesToSkip ) 92 throw(NotConnectedException, BufferSizeExceededException, 93 IOException, RuntimeException) 94 { 95 ::osl::MutexGuard aGuard( m_aMutex ); 96 97 sal_Int32 nAvail = avail(); 98 99 if (nBytesToSkip < 0) 100 throw BufferSizeExceededException(::rtl::OUString(),*this); 101 102 if (nAvail < nBytesToSkip) 103 nBytesToSkip = nAvail; 104 105 m_nPos += nBytesToSkip; 106 } 107 108 //------------------------------------------------------------------ 109 sal_Int32 SAL_CALL SequenceInputStream::available( ) 110 throw(NotConnectedException, IOException, RuntimeException) 111 { 112 ::osl::MutexGuard aGuard( m_aMutex ); 113 114 return avail(); 115 } 116 117 //------------------------------------------------------------------ 118 void SAL_CALL SequenceInputStream::closeInput( ) 119 throw(NotConnectedException, IOException, RuntimeException) 120 { 121 if (m_nPos == -1) 122 throw NotConnectedException(::rtl::OUString(), *this); 123 124 m_nPos = -1; 125 } 126 127 void SAL_CALL SequenceInputStream::seek( sal_Int64 location ) throw (IllegalArgumentException, IOException, RuntimeException) 128 { 129 if ( location > m_aData.getLength() || location < 0 || location > SAL_MAX_INT32 ) 130 throw IllegalArgumentException(); 131 m_nPos = (sal_Int32) location; 132 } 133 134 sal_Int64 SAL_CALL SequenceInputStream::getPosition() throw (IOException, RuntimeException) 135 { 136 return m_nPos; 137 } 138 139 sal_Int64 SAL_CALL SequenceInputStream::getLength( ) throw (IOException, RuntimeException) 140 { 141 return m_aData.getLength(); 142 } 143 144 //-------------------------------------------------------------------------- 145 OSequenceOutputStream::OSequenceOutputStream(Sequence< sal_Int8 >& _rSeq, double _nResizeFactor, sal_Int32 _nMinimumResize, sal_Int32 _nMaximumResize) 146 :m_rSequence(_rSeq) 147 ,m_nResizeFactor(_nResizeFactor) 148 ,m_nMinimumResize(_nMinimumResize) 149 ,m_nMaximumResize(_nMaximumResize) 150 ,m_nSize(0) // starting at position 0 151 ,m_bConnected(sal_True) 152 { 153 OSL_ENSURE(m_nResizeFactor > 1, "OSequenceOutputStream::OSequenceOutputStream : invalid resize factor !"); 154 OSL_ENSURE((m_nMaximumResize < 0) || (m_nMaximumResize > m_nMinimumResize), 155 "OSequenceOutputStream::OSequenceOutputStream : these limits don't make any sense !"); 156 157 if (m_nResizeFactor <= 1) 158 m_nResizeFactor = 1.3; 159 if ((m_nMaximumResize >= 0) && (m_nMaximumResize <= m_nMinimumResize)) 160 m_nMaximumResize = m_nMinimumResize * 2; 161 // this heuristic is as good as any other ... supply better parameters if you don't like it :) 162 } 163 164 //-------------------------------------------------------------------------- 165 void SAL_CALL OSequenceOutputStream::writeBytes( const Sequence< sal_Int8 >& _rData ) throw(NotConnectedException, BufferSizeExceededException, IOException, RuntimeException) 166 { 167 MutexGuard aGuard(m_aMutex); 168 if (!m_bConnected) 169 throw NotConnectedException(); 170 171 // ensure the sequence has enoungh space left 172 if (m_nSize + _rData.getLength() > m_rSequence.getLength()) 173 { 174 sal_Int32 nCurrentLength = m_rSequence.getLength(); 175 sal_Int32 nNewLength = static_cast< sal_Int32 >( 176 nCurrentLength * m_nResizeFactor); 177 178 if (m_nMinimumResize > nNewLength - nCurrentLength) 179 // we have a minimum so it's not too inefficient for small sequences and small write requests 180 nNewLength = nCurrentLength + m_nMinimumResize; 181 182 if ((m_nMaximumResize > 0) && (nNewLength - nCurrentLength > m_nMaximumResize)) 183 // such a large step is not allowed 184 nNewLength = nCurrentLength + m_nMaximumResize; 185 186 if (nNewLength < m_nSize + _rData.getLength()) 187 { // it's not enough .... the data would not fit 188 189 // let's take the double amount of the length of the data to be written, as the next write 190 // request could be as large as this one 191 sal_Int32 nNewGrowth = _rData.getLength() * 2; 192 if ((m_nMaximumResize > 0) && (nNewGrowth > m_nMaximumResize)) 193 { // we came to the limit, again ... 194 nNewGrowth = m_nMaximumResize; 195 if (nNewGrowth + nCurrentLength < m_nSize + _rData.getLength()) 196 // but it would not fit if we respect the limit 197 nNewGrowth = m_nSize + _rData.getLength() - nCurrentLength; 198 } 199 nNewLength = nCurrentLength + nNewGrowth; 200 } 201 202 // round it off to the next multiple of 4 ... 203 nNewLength = (nNewLength + 3) / 4 * 4; 204 205 m_rSequence.realloc(nNewLength); 206 } 207 208 OSL_ENSURE(m_rSequence.getLength() >= m_nSize + _rData.getLength(), 209 "ooops ... the realloc algorithm seems to be wrong :( !"); 210 211 memcpy(m_rSequence.getArray() + m_nSize, _rData.getConstArray(), _rData.getLength()); 212 m_nSize += _rData.getLength(); 213 } 214 215 //-------------------------------------------------------------------------- 216 void SAL_CALL OSequenceOutputStream::flush( ) throw(NotConnectedException, BufferSizeExceededException, IOException, RuntimeException) 217 { 218 MutexGuard aGuard(m_aMutex); 219 if (!m_bConnected) 220 throw NotConnectedException(); 221 222 // cut the sequence to the real size 223 m_rSequence.realloc(m_nSize); 224 } 225 226 //-------------------------------------------------------------------------- 227 void SAL_CALL OSequenceOutputStream::closeOutput( ) throw(NotConnectedException, BufferSizeExceededException, IOException, RuntimeException) 228 { 229 MutexGuard aGuard(m_aMutex); 230 if (!m_bConnected) 231 throw NotConnectedException(); 232 233 // cut the sequence to the real size 234 m_rSequence.realloc(m_nSize); 235 // and don't allow any further accesses 236 m_bConnected = sal_False; 237 } 238 239 } // namespace comphelper 240