LCOV - code coverage report
Current view: top level - comphelper/source/streaming - seqstream.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 57 89 64.0 %
Date: 2012-08-25 Functions: 10 14 71.4 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 36 120 30.0 %

           Branch data     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                 :       6908 : SequenceInputStream::SequenceInputStream(const ByteSequence& rData)
      37                 :            : :   m_aData(rData)
      38 [ +  - ][ +  - ]:       6908 : ,   m_nPos(0)
      39                 :            : {
      40                 :       6908 : }
      41                 :            : 
      42                 :            : // checks if closed, returns available size, not mutex-protected
      43                 :            : //------------------------------------------------------------------
      44                 :       9638 : inline sal_Int32 SequenceInputStream::avail()
      45                 :            : {
      46         [ -  + ]:       9638 :     if (m_nPos == -1)
      47 [ #  # ][ #  # ]:          0 :         throw NotConnectedException(::rtl::OUString(), *this);
      48                 :            : 
      49                 :       9638 :     return m_aData.getLength() - m_nPos;
      50                 :            : }
      51                 :            : 
      52                 :            : // com::sun::star::io::XInputStream
      53                 :            : //------------------------------------------------------------------
      54                 :       9638 : sal_Int32 SAL_CALL SequenceInputStream::readBytes( Sequence<sal_Int8>& aData, sal_Int32 nBytesToRead )
      55                 :            :     throw(NotConnectedException, BufferSizeExceededException,
      56                 :            :           IOException, RuntimeException)
      57                 :            : {
      58         [ +  - ]:       9638 :     ::osl::MutexGuard aGuard( m_aMutex );
      59                 :            : 
      60         [ +  - ]:       9638 :     sal_Int32 nAvail = avail();
      61                 :            : 
      62         [ -  + ]:       9638 :     if (nBytesToRead < 0)
      63 [ #  # ][ #  # ]:          0 :         throw BufferSizeExceededException(::rtl::OUString(),*this);
      64                 :            : 
      65         [ +  + ]:       9638 :     if (nAvail < nBytesToRead)
      66                 :       6996 :         nBytesToRead = nAvail;
      67                 :            : 
      68         [ +  - ]:       9638 :     aData.realloc(nBytesToRead);
      69         [ +  - ]:       9638 :     memcpy(aData.getArray(), m_aData.getConstArray() + m_nPos, nBytesToRead);
      70                 :       9638 :     m_nPos += nBytesToRead;
      71                 :            : 
      72         [ +  - ]:       9638 :     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                 :          2 : void SAL_CALL SequenceInputStream::closeInput(  )
     113                 :            :     throw(NotConnectedException, IOException, RuntimeException)
     114                 :            : {
     115         [ -  + ]:          2 :     if (m_nPos == -1)
     116 [ #  # ][ #  # ]:          0 :         throw NotConnectedException(::rtl::OUString(), *this);
     117                 :            : 
     118                 :          2 :     m_nPos = -1;
     119                 :          2 : }
     120                 :            : 
     121                 :       9147 : void SAL_CALL SequenceInputStream::seek( sal_Int64 location ) throw (IllegalArgumentException, IOException, RuntimeException)
     122                 :            : {
     123 [ +  - ][ +  - ]:       9147 :     if ( location > m_aData.getLength() || location < 0 || location > SAL_MAX_INT32 )
         [ -  + ][ -  + ]
     124         [ #  # ]:          0 :         throw IllegalArgumentException();
     125                 :       9147 :     m_nPos = (sal_Int32) location;
     126                 :       9147 : }
     127                 :            : 
     128                 :          0 : sal_Int64 SAL_CALL SequenceInputStream::getPosition() throw (IOException, RuntimeException)
     129                 :            : {
     130                 :          0 :     return m_nPos;
     131                 :            : }
     132                 :            : 
     133                 :       6904 : sal_Int64 SAL_CALL SequenceInputStream::getLength(  ) throw (IOException, RuntimeException)
     134                 :            : {
     135                 :       6904 :     return m_aData.getLength();
     136                 :            : }
     137                 :            : 
     138                 :            : //--------------------------------------------------------------------------
     139                 :          2 : 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         [ +  - ]:          2 :     ,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         [ -  + ]:          2 :     if (m_nResizeFactor <= 1)
     152                 :          0 :         m_nResizeFactor = 1.3;
     153 [ -  + ][ #  # ]:          2 :     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                 :          2 : }
     157                 :            : 
     158                 :            : //--------------------------------------------------------------------------
     159                 :          2 : void SAL_CALL OSequenceOutputStream::writeBytes( const Sequence< sal_Int8 >& _rData ) throw(NotConnectedException, BufferSizeExceededException, IOException, RuntimeException)
     160                 :            : {
     161         [ +  - ]:          2 :     MutexGuard aGuard(m_aMutex);
     162         [ -  + ]:          2 :     if (!m_bConnected)
     163         [ #  # ]:          0 :         throw NotConnectedException();
     164                 :            : 
     165                 :            :     // ensure the sequence has enoungh space left
     166         [ +  - ]:          2 :     if (m_nSize + _rData.getLength() > m_rSequence.getLength())
     167                 :            :     {
     168                 :          2 :         sal_Int32 nCurrentLength = m_rSequence.getLength();
     169                 :            :         sal_Int32 nNewLength = static_cast< sal_Int32 >(
     170                 :          2 :             nCurrentLength * m_nResizeFactor);
     171                 :            : 
     172         [ +  - ]:          2 :         if (m_nMinimumResize > nNewLength - nCurrentLength)
     173                 :            :             // we have a minimum so it's not too inefficient for small sequences and small write requests
     174                 :          2 :             nNewLength = nCurrentLength + m_nMinimumResize;
     175                 :            : 
     176 [ -  + ][ #  # ]:          2 :         if ((m_nMaximumResize > 0) && (nNewLength - nCurrentLength > m_nMaximumResize))
     177                 :            :             // such a large step is not allowed
     178                 :          0 :             nNewLength = nCurrentLength + m_nMaximumResize;
     179                 :            : 
     180         [ -  + ]:          2 :         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                 :          2 :         nNewLength = (nNewLength + 3) / 4 * 4;
     198                 :            : 
     199         [ +  - ]:          2 :         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         [ +  - ]:          2 :     memcpy(m_rSequence.getArray() + m_nSize, _rData.getConstArray(), _rData.getLength());
     206         [ +  - ]:          2 :     m_nSize += _rData.getLength();
     207                 :          2 : }
     208                 :            : 
     209                 :            : //--------------------------------------------------------------------------
     210                 :          2 : void SAL_CALL OSequenceOutputStream::flush(  ) throw(NotConnectedException, BufferSizeExceededException, IOException, RuntimeException)
     211                 :            : {
     212         [ +  - ]:          2 :     MutexGuard aGuard(m_aMutex);
     213         [ -  + ]:          2 :     if (!m_bConnected)
     214         [ #  # ]:          0 :         throw NotConnectedException();
     215                 :            : 
     216                 :            :     // cut the sequence to the real size
     217 [ +  - ][ +  - ]:          2 :     m_rSequence.realloc(m_nSize);
     218                 :          2 : }
     219                 :            : 
     220                 :            : //--------------------------------------------------------------------------
     221                 :          2 : void SAL_CALL OSequenceOutputStream::closeOutput(  ) throw(NotConnectedException, BufferSizeExceededException, IOException, RuntimeException)
     222                 :            : {
     223         [ +  - ]:          2 :     MutexGuard aGuard(m_aMutex);
     224         [ -  + ]:          2 :     if (!m_bConnected)
     225         [ #  # ]:          0 :         throw NotConnectedException();
     226                 :            : 
     227                 :            :     // cut the sequence to the real size
     228         [ +  - ]:          2 :     m_rSequence.realloc(m_nSize);
     229                 :            :     // and don't allow any further accesses
     230         [ +  - ]:          2 :     m_bConnected = sal_False;
     231                 :          2 : }
     232                 :            : 
     233                 :            : } // namespace comphelper
     234                 :            : 
     235                 :            : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10