LCOV - code coverage report
Current view: top level - libreoffice/comphelper/source/streaming - memorystream.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 68 78 87.2 %
Date: 2012-12-27 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             : 
      23             : #include <com/sun/star/io/XStream.hpp>
      24             : #include <com/sun/star/io/XSeekableInputStream.hpp>
      25             : #include <com/sun/star/io/XTruncate.hpp>
      26             : #include <com/sun/star/uno/XComponentContext.hpp>
      27             : #include <cppuhelper/implbase4.hxx>
      28             : 
      29             : #include <string.h>
      30             : #include <vector>
      31             : 
      32             : using ::rtl::OUString;
      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);
      51             :     virtual Reference< XOutputStream > SAL_CALL getOutputStream(  ) throw (RuntimeException);
      52             : 
      53             :     // XInputStream
      54             :     virtual sal_Int32 SAL_CALL readBytes( Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead ) throw (NotConnectedException, BufferSizeExceededException, IOException, RuntimeException);
      55             :     virtual sal_Int32 SAL_CALL readSomeBytes( Sequence< sal_Int8 >& aData, sal_Int32 nMaxBytesToRead ) throw (NotConnectedException, BufferSizeExceededException, IOException, RuntimeException);
      56             :     virtual void SAL_CALL skipBytes( sal_Int32 nBytesToSkip ) throw (NotConnectedException, BufferSizeExceededException, IOException, RuntimeException);
      57             :     virtual sal_Int32 SAL_CALL available() throw (NotConnectedException, IOException, RuntimeException);
      58             :     virtual void SAL_CALL closeInput() throw (NotConnectedException, IOException, RuntimeException);
      59             : 
      60             :     // XSeekable
      61             :     virtual void SAL_CALL seek( sal_Int64 location ) throw (IllegalArgumentException, IOException, RuntimeException);
      62             :     virtual sal_Int64 SAL_CALL getPosition() throw (IOException, RuntimeException);
      63             :     virtual sal_Int64 SAL_CALL getLength() throw (IOException, RuntimeException);
      64             : 
      65             :     // XOutputStream
      66             :     virtual void SAL_CALL writeBytes( const Sequence< sal_Int8 >& aData ) throw (NotConnectedException, BufferSizeExceededException, IOException, RuntimeException);
      67             :     virtual void SAL_CALL flush() throw (NotConnectedException, BufferSizeExceededException, IOException, RuntimeException);
      68             :     virtual void SAL_CALL closeOutput() throw (NotConnectedException, BufferSizeExceededException, IOException, RuntimeException);
      69             : 
      70             :     // XTruncate
      71             :     virtual void SAL_CALL truncate() throw (::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException);
      72             : 
      73             :     // XServiceInfo - static versions (used for component registration)
      74             :     static ::rtl::OUString SAL_CALL getImplementationName_static();
      75             :     static Sequence< ::rtl::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        1144 : UNOMemoryStream::UNOMemoryStream()
      84        1144 : : mnCursor(0)
      85             : {
      86        1144 : }
      87             : 
      88        1854 : UNOMemoryStream::~UNOMemoryStream()
      89             : {
      90        1854 : }
      91             : 
      92             : // XStream
      93        2288 : Reference< XInputStream > SAL_CALL UNOMemoryStream::getInputStream(  ) throw (RuntimeException)
      94             : {
      95        2288 :     return this;
      96             : }
      97             : 
      98        1610 : Reference< XOutputStream > SAL_CALL UNOMemoryStream::getOutputStream(  ) throw (RuntimeException)
      99             : {
     100        1610 :     return this;
     101             : }
     102             : 
     103             : // XInputStream
     104        1168 : sal_Int32 SAL_CALL UNOMemoryStream::readBytes( Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead ) throw (NotConnectedException, BufferSizeExceededException, IOException, RuntimeException)
     105             : {
     106        1168 :     if( nBytesToRead < 0 )
     107           0 :         throw IOException();
     108             : 
     109        1168 :     nBytesToRead = std::min( nBytesToRead, available() );
     110        1168 :     aData.realloc( nBytesToRead );
     111             : 
     112        1168 :     if( nBytesToRead )
     113             :     {
     114         720 :         sal_Int8* pData = static_cast<sal_Int8*>(&(*maData.begin()));
     115         720 :         sal_Int8* pCursor = &((pData)[mnCursor]);
     116         720 :         memcpy( (void*)aData.getArray(), (void*)pCursor, nBytesToRead );
     117             : 
     118         720 :         mnCursor += nBytesToRead;
     119             :     }
     120             : 
     121        1168 :     return nBytesToRead;
     122             : }
     123             : 
     124         902 : sal_Int32 SAL_CALL UNOMemoryStream::readSomeBytes( Sequence< sal_Int8 >& aData, sal_Int32 nMaxBytesToRead ) throw (NotConnectedException, BufferSizeExceededException, IOException, RuntimeException)
     125             : {
     126         902 :     return readBytes( aData, nMaxBytesToRead );
     127             : }
     128             : 
     129           0 : void SAL_CALL UNOMemoryStream::skipBytes( sal_Int32 nBytesToSkip ) throw (NotConnectedException, BufferSizeExceededException, IOException, RuntimeException)
     130             : {
     131           0 :     if( nBytesToSkip < 0 )
     132           0 :         throw IOException();
     133             : 
     134           0 :     mnCursor += std::min( nBytesToSkip, available() );
     135           0 : }
     136             : 
     137        1168 : sal_Int32 SAL_CALL UNOMemoryStream::available() throw (NotConnectedException, IOException, RuntimeException)
     138             : {
     139        1168 :     return static_cast< sal_Int32 >( maData.size() ) - mnCursor;
     140             : }
     141             : 
     142        1702 : void SAL_CALL UNOMemoryStream::closeInput() throw (NotConnectedException, IOException, RuntimeException)
     143             : {
     144        1702 :     mnCursor = 0;
     145        1702 : }
     146             : 
     147             : // XSeekable
     148        1118 : void SAL_CALL UNOMemoryStream::seek( sal_Int64 location ) throw (IllegalArgumentException, IOException, RuntimeException)
     149             : {
     150        1118 :     if( (location < 0) || (location > SAL_MAX_INT32) )
     151           0 :         throw IllegalArgumentException( OUString(RTL_CONSTASCII_USTRINGPARAM("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        1118 :     if ( location > static_cast< sal_Int64 >( maData.size() ) )
     155           0 :         maData.resize( static_cast< sal_Int32 >( location ) );
     156             : 
     157        1118 :     if ( location > static_cast< sal_Int64 >( maData.size() ) )
     158           0 :         maData.resize( static_cast< sal_Int32 >( location ) );
     159             : 
     160        1118 :     mnCursor = static_cast< sal_Int32 >( location );
     161        1118 : }
     162             : 
     163       47244 : sal_Int64 SAL_CALL UNOMemoryStream::getPosition() throw (IOException, RuntimeException)
     164             : {
     165       47244 :     return static_cast< sal_Int64 >( mnCursor );
     166             : }
     167             : 
     168         690 : sal_Int64 SAL_CALL UNOMemoryStream::getLength() throw (IOException, RuntimeException)
     169             : {
     170         690 :     return static_cast< sal_Int64 >( maData.size() );
     171             : }
     172             : 
     173             : // XOutputStream
     174       47345 : void SAL_CALL UNOMemoryStream::writeBytes( const Sequence< sal_Int8 >& aData ) throw (NotConnectedException, BufferSizeExceededException, IOException, RuntimeException)
     175             : {
     176       47345 :     const sal_Int32 nBytesToWrite( aData.getLength() );
     177       47345 :     if( nBytesToWrite )
     178             :     {
     179       47274 :         sal_Int64 nNewSize = static_cast< sal_Int64 >( mnCursor + nBytesToWrite );
     180       47274 :         if( nNewSize > SAL_MAX_INT32 )
     181             :         {
     182             :             OSL_ASSERT(false);
     183           0 :             throw IOException( OUString(RTL_CONSTASCII_USTRINGPARAM("this implementation does not support more than 2GB!")), Reference< XInterface >(static_cast<OWeakObject*>(this)) );
     184             :         }
     185             : 
     186       47274 :         if( static_cast< sal_Int32 >( nNewSize ) > static_cast< sal_Int32 >( maData.size() ) )
     187       47248 :             maData.resize( static_cast< sal_Int32 >( nNewSize ) );
     188             : 
     189       47274 :         sal_Int8* pData = static_cast<sal_Int8*>(&(*maData.begin()));
     190       47274 :         sal_Int8* pCursor = &(pData[mnCursor]);
     191       47274 :         memcpy( (void*)pCursor, (void*)aData.getConstArray(), nBytesToWrite );
     192             : 
     193       47274 :         mnCursor += nBytesToWrite;
     194             :     }
     195       47345 : }
     196             : 
     197         117 : void SAL_CALL UNOMemoryStream::flush() throw (NotConnectedException, BufferSizeExceededException, IOException, RuntimeException)
     198             : {
     199         117 : }
     200             : 
     201        1023 : void SAL_CALL UNOMemoryStream::closeOutput() throw (NotConnectedException, BufferSizeExceededException, IOException, RuntimeException)
     202             : {
     203        1023 :     mnCursor = 0;
     204        1023 : }
     205             : 
     206             : //XTruncate
     207           3 : void SAL_CALL UNOMemoryStream::truncate() throw (IOException, RuntimeException)
     208             : {
     209           3 :     maData.resize( 0 );
     210           3 :     mnCursor = 0;
     211           3 : }
     212             : 
     213          28 : ::rtl::OUString SAL_CALL UNOMemoryStream::getImplementationName_static()
     214             : {
     215          28 :     return ::rtl::OUString("com.sun.star.comp.MemoryStream");
     216             : }
     217             : 
     218          14 : Sequence< ::rtl::OUString > SAL_CALL UNOMemoryStream::getSupportedServiceNames_static()
     219             : {
     220          14 :     Sequence< OUString > aSeq(1);
     221          14 :     aSeq[0] = getImplementationName_static();
     222          14 :     return aSeq;
     223             : }
     224             : 
     225        1144 : Reference< XInterface > SAL_CALL UNOMemoryStream::Create(
     226             :     SAL_UNUSED_PARAMETER const Reference< XComponentContext >& )
     227             : {
     228        1144 :     return static_cast<OWeakObject*>(new UNOMemoryStream());
     229             : }
     230             : 
     231             : } // namespace comphelper
     232             : 
     233          14 : void createRegistryInfo_UNOMemoryStream()
     234             : {
     235          14 :     static ::comphelper::module::OAutoRegistration< ::comphelper::UNOMemoryStream > aAutoRegistration;
     236          14 : }
     237             : 
     238             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10