LCOV - code coverage report
Current view: top level - comphelper/source/streaming - seqstream.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 62 89 69.7 %
Date: 2015-06-13 12:38:46 Functions: 12 14 85.7 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.11