LCOV - code coverage report
Current view: top level - comphelper/source/streaming - seekableinput.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 57 95 60.0 %
Date: 2014-11-03 Functions: 10 14 71.4 %
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 <com/sun/star/io/TempFile.hpp>
      21             : #include <com/sun/star/io/XOutputStream.hpp>
      22             : 
      23             : 
      24             : #include <comphelper/processfactory.hxx>
      25             : #include <comphelper/seekableinput.hxx>
      26             : 
      27             : using namespace ::com::sun::star;
      28             : 
      29             : namespace comphelper
      30             : {
      31             : 
      32             : const sal_Int32 nConstBufferSize = 32000;
      33             : 
      34             : 
      35          54 : void copyInputToOutput_Impl( const uno::Reference< io::XInputStream >& xIn,
      36             :                             const uno::Reference< io::XOutputStream >& xOut )
      37             : {
      38             :     sal_Int32 nRead;
      39          54 :     uno::Sequence< sal_Int8 > aSequence( nConstBufferSize );
      40             : 
      41         316 :     do
      42             :     {
      43         316 :         nRead = xIn->readBytes( aSequence, nConstBufferSize );
      44         316 :         if ( nRead < nConstBufferSize )
      45             :         {
      46          54 :             uno::Sequence< sal_Int8 > aTempBuf( aSequence.getConstArray(), nRead );
      47          54 :             xOut->writeBytes( aTempBuf );
      48             :         }
      49             :         else
      50         262 :             xOut->writeBytes( aSequence );
      51             :     }
      52          54 :     while ( nRead == nConstBufferSize );
      53          54 : }
      54             : 
      55             : 
      56          54 : OSeekableInputWrapper::OSeekableInputWrapper(
      57             :             const uno::Reference< io::XInputStream >& xInStream,
      58             :             const uno::Reference< uno::XComponentContext >& rxContext )
      59             : : m_xContext( rxContext )
      60          54 : , m_xOriginalStream( xInStream )
      61             : {
      62          54 :     if ( !m_xContext.is() )
      63           0 :         throw uno::RuntimeException();
      64          54 : }
      65             : 
      66             : 
      67         108 : OSeekableInputWrapper::~OSeekableInputWrapper()
      68             : {
      69         108 : }
      70             : 
      71             : 
      72       44860 : uno::Reference< io::XInputStream > OSeekableInputWrapper::CheckSeekableCanWrap(
      73             :                             const uno::Reference< io::XInputStream >& xInStream,
      74             :                             const uno::Reference< uno::XComponentContext >& rxContext )
      75             : {
      76             :     // check that the stream is seekable and just wrap it if it is not
      77       44860 :     uno::Reference< io::XSeekable > xSeek( xInStream, uno::UNO_QUERY );
      78       44860 :     if ( xSeek.is() )
      79       44860 :         return xInStream;
      80             : 
      81             :     uno::Reference< io::XInputStream > xNewStream(
      82             :             static_cast< io::XInputStream* >(
      83           0 :                 new OSeekableInputWrapper( xInStream, rxContext ) ) );
      84       44860 :     return xNewStream;
      85             : }
      86             : 
      87             : 
      88         980 : void OSeekableInputWrapper::PrepareCopy_Impl()
      89             : {
      90         980 :     if ( !m_xCopyInput.is() )
      91             :     {
      92          54 :         if ( !m_xContext.is() )
      93           0 :             throw uno::RuntimeException();
      94             : 
      95             :         uno::Reference< io::XOutputStream > xTempOut(
      96             :                 io::TempFile::create(m_xContext),
      97          54 :                 uno::UNO_QUERY_THROW );
      98             : 
      99          54 :         copyInputToOutput_Impl( m_xOriginalStream, xTempOut );
     100          54 :         xTempOut->closeOutput();
     101             : 
     102         108 :         uno::Reference< io::XSeekable > xTempSeek( xTempOut, uno::UNO_QUERY );
     103          54 :         if ( xTempSeek.is() )
     104             :         {
     105          54 :             xTempSeek->seek( 0 );
     106          54 :             m_xCopyInput = uno::Reference< io::XInputStream >( xTempOut, uno::UNO_QUERY );
     107          54 :             if ( m_xCopyInput.is() )
     108          54 :                 m_xCopySeek = xTempSeek;
     109          54 :         }
     110             :     }
     111             : 
     112         980 :     if ( !m_xCopyInput.is() )
     113           0 :         throw io::IOException();
     114         980 : }
     115             : 
     116             : // XInputStream
     117             : 
     118           0 : sal_Int32 SAL_CALL OSeekableInputWrapper::readBytes( uno::Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead )
     119             :     throw ( io::NotConnectedException,
     120             :             io::BufferSizeExceededException,
     121             :             io::IOException,
     122             :             uno::RuntimeException, std::exception )
     123             : {
     124           0 :     ::osl::MutexGuard aGuard( m_aMutex );
     125             : 
     126           0 :     if ( !m_xOriginalStream.is() )
     127           0 :         throw io::NotConnectedException();
     128             : 
     129           0 :     PrepareCopy_Impl();
     130             : 
     131           0 :     return m_xCopyInput->readBytes( aData, nBytesToRead );
     132             : }
     133             : 
     134             : 
     135          92 : sal_Int32 SAL_CALL OSeekableInputWrapper::readSomeBytes( uno::Sequence< sal_Int8 >& aData, sal_Int32 nMaxBytesToRead )
     136             :     throw ( io::NotConnectedException,
     137             :             io::BufferSizeExceededException,
     138             :             io::IOException,
     139             :             uno::RuntimeException, std::exception )
     140             : {
     141          92 :     ::osl::MutexGuard aGuard( m_aMutex );
     142             : 
     143          92 :     if ( !m_xOriginalStream.is() )
     144           0 :         throw io::NotConnectedException();
     145             : 
     146          92 :     PrepareCopy_Impl();
     147             : 
     148          92 :     return m_xCopyInput->readSomeBytes( aData, nMaxBytesToRead );
     149             : }
     150             : 
     151             : 
     152           0 : void SAL_CALL OSeekableInputWrapper::skipBytes( sal_Int32 nBytesToSkip )
     153             :     throw ( io::NotConnectedException,
     154             :             io::BufferSizeExceededException,
     155             :             io::IOException,
     156             :             uno::RuntimeException, std::exception )
     157             : {
     158           0 :     ::osl::MutexGuard aGuard( m_aMutex );
     159             : 
     160           0 :     if ( !m_xOriginalStream.is() )
     161           0 :         throw io::NotConnectedException();
     162             : 
     163           0 :     PrepareCopy_Impl();
     164             : 
     165           0 :     m_xCopyInput->skipBytes( nBytesToSkip );
     166           0 : }
     167             : 
     168             : 
     169           0 : sal_Int32 SAL_CALL OSeekableInputWrapper::available()
     170             :     throw ( io::NotConnectedException,
     171             :             io::IOException,
     172             :             uno::RuntimeException, std::exception )
     173             : {
     174           0 :     ::osl::MutexGuard aGuard( m_aMutex );
     175             : 
     176           0 :     if ( !m_xOriginalStream.is() )
     177           0 :         throw io::NotConnectedException();
     178             : 
     179           0 :     PrepareCopy_Impl();
     180             : 
     181           0 :     return m_xCopyInput->available();
     182             : }
     183             : 
     184             : 
     185           0 : void SAL_CALL OSeekableInputWrapper::closeInput()
     186             :     throw ( io::NotConnectedException,
     187             :             io::IOException,
     188             :             uno::RuntimeException, std::exception )
     189             : {
     190           0 :     ::osl::MutexGuard aGuard( m_aMutex );
     191             : 
     192           0 :     if ( !m_xOriginalStream.is() )
     193           0 :         throw io::NotConnectedException();
     194             : 
     195           0 :     m_xOriginalStream->closeInput();
     196           0 :     m_xOriginalStream = uno::Reference< io::XInputStream >();
     197             : 
     198           0 :     if ( m_xCopyInput.is() )
     199             :     {
     200           0 :         m_xCopyInput->closeInput();
     201           0 :         m_xCopyInput = uno::Reference< io::XInputStream >();
     202             :     }
     203             : 
     204           0 :     m_xCopySeek = uno::Reference< io::XSeekable >();
     205           0 : }
     206             : 
     207             : 
     208             : // XSeekable
     209             : 
     210         128 : void SAL_CALL OSeekableInputWrapper::seek( sal_Int64 location )
     211             :     throw ( lang::IllegalArgumentException,
     212             :             io::IOException,
     213             :             uno::RuntimeException, std::exception )
     214             : {
     215         128 :     ::osl::MutexGuard aGuard( m_aMutex );
     216             : 
     217         128 :     if ( !m_xOriginalStream.is() )
     218           0 :         throw io::NotConnectedException();
     219             : 
     220         128 :     PrepareCopy_Impl();
     221             : 
     222         128 :     m_xCopySeek->seek( location );
     223         128 : }
     224             : 
     225             : 
     226         694 : sal_Int64 SAL_CALL OSeekableInputWrapper::getPosition()
     227             :     throw ( io::IOException,
     228             :             uno::RuntimeException, std::exception )
     229             : {
     230         694 :     ::osl::MutexGuard aGuard( m_aMutex );
     231             : 
     232         694 :     if ( !m_xOriginalStream.is() )
     233           0 :         throw io::NotConnectedException();
     234             : 
     235         694 :     PrepareCopy_Impl();
     236             : 
     237         694 :     return m_xCopySeek->getPosition();
     238             : }
     239             : 
     240             : 
     241          66 : sal_Int64 SAL_CALL OSeekableInputWrapper::getLength()
     242             :     throw ( io::IOException,
     243             :             uno::RuntimeException, std::exception )
     244             : {
     245          66 :     ::osl::MutexGuard aGuard( m_aMutex );
     246             : 
     247          66 :     if ( !m_xOriginalStream.is() )
     248           0 :         throw io::NotConnectedException();
     249             : 
     250          66 :     PrepareCopy_Impl();
     251             : 
     252          66 :     return m_xCopySeek->getLength();
     253             : }
     254             : 
     255             : }   // namespace comphelper
     256             : 
     257             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10