LCOV - code coverage report
Current view: top level - comphelper/source/streaming - memorystream.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 68 78 87.2 %
Date: 2014-04-11 Functions: 20 21 95.2 %
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             : 
      21             : #include "comphelper_module.hxx"
      22             : #include "comphelper_services.hxx"
      23             : 
      24             : #include <com/sun/star/io/XStream.hpp>
      25             : #include <com/sun/star/io/XSeekableInputStream.hpp>
      26             : #include <com/sun/star/io/XTruncate.hpp>
      27             : #include <com/sun/star/uno/XComponentContext.hpp>
      28             : #include <cppuhelper/implbase4.hxx>
      29             : 
      30             : #include <string.h>
      31             : #include <vector>
      32             : 
      33             : using ::cppu::OWeakObject;
      34             : using ::cppu::WeakImplHelper4;
      35             : using namespace ::com::sun::star::io;
      36             : using namespace ::com::sun::star::uno;
      37             : using namespace ::com::sun::star::lang;
      38             : using namespace ::osl;
      39             : 
      40             : namespace comphelper
      41             : {
      42             : 
      43             : class UNOMemoryStream : public WeakImplHelper4 < XStream, XSeekableInputStream, XOutputStream, XTruncate >
      44             : {
      45             : public:
      46             :     UNOMemoryStream();
      47             :     virtual ~UNOMemoryStream();
      48             : 
      49             :     // XStream
      50             :     virtual Reference< XInputStream > SAL_CALL getInputStream(  ) throw (RuntimeException, std::exception) SAL_OVERRIDE;
      51             :     virtual Reference< XOutputStream > SAL_CALL getOutputStream(  ) throw (RuntimeException, std::exception) SAL_OVERRIDE;
      52             : 
      53             :     // XInputStream
      54             :     virtual sal_Int32 SAL_CALL readBytes( Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead ) throw (NotConnectedException, BufferSizeExceededException, IOException, RuntimeException, std::exception) SAL_OVERRIDE;
      55             :     virtual sal_Int32 SAL_CALL readSomeBytes( Sequence< sal_Int8 >& aData, sal_Int32 nMaxBytesToRead ) throw (NotConnectedException, BufferSizeExceededException, IOException, RuntimeException, std::exception) SAL_OVERRIDE;
      56             :     virtual void SAL_CALL skipBytes( sal_Int32 nBytesToSkip ) throw (NotConnectedException, BufferSizeExceededException, IOException, RuntimeException, std::exception) SAL_OVERRIDE;
      57             :     virtual sal_Int32 SAL_CALL available() throw (NotConnectedException, IOException, RuntimeException, std::exception) SAL_OVERRIDE;
      58             :     virtual void SAL_CALL closeInput() throw (NotConnectedException, IOException, RuntimeException, std::exception) SAL_OVERRIDE;
      59             : 
      60             :     // XSeekable
      61             :     virtual void SAL_CALL seek( sal_Int64 location ) throw (IllegalArgumentException, IOException, RuntimeException, std::exception) SAL_OVERRIDE;
      62             :     virtual sal_Int64 SAL_CALL getPosition() throw (IOException, RuntimeException, std::exception) SAL_OVERRIDE;
      63             :     virtual sal_Int64 SAL_CALL getLength() throw (IOException, RuntimeException, std::exception) SAL_OVERRIDE;
      64             : 
      65             :     // XOutputStream
      66             :     virtual void SAL_CALL writeBytes( const Sequence< sal_Int8 >& aData ) throw (NotConnectedException, BufferSizeExceededException, IOException, RuntimeException, std::exception) SAL_OVERRIDE;
      67             :     virtual void SAL_CALL flush() throw (NotConnectedException, BufferSizeExceededException, IOException, RuntimeException, std::exception) SAL_OVERRIDE;
      68             :     virtual void SAL_CALL closeOutput() throw (NotConnectedException, BufferSizeExceededException, IOException, RuntimeException, std::exception) SAL_OVERRIDE;
      69             : 
      70             :     // XTruncate
      71             :     virtual void SAL_CALL truncate() throw (::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
      72             : 
      73             :     // XServiceInfo - static versions (used for component registration)
      74             :     static OUString SAL_CALL getImplementationName_static();
      75             :     static Sequence< OUString > SAL_CALL getSupportedServiceNames_static();
      76             :     static Reference< XInterface > SAL_CALL Create( const Reference< ::com::sun::star::uno::XComponentContext >& );
      77             : 
      78             : private:
      79             :     std::vector< sal_Int8 > maData;
      80             :     sal_Int32 mnCursor;
      81             : };
      82             : 
      83       18000 : UNOMemoryStream::UNOMemoryStream()
      84       18000 : : mnCursor(0)
      85             : {
      86       18000 : }
      87             : 
      88       35950 : UNOMemoryStream::~UNOMemoryStream()
      89             : {
      90       35950 : }
      91             : 
      92             : // XStream
      93       39318 : Reference< XInputStream > SAL_CALL UNOMemoryStream::getInputStream(  ) throw (RuntimeException, std::exception)
      94             : {
      95       39318 :     return this;
      96             : }
      97             : 
      98       27938 : Reference< XOutputStream > SAL_CALL UNOMemoryStream::getOutputStream(  ) throw (RuntimeException, std::exception)
      99             : {
     100       27938 :     return this;
     101             : }
     102             : 
     103             : // XInputStream
     104       29365 : sal_Int32 SAL_CALL UNOMemoryStream::readBytes( Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead ) throw (NotConnectedException, BufferSizeExceededException, IOException, RuntimeException, std::exception)
     105             : {
     106       29365 :     if( nBytesToRead < 0 )
     107           0 :         throw IOException();
     108             : 
     109       29365 :     nBytesToRead = std::min( nBytesToRead, available() );
     110       29365 :     aData.realloc( nBytesToRead );
     111             : 
     112       29365 :     if( nBytesToRead )
     113             :     {
     114       18353 :         sal_Int8* pData = static_cast<sal_Int8*>(&(*maData.begin()));
     115       18353 :         sal_Int8* pCursor = &((pData)[mnCursor]);
     116       18353 :         memcpy( (void*)aData.getArray(), (void*)pCursor, nBytesToRead );
     117             : 
     118       18353 :         mnCursor += nBytesToRead;
     119             :     }
     120             : 
     121       29365 :     return nBytesToRead;
     122             : }
     123             : 
     124       14952 : sal_Int32 SAL_CALL UNOMemoryStream::readSomeBytes( Sequence< sal_Int8 >& aData, sal_Int32 nMaxBytesToRead ) throw (NotConnectedException, BufferSizeExceededException, IOException, RuntimeException, std::exception)
     125             : {
     126       14952 :     return readBytes( aData, nMaxBytesToRead );
     127             : }
     128             : 
     129           0 : void SAL_CALL UNOMemoryStream::skipBytes( sal_Int32 nBytesToSkip ) throw (NotConnectedException, BufferSizeExceededException, IOException, RuntimeException, std::exception)
     130             : {
     131           0 :     if( nBytesToSkip < 0 )
     132           0 :         throw IOException();
     133             : 
     134           0 :     mnCursor += std::min( nBytesToSkip, available() );
     135           0 : }
     136             : 
     137       36129 : sal_Int32 SAL_CALL UNOMemoryStream::available() throw (NotConnectedException, IOException, RuntimeException, std::exception)
     138             : {
     139       36129 :     return static_cast< sal_Int32 >( maData.size() ) - mnCursor;
     140             : }
     141             : 
     142       33803 : void SAL_CALL UNOMemoryStream::closeInput() throw (NotConnectedException, IOException, RuntimeException, std::exception)
     143             : {
     144       33803 :     mnCursor = 0;
     145       33803 : }
     146             : 
     147             : // XSeekable
     148       23547 : void SAL_CALL UNOMemoryStream::seek( sal_Int64 location ) throw (IllegalArgumentException, IOException, RuntimeException, std::exception)
     149             : {
     150       23547 :     if( (location < 0) || (location > SAL_MAX_INT32) )
     151           0 :         throw IllegalArgumentException("this implementation does not support more than 2GB!", Reference< XInterface >(static_cast<OWeakObject*>(this)), 0 );
     152             : 
     153             :     // seek operation should be able to resize the stream
     154       23547 :     if ( location > static_cast< sal_Int64 >( maData.size() ) )
     155           0 :         maData.resize( static_cast< sal_Int32 >( location ) );
     156             : 
     157       23547 :     if ( location > static_cast< sal_Int64 >( maData.size() ) )
     158           0 :         maData.resize( static_cast< sal_Int32 >( location ) );
     159             : 
     160       23547 :     mnCursor = static_cast< sal_Int32 >( location );
     161       23547 : }
     162             : 
     163     2303306 : sal_Int64 SAL_CALL UNOMemoryStream::getPosition() throw (IOException, RuntimeException, std::exception)
     164             : {
     165     2303306 :     return static_cast< sal_Int64 >( mnCursor );
     166             : }
     167             : 
     168        8358 : sal_Int64 SAL_CALL UNOMemoryStream::getLength() throw (IOException, RuntimeException, std::exception)
     169             : {
     170        8358 :     return static_cast< sal_Int64 >( maData.size() );
     171             : }
     172             : 
     173             : // XOutputStream
     174     2305184 : void SAL_CALL UNOMemoryStream::writeBytes( const Sequence< sal_Int8 >& aData ) throw (NotConnectedException, BufferSizeExceededException, IOException, RuntimeException, std::exception)
     175             : {
     176     2305184 :     const sal_Int32 nBytesToWrite( aData.getLength() );
     177     2305184 :     if( nBytesToWrite )
     178             :     {
     179     2303061 :         sal_Int64 nNewSize = static_cast< sal_Int64 >( mnCursor + nBytesToWrite );
     180     2303061 :         if( nNewSize > SAL_MAX_INT32 )
     181             :         {
     182             :             OSL_ASSERT(false);
     183           0 :             throw IOException("this implementation does not support more than 2GB!", Reference< XInterface >(static_cast<OWeakObject*>(this)) );
     184             :         }
     185             : 
     186     2303061 :         if( static_cast< sal_Int32 >( nNewSize ) > static_cast< sal_Int32 >( maData.size() ) )
     187     2302864 :             maData.resize( static_cast< sal_Int32 >( nNewSize ) );
     188             : 
     189     2303061 :         sal_Int8* pData = static_cast<sal_Int8*>(&(*maData.begin()));
     190     2303061 :         sal_Int8* pCursor = &(pData[mnCursor]);
     191     2303061 :         memcpy( (void*)pCursor, (void*)aData.getConstArray(), nBytesToWrite );
     192             : 
     193     2303061 :         mnCursor += nBytesToWrite;
     194             :     }
     195     2305184 : }
     196             : 
     197        1625 : void SAL_CALL UNOMemoryStream::flush() throw (NotConnectedException, BufferSizeExceededException, IOException, RuntimeException, std::exception)
     198             : {
     199        1625 : }
     200             : 
     201       16800 : void SAL_CALL UNOMemoryStream::closeOutput() throw (NotConnectedException, BufferSizeExceededException, IOException, RuntimeException, std::exception)
     202             : {
     203       16800 :     mnCursor = 0;
     204       16800 : }
     205             : 
     206             : //XTruncate
     207          28 : void SAL_CALL UNOMemoryStream::truncate() throw (IOException, RuntimeException, std::exception)
     208             : {
     209          28 :     maData.resize( 0 );
     210          28 :     mnCursor = 0;
     211          28 : }
     212             : 
     213         256 : OUString SAL_CALL UNOMemoryStream::getImplementationName_static()
     214             : {
     215         256 :     return OUString("com.sun.star.comp.MemoryStream");
     216             : }
     217             : 
     218         128 : Sequence< OUString > SAL_CALL UNOMemoryStream::getSupportedServiceNames_static()
     219             : {
     220         128 :     Sequence< OUString > aSeq(1);
     221         128 :     aSeq[0] = getImplementationName_static();
     222         128 :     return aSeq;
     223             : }
     224             : 
     225       18000 : Reference< XInterface > SAL_CALL UNOMemoryStream::Create(
     226             :     SAL_UNUSED_PARAMETER const Reference< XComponentContext >& )
     227             : {
     228       18000 :     return static_cast<OWeakObject*>(new UNOMemoryStream());
     229             : }
     230             : 
     231             : } // namespace comphelper
     232             : 
     233         128 : void createRegistryInfo_UNOMemoryStream()
     234             : {
     235         128 :     static ::comphelper::module::OAutoRegistration< ::comphelper::UNOMemoryStream > aAutoRegistration;
     236         128 : }
     237             : 
     238             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10