LCOV - code coverage report
Current view: top level - comphelper/source/streaming - seqstream.cxx (source / functions) Hit Total Coverage
Test: commit e02a6cb2c3e2b23b203b422e4e0680877f232636 Lines: 0 89 0.0 %
Date: 2014-04-14 Functions: 0 14 0.0 %
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           0 : SequenceInputStream::SequenceInputStream(
      37             :     css::uno::Sequence<sal_Int8> const & rData)
      38             : :   m_aData(rData)
      39           0 : ,   m_nPos(0)
      40             : {
      41           0 : }
      42             : 
      43             : // checks if closed, returns available size, not mutex-protected
      44             : 
      45           0 : inline sal_Int32 SequenceInputStream::avail()
      46             : {
      47           0 :     if (m_nPos == -1)
      48           0 :         throw NotConnectedException(OUString(), *this);
      49             : 
      50           0 :     return m_aData.getLength() - m_nPos;
      51             : }
      52             : 
      53             : // com::sun::star::io::XInputStream
      54             : 
      55           0 : sal_Int32 SAL_CALL SequenceInputStream::readBytes( Sequence<sal_Int8>& aData, sal_Int32 nBytesToRead )
      56             :     throw(NotConnectedException, BufferSizeExceededException,
      57             :           IOException, RuntimeException, std::exception)
      58             : {
      59           0 :     ::osl::MutexGuard aGuard( m_aMutex );
      60             : 
      61           0 :     sal_Int32 nAvail = avail();
      62             : 
      63           0 :     if (nBytesToRead < 0)
      64           0 :         throw BufferSizeExceededException(OUString(),*this);
      65             : 
      66           0 :     if (nAvail < nBytesToRead)
      67           0 :         nBytesToRead = nAvail;
      68             : 
      69           0 :     aData.realloc(nBytesToRead);
      70           0 :     memcpy(aData.getArray(), m_aData.getConstArray() + m_nPos, nBytesToRead);
      71           0 :     m_nPos += nBytesToRead;
      72             : 
      73           0 :     return nBytesToRead;
      74             : }
      75             : 
      76             : 
      77           0 : 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           0 :     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           0 : void SAL_CALL SequenceInputStream::closeInput(  )
     114             :     throw(NotConnectedException, IOException, RuntimeException, std::exception)
     115             : {
     116           0 :     if (m_nPos == -1)
     117           0 :         throw NotConnectedException(OUString(), *this);
     118             : 
     119           0 :     m_nPos = -1;
     120           0 : }
     121             : 
     122           0 : void SAL_CALL SequenceInputStream::seek( sal_Int64 location ) throw (IllegalArgumentException, IOException, RuntimeException, std::exception)
     123             : {
     124           0 :     if ( location > m_aData.getLength() || location < 0 || location > SAL_MAX_INT32 )
     125           0 :         throw IllegalArgumentException();
     126           0 :     m_nPos = (sal_Int32) location;
     127           0 : }
     128             : 
     129           0 : sal_Int64 SAL_CALL SequenceInputStream::getPosition() throw (IOException, RuntimeException, std::exception)
     130             : {
     131           0 :     return m_nPos;
     132             : }
     133             : 
     134           0 : sal_Int64 SAL_CALL SequenceInputStream::getLength(  ) throw (IOException, RuntimeException, std::exception)
     135             : {
     136           0 :     return m_aData.getLength();
     137             : }
     138             : 
     139             : 
     140           0 : 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           0 :     ,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           0 :     if (m_nResizeFactor <= 1)
     153           0 :         m_nResizeFactor = 1.3;
     154           0 :     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           0 : }
     158             : 
     159             : 
     160           0 : void SAL_CALL OSequenceOutputStream::writeBytes( const Sequence< sal_Int8 >& _rData ) throw(NotConnectedException, BufferSizeExceededException, IOException, RuntimeException, std::exception)
     161             : {
     162           0 :     MutexGuard aGuard(m_aMutex);
     163           0 :     if (!m_bConnected)
     164           0 :         throw NotConnectedException();
     165             : 
     166             :     // ensure the sequence has enough space left
     167           0 :     if (m_nSize + _rData.getLength() > m_rSequence.getLength())
     168             :     {
     169           0 :         sal_Int32 nCurrentLength = m_rSequence.getLength();
     170             :         sal_Int32 nNewLength = static_cast< sal_Int32 >(
     171           0 :             nCurrentLength * m_nResizeFactor);
     172             : 
     173           0 :         if (m_nMinimumResize > nNewLength - nCurrentLength)
     174             :             // we have a minimum so it's not too inefficient for small sequences and small write requests
     175           0 :             nNewLength = nCurrentLength + m_nMinimumResize;
     176             : 
     177           0 :         if ((m_nMaximumResize > 0) && (nNewLength - nCurrentLength > m_nMaximumResize))
     178             :             // such a large step is not allowed
     179           0 :             nNewLength = nCurrentLength + m_nMaximumResize;
     180             : 
     181           0 :         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           0 :         nNewLength = (nNewLength + 3) / 4 * 4;
     199             : 
     200           0 :         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           0 :     memcpy(m_rSequence.getArray() + m_nSize, _rData.getConstArray(), _rData.getLength());
     207           0 :     m_nSize += _rData.getLength();
     208           0 : }
     209             : 
     210             : 
     211           0 : void SAL_CALL OSequenceOutputStream::flush(  ) throw(NotConnectedException, BufferSizeExceededException, IOException, RuntimeException, std::exception)
     212             : {
     213           0 :     MutexGuard aGuard(m_aMutex);
     214           0 :     if (!m_bConnected)
     215           0 :         throw NotConnectedException();
     216             : 
     217             :     // cut the sequence to the real size
     218           0 :     m_rSequence.realloc(m_nSize);
     219           0 : }
     220             : 
     221             : 
     222           0 : void SAL_CALL OSequenceOutputStream::closeOutput(  ) throw(NotConnectedException, BufferSizeExceededException, IOException, RuntimeException, std::exception)
     223             : {
     224           0 :     MutexGuard aGuard(m_aMutex);
     225           0 :     if (!m_bConnected)
     226           0 :         throw NotConnectedException();
     227             : 
     228             :     // cut the sequence to the real size
     229           0 :     m_rSequence.realloc(m_nSize);
     230             :     // and don't allow any further accesses
     231           0 :     m_bConnected = false;
     232           0 : }
     233             : 
     234             : } // namespace comphelper
     235             : 
     236             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10