LCOV - code coverage report
Current view: top level - package/source/zipapi - ByteGrabber.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 56 83 67.5 %
Date: 2014-04-11 Functions: 10 13 76.9 %
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 <ByteGrabber.hxx>
      21             : #include <com/sun/star/io/XSeekable.hpp>
      22             : #include <com/sun/star/io/XInputStream.hpp>
      23             : 
      24             : using namespace ::com::sun::star;
      25             : 
      26             : #if OSL_DEBUG_LEVEL > 0
      27             : #define THROW_WHERE SAL_WHERE
      28             : #else
      29             : #define THROW_WHERE ""
      30             : #endif
      31             : 
      32             : /** ByteGrabber implements the >> operators on an XOutputStream. This is
      33             :  *  potentially quite slow and may need to be optimised
      34             :  */
      35             : 
      36        6188 : ByteGrabber::ByteGrabber(uno::Reference  < io::XInputStream > xIstream)
      37             : : xStream(xIstream)
      38             : , xSeek (xIstream, uno::UNO_QUERY )
      39        6188 : , aSequence ( 4 )
      40             : {
      41        6188 :     pSequence = aSequence.getArray();
      42        6188 : }
      43             : 
      44        6173 : ByteGrabber::~ByteGrabber()
      45             : {
      46        6173 : }
      47             : 
      48          27 : void ByteGrabber::setInputStream (uno::Reference < io::XInputStream > xNewStream)
      49             : {
      50          27 :     ::osl::MutexGuard aGuard( m_aMutex );
      51          27 :     xStream = xNewStream;
      52          27 :     xSeek = uno::Reference < io::XSeekable > (xNewStream, uno::UNO_QUERY);
      53          27 : }
      54             : 
      55             : // XInputStream chained
      56       39294 : sal_Int32 SAL_CALL ByteGrabber::readBytes( uno::Sequence< sal_Int8 >& aData,
      57             :                                         sal_Int32 nBytesToRead )
      58             :     throw(io::NotConnectedException, io::BufferSizeExceededException, io::IOException, uno::RuntimeException)
      59             : {
      60       39294 :     ::osl::MutexGuard aGuard( m_aMutex );
      61       39294 :     return xStream->readBytes(aData, nBytesToRead );
      62             : }
      63             : 
      64             : // XSeekable chained...
      65       45013 : sal_Int64 SAL_CALL ByteGrabber::seek( sal_Int64 location )
      66             :     throw(lang::IllegalArgumentException, io::IOException, uno::RuntimeException)
      67             : {
      68       45013 :     ::osl::MutexGuard aGuard( m_aMutex );
      69       45013 :     if (xSeek.is() )
      70             :     {
      71       45013 :         sal_Int64 nLen = xSeek->getLength();
      72       45013 :         if ( location < 0 || location > nLen )
      73           0 :             throw lang::IllegalArgumentException(THROW_WHERE, uno::Reference< uno::XInterface >(), 1 );
      74       45013 :         if (location > nLen )
      75           0 :             location = nLen;
      76       45013 :         xSeek->seek( location );
      77       90026 :         return location;
      78             :     }
      79             :     else
      80       45013 :         throw io::IOException(THROW_WHERE, uno::Reference< uno::XInterface >() );
      81             : }
      82             : 
      83       27843 : sal_Int64 SAL_CALL ByteGrabber::getPosition(  )
      84             :         throw(io::IOException, uno::RuntimeException)
      85             : {
      86       27843 :     ::osl::MutexGuard aGuard( m_aMutex );
      87       27843 :     if (xSeek.is() )
      88       55686 :         return xSeek->getPosition();
      89             :     else
      90       27843 :         throw io::IOException(THROW_WHERE, uno::Reference< uno::XInterface >() );
      91             : }
      92             : 
      93        5733 : sal_Int64 SAL_CALL ByteGrabber::getLength(  )
      94             :         throw(io::IOException, uno::RuntimeException)
      95             : {
      96        5733 :     ::osl::MutexGuard aGuard( m_aMutex );
      97        5733 :     if (xSeek.is() )
      98       11466 :         return xSeek->getLength();
      99             :     else
     100        5733 :         throw io::IOException(THROW_WHERE, uno::Reference< uno::XInterface >() );
     101             : }
     102             : 
     103           0 : ByteGrabber& ByteGrabber::operator >> (sal_Int8& rInt8)
     104             : {
     105           0 :     ::osl::MutexGuard aGuard( m_aMutex );
     106           0 :     if (xStream->readBytes(aSequence,1) != 1)
     107           0 :         rInt8 = 0;
     108             :     else
     109           0 :         rInt8 = aSequence[0] & 0xFF;
     110           0 :     return *this;
     111             : }
     112             : 
     113      139215 : ByteGrabber& ByteGrabber::operator >> (sal_Int16& rInt16)
     114             : {
     115      139215 :     ::osl::MutexGuard aGuard( m_aMutex );
     116      139215 :     if (xStream->readBytes ( aSequence, 2) != 2)
     117           0 :         rInt16 = 0;
     118             :     else
     119             :     {
     120      139215 :         pSequence = aSequence.getConstArray();
     121             :         rInt16 = static_cast <sal_Int16>
     122      139215 :                ( (pSequence[0] & 0xFF)
     123      139215 :               | (pSequence[1] & 0xFF) << 8);
     124             :     }
     125      139215 :     return *this;
     126             : }
     127             : 
     128      150653 : ByteGrabber& ByteGrabber::operator >> (sal_Int32& rInt32)
     129             : {
     130      150653 :     ::osl::MutexGuard aGuard( m_aMutex );
     131             : 
     132      150653 :     if (xStream->readBytes(aSequence, 4) != 4)
     133           0 :         rInt32 = 0;
     134             :     else
     135             :     {
     136      150653 :         pSequence = aSequence.getConstArray();
     137             :         rInt32 = static_cast < sal_Int32 >
     138      150653 :                 ( (pSequence[0] & 0xFF)
     139      150653 :               | ( pSequence[1] & 0xFF ) << 8
     140      150653 :               | ( pSequence[2] & 0xFF ) << 16
     141      150653 :               | ( pSequence[3] & 0xFF ) << 24 );
     142             :     }
     143      150653 :     return *this;
     144             : }
     145             : 
     146           0 : ByteGrabber& ByteGrabber::operator >> (sal_uInt8& rInt8)
     147             : {
     148           0 :     ::osl::MutexGuard aGuard( m_aMutex );
     149             : 
     150           0 :     if (xStream->readBytes(aSequence,1) != 1)
     151           0 :         rInt8 = 0;
     152             :     else
     153           0 :         rInt8 = static_cast < sal_uInt8 > (aSequence[0] & 0xFF );
     154           0 :     return *this;
     155             : }
     156        5719 : ByteGrabber& ByteGrabber::operator >> (sal_uInt16& rInt16)
     157             : {
     158        5719 :     ::osl::MutexGuard aGuard( m_aMutex );
     159             : 
     160        5719 :     if (xStream->readBytes(aSequence, 2) != 2)
     161           0 :         rInt16 = 0;
     162             :     else
     163             :     {
     164        5719 :         pSequence = aSequence.getConstArray();
     165             :         rInt16 = static_cast <sal_uInt16>
     166        5719 :                ( (pSequence[0] & 0xFF)
     167        5719 :               | (pSequence[1] & 0xFF) << 8);
     168             :     }
     169        5719 :     return *this;
     170             : }
     171           0 : ByteGrabber& ByteGrabber::operator >> (sal_uInt32& ruInt32)
     172             : {
     173           0 :     ::osl::MutexGuard aGuard( m_aMutex );
     174             : 
     175           0 :     if (xStream->readBytes(aSequence, 4) != 4)
     176           0 :         ruInt32 = 0;
     177             :     else
     178             :     {
     179           0 :         pSequence = aSequence.getConstArray();
     180             :         ruInt32 = static_cast < sal_uInt32 >
     181           0 :                 ( (pSequence[0] & 0xFF)
     182           0 :               | ( pSequence[1] & 0xFF ) << 8
     183           0 :               | ( pSequence[2] & 0xFF ) << 16
     184           0 :               | ( pSequence[3] & 0xFF ) << 24 );
     185             :     }
     186           0 :     return *this;
     187             : }
     188             : 
     189             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10