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

Generated by: LCOV version 1.10