LCOV - code coverage report
Current view: top level - libreoffice/unotools/source/streaming - streamwrap.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 134 166 80.7 %
Date: 2012-12-17 Functions: 35 39 89.7 %
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 <unotools/streamwrap.hxx>
      21             : #include <tools/stream.hxx>
      22             : #include <tools/debug.hxx>
      23             : 
      24             : namespace utl
      25             : {
      26             : 
      27             : using namespace ::com::sun::star::uno;
      28             : using namespace ::com::sun::star::io;
      29             : using namespace ::com::sun::star::lang;
      30             : 
      31             : //==================================================================
      32             : //= OInputStreamWrapper
      33             : //==================================================================
      34             : DBG_NAME(OInputStreamWrapper)
      35             : //------------------------------------------------------------------
      36         236 : OInputStreamWrapper::OInputStreamWrapper( SvStream& _rStream )
      37             :                  :m_pSvStream(&_rStream)
      38         236 :                  ,m_bSvStreamOwner(sal_False)
      39             : {
      40             :     DBG_CTOR(OInputStreamWrapper,NULL);
      41             : 
      42         236 : }
      43             : 
      44             : //------------------------------------------------------------------
      45          18 : OInputStreamWrapper::OInputStreamWrapper( SvStream* pStream, sal_Bool bOwner )
      46             :                  :m_pSvStream( pStream )
      47          18 :                  ,m_bSvStreamOwner( bOwner )
      48             : {
      49             :     DBG_CTOR(OInputStreamWrapper,NULL);
      50             : 
      51          18 : }
      52             : 
      53             : //------------------------------------------------------------------
      54        1694 : OInputStreamWrapper::~OInputStreamWrapper()
      55             : {
      56         720 :     if( m_bSvStreamOwner )
      57           2 :         delete m_pSvStream;
      58             : 
      59             :     DBG_DTOR(OInputStreamWrapper,NULL);
      60         974 : }
      61             : 
      62             : //------------------------------------------------------------------------------
      63        5430 : sal_Int32 SAL_CALL OInputStreamWrapper::readBytes(staruno::Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead)
      64             :                 throw( stario::NotConnectedException, stario::BufferSizeExceededException, staruno::RuntimeException )
      65             : {
      66        5430 :     checkConnected();
      67             : 
      68        5430 :     if (nBytesToRead < 0)
      69           0 :         throw stario::BufferSizeExceededException(::rtl::OUString(),static_cast<staruno::XWeak*>(this));
      70             : 
      71        5430 :     ::osl::MutexGuard aGuard( m_aMutex );
      72             : 
      73        5430 :     aData.realloc(nBytesToRead);
      74             : 
      75        5430 :     sal_uInt32 nRead = m_pSvStream->Read((void*)aData.getArray(), nBytesToRead);
      76        5430 :     checkError();
      77             : 
      78             :     // Wenn gelesene Zeichen < MaxLength, staruno::Sequence anpassen
      79        5430 :     if (nRead < (sal_uInt32)nBytesToRead)
      80         314 :         aData.realloc( nRead );
      81             : 
      82        5430 :     return nRead;
      83             : }
      84             : 
      85             : //------------------------------------------------------------------------------
      86           2 : sal_Int32 SAL_CALL OInputStreamWrapper::readSomeBytes(staruno::Sequence< sal_Int8 >& aData, sal_Int32 nMaxBytesToRead) throw( stario::NotConnectedException, stario::BufferSizeExceededException, staruno::RuntimeException )
      87             : {
      88           2 :     checkError();
      89             : 
      90           0 :     if (nMaxBytesToRead < 0)
      91           0 :         throw stario::BufferSizeExceededException(::rtl::OUString(),static_cast<staruno::XWeak*>(this));
      92             : 
      93           0 :     if (m_pSvStream->IsEof())
      94             :     {
      95           0 :         aData.realloc(0);
      96           0 :         return 0;
      97             :     }
      98             :     else
      99           0 :         return readBytes(aData, nMaxBytesToRead);
     100             : }
     101             : 
     102             : //------------------------------------------------------------------------------
     103         216 : void SAL_CALL OInputStreamWrapper::skipBytes(sal_Int32 nBytesToSkip) throw( stario::NotConnectedException, stario::BufferSizeExceededException, staruno::RuntimeException )
     104             : {
     105         216 :     ::osl::MutexGuard aGuard( m_aMutex );
     106         216 :     checkError();
     107             : 
     108         216 :     m_pSvStream->SeekRel(nBytesToSkip);
     109         216 :     checkError();
     110         216 : }
     111             : 
     112             : //------------------------------------------------------------------------------
     113           0 : sal_Int32 SAL_CALL OInputStreamWrapper::available() throw( stario::NotConnectedException, staruno::RuntimeException )
     114             : {
     115           0 :     ::osl::MutexGuard aGuard( m_aMutex );
     116           0 :     checkConnected();
     117             : 
     118           0 :     sal_uInt32 nPos = m_pSvStream->Tell();
     119           0 :     checkError();
     120             : 
     121           0 :     m_pSvStream->Seek(STREAM_SEEK_TO_END);
     122           0 :     checkError();
     123             : 
     124           0 :     sal_Int32 nAvailable = (sal_Int32)m_pSvStream->Tell() - nPos;
     125           0 :     m_pSvStream->Seek(nPos);
     126           0 :     checkError();
     127             : 
     128           0 :     return nAvailable;
     129             : }
     130             : 
     131             : //------------------------------------------------------------------------------
     132          38 : void SAL_CALL OInputStreamWrapper::closeInput() throw( stario::NotConnectedException, staruno::RuntimeException )
     133             : {
     134          38 :     ::osl::MutexGuard aGuard( m_aMutex );
     135          38 :     checkConnected();
     136             : 
     137          38 :     if (m_bSvStreamOwner)
     138           0 :         delete m_pSvStream;
     139             : 
     140          38 :     m_pSvStream = NULL;
     141          38 : }
     142             : 
     143             : //------------------------------------------------------------------------------
     144       19224 : void OInputStreamWrapper::checkConnected() const
     145             : {
     146       19224 :     if (!m_pSvStream)
     147           0 :         throw stario::NotConnectedException(::rtl::OUString(), const_cast<staruno::XWeak*>(static_cast<const staruno::XWeak*>(this)));
     148       19224 : }
     149             : 
     150             : //------------------------------------------------------------------------------
     151       10260 : void OInputStreamWrapper::checkError() const
     152             : {
     153       10260 :     checkConnected();
     154             : 
     155       10260 :     if (m_pSvStream->SvStream::GetError() != ERRCODE_NONE)
     156             :         // TODO: really evaluate the error
     157           2 :         throw stario::NotConnectedException(::rtl::OUString(), const_cast<staruno::XWeak*>(static_cast<const staruno::XWeak*>(this)));
     158       10258 : }
     159             : 
     160             : //==================================================================
     161             : //= OSeekableInputStreamWrapper
     162             : //==================================================================
     163             : //------------------------------------------------------------------------------
     164         366 : OSeekableInputStreamWrapper::OSeekableInputStreamWrapper(SvStream& _rStream)
     165             : {
     166         366 :     SetStream( &_rStream, sal_False );
     167         366 : }
     168             : 
     169             : //------------------------------------------------------------------------------
     170           0 : OSeekableInputStreamWrapper::OSeekableInputStreamWrapper(SvStream* _pStream, sal_Bool _bOwner)
     171             : {
     172           0 :     SetStream( _pStream, _bOwner );
     173           0 : }
     174             : 
     175             : //------------------------------------------------------------------------------
     176        1660 : void SAL_CALL OSeekableInputStreamWrapper::seek( sal_Int64 _nLocation ) throw (IllegalArgumentException, IOException, RuntimeException)
     177             : {
     178        1660 :     ::osl::MutexGuard aGuard( m_aMutex );
     179        1660 :     checkConnected();
     180             : 
     181        1660 :     m_pSvStream->Seek((sal_uInt32)_nLocation);
     182        1660 :     checkError();
     183        1660 : }
     184             : 
     185             : //------------------------------------------------------------------------------
     186         936 : sal_Int64 SAL_CALL OSeekableInputStreamWrapper::getPosition(  ) throw (IOException, RuntimeException)
     187             : {
     188         936 :     ::osl::MutexGuard aGuard( m_aMutex );
     189         936 :     checkConnected();
     190             : 
     191         936 :     sal_uInt32 nPos = m_pSvStream->Tell();
     192         936 :     checkError();
     193         936 :     return (sal_Int64)nPos;
     194             : }
     195             : 
     196             : //------------------------------------------------------------------------------
     197         900 : sal_Int64 SAL_CALL OSeekableInputStreamWrapper::getLength(  ) throw (IOException, RuntimeException)
     198             : {
     199         900 :     ::osl::MutexGuard aGuard( m_aMutex );
     200         900 :     checkConnected();
     201             : 
     202         900 :     sal_uInt32 nCurrentPos = m_pSvStream->Tell();
     203         900 :     checkError();
     204             : 
     205         900 :     m_pSvStream->Seek(STREAM_SEEK_TO_END);
     206         900 :     sal_uInt32 nEndPos = m_pSvStream->Tell();
     207         900 :     m_pSvStream->Seek(nCurrentPos);
     208             : 
     209         900 :     checkError();
     210             : 
     211         900 :     return (sal_Int64)nEndPos;
     212             : }
     213             : 
     214             : //==================================================================
     215             : //= OOutputStreamWrapper
     216             : //==================================================================
     217             : 
     218         114 : OOutputStreamWrapper::OOutputStreamWrapper(SvStream& _rStream):
     219         114 :     rStream(_rStream)
     220         114 : {}
     221             : 
     222         222 : OOutputStreamWrapper::~OOutputStreamWrapper() {}
     223             : 
     224         124 : void SAL_CALL OOutputStreamWrapper::writeBytes(const staruno::Sequence< sal_Int8 >& aData) throw( stario::NotConnectedException, stario::BufferSizeExceededException, staruno::RuntimeException )
     225             : {
     226         124 :     sal_uInt32 nWritten = rStream.Write(aData.getConstArray(),aData.getLength());
     227         124 :     ErrCode err = rStream.GetError();
     228         248 :     if  (   (ERRCODE_NONE != err)
     229         124 :         ||  (nWritten != (sal_uInt32)aData.getLength())
     230             :         )
     231             :     {
     232           0 :         throw stario::BufferSizeExceededException(::rtl::OUString(),static_cast<staruno::XWeak*>(this));
     233             :     }
     234         124 : }
     235             : 
     236             : //------------------------------------------------------------------
     237           6 : void SAL_CALL OOutputStreamWrapper::flush() throw( stario::NotConnectedException, stario::BufferSizeExceededException, staruno::RuntimeException )
     238             : {
     239           6 :     rStream.Flush();
     240           6 :     checkError();
     241           6 : }
     242             : 
     243             : //------------------------------------------------------------------
     244           8 : void SAL_CALL OOutputStreamWrapper::closeOutput() throw( stario::NotConnectedException, stario::BufferSizeExceededException, staruno::RuntimeException )
     245             : {
     246           8 : }
     247             : 
     248             : //------------------------------------------------------------------------------
     249         160 : void OOutputStreamWrapper::checkError() const
     250             : {
     251         160 :     if (rStream.GetError() != ERRCODE_NONE)
     252             :         // TODO: really evaluate the error
     253           0 :         throw stario::NotConnectedException(::rtl::OUString(), const_cast<staruno::XWeak*>(static_cast<const staruno::XWeak*>(this)));
     254         160 : }
     255             : 
     256             : //==================================================================
     257             : //= OSeekableOutputStreamWrapper
     258             : //==================================================================
     259             : //------------------------------------------------------------------------------
     260           6 : OSeekableOutputStreamWrapper::OSeekableOutputStreamWrapper(SvStream& _rStream)
     261           6 :     :OOutputStreamWrapper(_rStream)
     262             : {
     263           6 : }
     264             : 
     265          12 : OSeekableOutputStreamWrapper::~OSeekableOutputStreamWrapper() {}
     266             : 
     267             : //------------------------------------------------------------------------------
     268          12 : Any SAL_CALL OSeekableOutputStreamWrapper::queryInterface( const Type& _rType ) throw (RuntimeException)
     269             : {
     270          12 :     Any aReturn = OOutputStreamWrapper::queryInterface(_rType);
     271          12 :     if (!aReturn.hasValue())
     272          12 :         aReturn = OSeekableOutputStreamWrapper_Base::queryInterface(_rType);
     273          12 :     return aReturn;
     274             : }
     275             : 
     276             : //------------------------------------------------------------------------------
     277          42 : void SAL_CALL OSeekableOutputStreamWrapper::acquire(  ) throw ()
     278             : {
     279          42 :     OOutputStreamWrapper::acquire();
     280          42 : }
     281             : 
     282             : //------------------------------------------------------------------------------
     283          42 : void SAL_CALL OSeekableOutputStreamWrapper::release(  ) throw ()
     284             : {
     285          42 :     OOutputStreamWrapper::release();
     286          42 : }
     287             : 
     288             : //------------------------------------------------------------------------------
     289           8 : void SAL_CALL OSeekableOutputStreamWrapper::seek( sal_Int64 _nLocation ) throw (IllegalArgumentException, IOException, RuntimeException)
     290             : {
     291           8 :     rStream.Seek((sal_uInt32)_nLocation);
     292           8 :     checkError();
     293           8 : }
     294             : 
     295             : //------------------------------------------------------------------------------
     296         138 : sal_Int64 SAL_CALL OSeekableOutputStreamWrapper::getPosition(  ) throw (IOException, RuntimeException)
     297             : {
     298         138 :     sal_uInt32 nPos = rStream.Tell();
     299         138 :     checkError();
     300         138 :     return (sal_Int64)nPos;
     301             : }
     302             : 
     303             : //------------------------------------------------------------------------------
     304           4 : sal_Int64 SAL_CALL OSeekableOutputStreamWrapper::getLength(  ) throw (IOException, RuntimeException)
     305             : {
     306           4 :     sal_uInt32 nCurrentPos = rStream.Tell();
     307           4 :     checkError();
     308             : 
     309           4 :     rStream.Seek(STREAM_SEEK_TO_END);
     310           4 :     sal_uInt32 nEndPos = rStream.Tell();
     311           4 :     rStream.Seek(nCurrentPos);
     312             : 
     313           4 :     checkError();
     314             : 
     315           4 :     return (sal_Int64)nEndPos;
     316             : }
     317             : 
     318             : //------------------------------------------------------------------------------
     319         108 : OStreamWrapper::OStreamWrapper(SvStream& _rStream)
     320             : {
     321         108 :     SetStream( &_rStream, sal_False );
     322         108 : }
     323             : 
     324             : //------------------------------------------------------------------------------
     325         212 : ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream > SAL_CALL OStreamWrapper::getInputStream(  ) throw (::com::sun::star::uno::RuntimeException)
     326             : {
     327         212 :     return this;
     328             : }
     329             : 
     330             : //------------------------------------------------------------------------------
     331         216 : ::com::sun::star::uno::Reference< ::com::sun::star::io::XOutputStream > SAL_CALL OStreamWrapper::getOutputStream(  ) throw (::com::sun::star::uno::RuntimeException)
     332             : {
     333         216 :     return this;
     334             : }
     335             : 
     336             : //------------------------------------------------------------------------------
     337       15554 : void SAL_CALL OStreamWrapper::writeBytes(const staruno::Sequence< sal_Int8 >& aData) throw(stario::NotConnectedException, stario::BufferSizeExceededException, staruno::RuntimeException)
     338             : {
     339       15554 :     sal_uInt32 nWritten = m_pSvStream->Write(aData.getConstArray(),aData.getLength());
     340       15554 :     ErrCode err = m_pSvStream->GetError();
     341       31108 :     if  (   (ERRCODE_NONE != err)
     342       15554 :         ||  (nWritten != (sal_uInt32)aData.getLength())
     343             :         )
     344             :     {
     345           0 :         throw stario::BufferSizeExceededException(::rtl::OUString(),static_cast<staruno::XWeak*>(this));
     346             :     }
     347       15554 : }
     348             : 
     349             : //------------------------------------------------------------------------------
     350         156 : void SAL_CALL OStreamWrapper::flush() throw(stario::NotConnectedException, stario::BufferSizeExceededException, staruno::RuntimeException)
     351             : {
     352         156 :     m_pSvStream->Flush();
     353         156 :     if (m_pSvStream->GetError() != ERRCODE_NONE)
     354           0 :         throw stario::NotConnectedException(::rtl::OUString(),static_cast<staruno::XWeak*>(this));
     355         156 : }
     356             : 
     357             : //------------------------------------------------------------------------------
     358           0 : void SAL_CALL OStreamWrapper::closeOutput() throw(stario::NotConnectedException, stario::BufferSizeExceededException, staruno::RuntimeException)
     359             : {
     360           0 : }
     361             : 
     362             : //------------------------------------------------------------------------------
     363           0 : void SAL_CALL OStreamWrapper::truncate() throw(::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException)
     364             : {
     365           0 :     m_pSvStream->SetStreamSize(0);
     366           0 : }
     367             : 
     368             : } // namespace utl
     369             : 
     370             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10