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

Generated by: LCOV version 1.10