LCOV - code coverage report
Current view: top level - package/source/xstor - switchpersistencestream.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 116 198 58.6 %
Date: 2015-06-13 12:38:46 Functions: 20 25 80.0 %
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 <osl/diagnose.h>
      21             : 
      22             : #include <com/sun/star/io/TempFile.hpp>
      23             : #include <comphelper/processfactory.hxx>
      24             : #include <comphelper/storagehelper.hxx>
      25             : #include <switchpersistencestream.hxx>
      26             : 
      27             : using namespace ::com::sun::star;
      28             : 
      29       21638 : struct SPStreamData_Impl
      30             : {
      31             :     bool m_bInStreamBased;
      32             : 
      33             :     // the streams below are not visible from outside so there is no need to remember position
      34             : 
      35             :     // original stream related members
      36             :     uno::Reference< io::XStream > m_xOrigStream;
      37             :     uno::Reference< io::XTruncate > m_xOrigTruncate;
      38             :     uno::Reference< io::XSeekable > m_xOrigSeekable;
      39             :     uno::Reference< io::XInputStream > m_xOrigInStream;
      40             :     uno::Reference< io::XOutputStream > m_xOrigOutStream;
      41             : 
      42             :     bool m_bInOpen;
      43             :     bool m_bOutOpen;
      44             : 
      45       21669 :     SPStreamData_Impl(
      46             :             bool bInStreamBased,
      47             :             const uno::Reference< io::XStream >& xOrigStream,
      48             :             const uno::Reference< io::XTruncate >& xOrigTruncate,
      49             :             const uno::Reference< io::XSeekable >& xOrigSeekable,
      50             :             const uno::Reference< io::XInputStream >& xOrigInStream,
      51             :             const uno::Reference< io::XOutputStream >& xOrigOutStream,
      52             :             bool bInOpen,
      53             :             bool bOutOpen )
      54             :     : m_bInStreamBased( bInStreamBased )
      55             :     , m_xOrigStream( xOrigStream )
      56             :     , m_xOrigTruncate( xOrigTruncate )
      57             :     , m_xOrigSeekable( xOrigSeekable )
      58             :     , m_xOrigInStream( xOrigInStream )
      59             :     , m_xOrigOutStream( xOrigOutStream )
      60             :     , m_bInOpen( bInOpen )
      61       21669 :     , m_bOutOpen( bOutOpen )
      62             :     {
      63       21669 :     }
      64             : };
      65             : 
      66        7032 : SwitchablePersistenceStream::SwitchablePersistenceStream(
      67             :         const uno::Reference< uno::XComponentContext >& xContext,
      68             :         const uno::Reference< io::XStream >& xStream )
      69             : : m_xContext( xContext )
      70        7032 : , m_pStreamData( NULL )
      71             : {
      72        7032 :     SwitchPersistenceTo( xStream );
      73        7032 : }
      74             : 
      75       14636 : SwitchablePersistenceStream::SwitchablePersistenceStream(
      76             :         const uno::Reference< uno::XComponentContext >& xContext,
      77             :         const uno::Reference< io::XInputStream >& xInputStream )
      78             : : m_xContext( xContext )
      79       14636 : , m_pStreamData( NULL )
      80             : {
      81       14636 :     SwitchPersistenceTo( xInputStream );
      82       14636 : }
      83             : 
      84       64911 : SwitchablePersistenceStream::~SwitchablePersistenceStream()
      85             : {
      86       21637 :     CloseAll_Impl();
      87       43274 : }
      88             : 
      89        7032 : void SwitchablePersistenceStream::SwitchPersistenceTo( const uno::Reference< io::XStream >& xStream )
      90             : {
      91        7032 :     uno::Reference< io::XTruncate > xNewTruncate( xStream, uno::UNO_QUERY_THROW );
      92       14064 :     uno::Reference< io::XSeekable > xNewSeekable( xStream, uno::UNO_QUERY_THROW );
      93       14064 :     uno::Reference< io::XInputStream > xNewInStream = xStream->getInputStream();
      94       14064 :     uno::Reference< io::XOutputStream > xNewOutStream = xStream->getOutputStream();
      95        7032 :     if ( !xNewInStream.is() || !xNewOutStream.is() )
      96           0 :         throw uno::RuntimeException();
      97             : 
      98        7032 :     sal_Int64 nPos = 0;
      99        7032 :     bool bInOpen = false;
     100        7032 :     bool bOutOpen = false;
     101             : 
     102        7032 :     if ( m_pStreamData && m_pStreamData->m_xOrigSeekable.is() )
     103             :     {
     104             :         // check that the length is the same
     105           0 :         if ( m_pStreamData->m_xOrigSeekable->getLength() != xNewSeekable->getLength() )
     106           0 :             throw uno::RuntimeException();
     107             : 
     108             :         // get the current position
     109           0 :         nPos = m_pStreamData->m_xOrigSeekable->getPosition();
     110           0 :         bInOpen = m_pStreamData->m_bInOpen;
     111           0 :         bOutOpen = m_pStreamData->m_bOutOpen;
     112             :     }
     113             : 
     114        7032 :     xNewSeekable->seek( nPos );
     115             : 
     116        7032 :     CloseAll_Impl();
     117             : 
     118             :     m_pStreamData = new SPStreamData_Impl( false,
     119             :                                             xStream, xNewTruncate, xNewSeekable, xNewInStream, xNewOutStream,
     120       14064 :                                             bInOpen, bOutOpen );
     121        7032 : }
     122             : 
     123       14637 : void SwitchablePersistenceStream::SwitchPersistenceTo( const uno::Reference< io::XInputStream >& xInputStream )
     124             : {
     125       14637 :     uno::Reference< io::XStream > xNewStream;
     126       29274 :     uno::Reference< io::XTruncate > xNewTruncate;
     127       29274 :     uno::Reference< io::XSeekable > xNewSeekable( xInputStream, uno::UNO_QUERY_THROW );
     128       29274 :     uno::Reference< io::XOutputStream > xNewOutStream;
     129       14637 :     if ( !xInputStream.is() )
     130           0 :         throw uno::RuntimeException();
     131             : 
     132       14637 :     sal_Int64 nPos = 0;
     133       14637 :     bool bInOpen = false;
     134       14637 :     bool bOutOpen = false;
     135             : 
     136       14637 :     if ( m_pStreamData && m_pStreamData->m_xOrigSeekable.is() )
     137             :     {
     138             :         // check that the length is the same
     139           1 :         if ( m_pStreamData->m_xOrigSeekable->getLength() != xNewSeekable->getLength() )
     140           0 :             throw uno::RuntimeException();
     141             : 
     142             :         // get the current position
     143           1 :         nPos = m_pStreamData->m_xOrigSeekable->getPosition();
     144           1 :         bInOpen = m_pStreamData->m_bInOpen;
     145           1 :         bOutOpen = m_pStreamData->m_bOutOpen;
     146             :     }
     147             : 
     148       14637 :     xNewSeekable->seek( nPos );
     149             : 
     150       14637 :     CloseAll_Impl();
     151             : 
     152             :     m_pStreamData = new SPStreamData_Impl( true,
     153             :                                             xNewStream, xNewTruncate, xNewSeekable, xInputStream, xNewOutStream,
     154       29274 :                                             bInOpen, bOutOpen );
     155             : 
     156       14637 : }
     157             : 
     158           0 : void SwitchablePersistenceStream::CopyAndSwitchPersistenceTo( const uno::Reference< io::XStream >& xStream )
     159             : {
     160           0 :     uno::Reference< io::XStream > xTargetStream = xStream;
     161           0 :     uno::Reference< io::XSeekable > xTargetSeek;
     162             : 
     163           0 :     if ( !xTargetStream.is() )
     164             :     {
     165           0 :         xTargetStream = uno::Reference < io::XStream >(
     166             :             io::TempFile::create(m_xContext),
     167           0 :             uno::UNO_QUERY_THROW );
     168             : 
     169           0 :         xTargetSeek = uno::Reference< io::XSeekable >( xTargetStream, uno::UNO_QUERY_THROW );
     170             :     }
     171             :     else
     172             :     {
     173             :         // the provided stream must be empty
     174           0 :         xTargetSeek = uno::Reference< io::XSeekable >( xTargetStream, uno::UNO_QUERY_THROW );
     175           0 :         if ( xTargetSeek->getLength() )
     176           0 :             throw io::IOException();
     177             :     }
     178             : 
     179           0 :     uno::Reference< io::XTruncate > xTargetTruncate( xTargetStream, uno::UNO_QUERY_THROW );
     180           0 :     uno::Reference< io::XInputStream > xTargetInStream = xTargetStream->getInputStream();
     181           0 :     uno::Reference< io::XOutputStream > xTargetOutStream = xTargetStream->getOutputStream();
     182           0 :     if ( !xTargetInStream.is() || !xTargetOutStream.is() )
     183           0 :         throw uno::RuntimeException();
     184             : 
     185           0 :     if ( !m_pStreamData->m_xOrigInStream.is() || !m_pStreamData->m_xOrigSeekable.is() )
     186           0 :         throw uno::RuntimeException();
     187             : 
     188           0 :     sal_Int64 nPos = m_pStreamData->m_xOrigSeekable->getPosition();
     189           0 :     m_pStreamData->m_xOrigSeekable->seek( 0 );
     190           0 :     ::comphelper::OStorageHelper::CopyInputToOutput( m_pStreamData->m_xOrigInStream, xTargetOutStream );
     191           0 :     xTargetOutStream->flush();
     192           0 :     xTargetSeek->seek( nPos );
     193             : 
     194           0 :     bool bInOpen = m_pStreamData->m_bInOpen;
     195           0 :     bool bOutOpen = m_pStreamData->m_bOutOpen;
     196             : 
     197           0 :     CloseAll_Impl();
     198             : 
     199             :     m_pStreamData = new SPStreamData_Impl( false,
     200             :                                         xTargetStream, xTargetTruncate, xTargetSeek, xTargetInStream, xTargetOutStream,
     201           0 :                                         bInOpen, bOutOpen );
     202           0 : }
     203             : 
     204       43317 : void SwitchablePersistenceStream::CloseAll_Impl()
     205             : {
     206       43317 :     if ( m_pStreamData )
     207             :     {
     208       21638 :         delete m_pStreamData;
     209       21638 :         m_pStreamData = NULL;
     210             :     }
     211       43317 : }
     212             : 
     213             : // com::sun::star::io::XStream
     214       31336 : uno::Reference< io::XInputStream > SAL_CALL SwitchablePersistenceStream::getInputStream(  )
     215             :     throw (uno::RuntimeException, std::exception)
     216             : {
     217       31336 :     ::osl::MutexGuard aGuard( m_aMutex );
     218             : 
     219       31336 :     if ( m_pStreamData )
     220       31336 :         m_pStreamData->m_bInOpen = true;
     221       31336 :     return static_cast< io::XInputStream* >( this );
     222             : }
     223             : 
     224         832 : uno::Reference< io::XOutputStream > SAL_CALL SwitchablePersistenceStream::getOutputStream(  )
     225             :     throw (uno::RuntimeException, std::exception)
     226             : {
     227         832 :     ::osl::MutexGuard aGuard( m_aMutex );
     228             : 
     229         832 :     if ( m_pStreamData )
     230         832 :         m_pStreamData->m_bOutOpen = true;
     231         832 :     return static_cast< io::XOutputStream* >( this );
     232             : }
     233             : 
     234             : // com::sun::star::io::XInputStream
     235      599081 : ::sal_Int32 SAL_CALL SwitchablePersistenceStream::readBytes( uno::Sequence< ::sal_Int8 >& aData, ::sal_Int32 nBytesToRead )
     236             :     throw (io::NotConnectedException, io::BufferSizeExceededException, io::IOException, uno::RuntimeException, std::exception)
     237             : {
     238      599081 :     ::osl::MutexGuard aGuard( m_aMutex );
     239             : 
     240      599081 :     if ( !m_pStreamData )
     241           0 :         throw io::NotConnectedException();
     242             : 
     243             :     // the original stream data should be provided
     244      599081 :     if ( !m_pStreamData->m_xOrigInStream.is() )
     245           0 :         throw uno::RuntimeException();
     246             : 
     247      599081 :     return m_pStreamData->m_xOrigInStream->readBytes( aData, nBytesToRead );
     248             : }
     249             : 
     250           0 : ::sal_Int32 SAL_CALL SwitchablePersistenceStream::readSomeBytes( uno::Sequence< ::sal_Int8 >& aData, ::sal_Int32 nMaxBytesToRead )
     251             :     throw (io::NotConnectedException, io::BufferSizeExceededException, io::IOException, uno::RuntimeException, std::exception)
     252             : {
     253           0 :     ::osl::MutexGuard aGuard( m_aMutex );
     254             : 
     255           0 :     if ( !m_pStreamData )
     256           0 :         throw io::NotConnectedException();
     257             : 
     258             :     // the original stream data should be provided
     259           0 :     if ( !m_pStreamData->m_xOrigInStream.is() )
     260           0 :         throw uno::RuntimeException();
     261             : 
     262           0 :     return m_pStreamData->m_xOrigInStream->readBytes( aData, nMaxBytesToRead );
     263             : }
     264             : 
     265           0 : void SAL_CALL SwitchablePersistenceStream::skipBytes( ::sal_Int32 nBytesToSkip )
     266             :     throw (io::NotConnectedException, io::BufferSizeExceededException, io::IOException, uno::RuntimeException, std::exception)
     267             : {
     268           0 :     ::osl::MutexGuard aGuard( m_aMutex );
     269             : 
     270           0 :     if ( !m_pStreamData )
     271           0 :         throw io::NotConnectedException();
     272             : 
     273             :     // the original stream data should be provided
     274           0 :     if ( !m_pStreamData->m_xOrigInStream.is() )
     275           0 :         throw uno::RuntimeException();
     276             : 
     277           0 :     m_pStreamData->m_xOrigInStream->skipBytes( nBytesToSkip );
     278           0 : }
     279             : 
     280           0 : ::sal_Int32 SAL_CALL SwitchablePersistenceStream::available(  )
     281             :     throw (io::NotConnectedException, io::IOException, uno::RuntimeException, std::exception)
     282             : {
     283           0 :     ::osl::MutexGuard aGuard( m_aMutex );
     284             : 
     285           0 :     if ( !m_pStreamData )
     286           0 :         throw io::NotConnectedException();
     287             : 
     288             :     // the original stream data should be provided
     289           0 :     if ( !m_pStreamData->m_xOrigInStream.is() )
     290           0 :         throw uno::RuntimeException();
     291             : 
     292           0 :     return m_pStreamData->m_xOrigInStream->available();
     293             : }
     294             : 
     295          11 : void SAL_CALL SwitchablePersistenceStream::closeInput()
     296             :     throw (io::NotConnectedException, io::IOException, uno::RuntimeException, std::exception)
     297             : {
     298          11 :     ::osl::MutexGuard aGuard( m_aMutex );
     299             : 
     300          11 :     if ( !m_pStreamData )
     301           0 :         throw io::NotConnectedException();
     302             : 
     303          11 :     m_pStreamData->m_bInOpen = false;
     304          11 :     if ( !m_pStreamData->m_bOutOpen )
     305          11 :         CloseAll_Impl();
     306          11 : }
     307             : 
     308             : // com::sun::star::io::XOutputStream
     309      390850 : void SAL_CALL SwitchablePersistenceStream::writeBytes( const uno::Sequence< ::sal_Int8 >& aData )
     310             :     throw (io::NotConnectedException, io::BufferSizeExceededException, io::IOException, uno::RuntimeException, std::exception)
     311             : {
     312      390850 :     ::osl::MutexGuard aGuard( m_aMutex );
     313             : 
     314      390850 :     if ( !m_pStreamData )
     315           0 :         throw io::NotConnectedException();
     316             : 
     317      390850 :     if ( m_pStreamData->m_bInStreamBased )
     318           0 :         throw io::IOException();
     319             : 
     320             :     // the original stream data should be provided
     321      390850 :     if ( !m_pStreamData->m_xOrigOutStream.is() )
     322           0 :         throw uno::RuntimeException();
     323             : 
     324      390850 :     m_pStreamData->m_xOrigOutStream->writeBytes( aData );
     325      390850 : }
     326             : 
     327        1628 : void SAL_CALL SwitchablePersistenceStream::flush(  )
     328             :     throw (io::NotConnectedException, io::BufferSizeExceededException, io::IOException, uno::RuntimeException, std::exception)
     329             : {
     330        1628 :     ::osl::MutexGuard aGuard( m_aMutex );
     331             : 
     332        1628 :     if ( !m_pStreamData || m_pStreamData->m_bInStreamBased )
     333             :     {
     334             :         OSL_FAIL( "flush() is not acceptable!\n" );
     335        1628 :         return;
     336             :         // in future throw exception, for now some code might call flush() on closed stream
     337             :         // since file ucp implementation allows it
     338             :         // throw io::NotConnectedException();
     339             :     }
     340             : 
     341             :     // the original stream data should be provided
     342        1628 :     if ( !m_pStreamData->m_xOrigOutStream.is() )
     343           0 :         throw uno::RuntimeException();
     344             : 
     345        1628 :     m_pStreamData->m_xOrigOutStream->flush();
     346             : }
     347             : 
     348           0 : void SAL_CALL SwitchablePersistenceStream::closeOutput(  )
     349             :     throw (io::NotConnectedException, io::BufferSizeExceededException, io::IOException, uno::RuntimeException, std::exception)
     350             : {
     351           0 :     ::osl::MutexGuard aGuard( m_aMutex );
     352             : 
     353           0 :     if ( !m_pStreamData )
     354           0 :         throw io::NotConnectedException();
     355             : 
     356           0 :     m_pStreamData->m_bOutOpen = false;
     357           0 :     if ( !m_pStreamData->m_bInOpen )
     358           0 :         CloseAll_Impl();
     359           0 : }
     360             : 
     361             : // com::sun::star::io::XTruncate
     362          36 : void SAL_CALL SwitchablePersistenceStream::truncate(  )
     363             :     throw (io::IOException, uno::RuntimeException, std::exception)
     364             : {
     365          36 :     ::osl::MutexGuard aGuard( m_aMutex );
     366             : 
     367          36 :     if ( !m_pStreamData )
     368           0 :         throw io::NotConnectedException();
     369             : 
     370          36 :     if ( m_pStreamData->m_bInStreamBased )
     371           0 :         throw io::IOException();
     372             : 
     373             :     // the original stream data should be provided
     374          36 :     if ( !m_pStreamData->m_xOrigTruncate.is() )
     375           0 :         throw uno::RuntimeException();
     376             : 
     377          36 :     m_pStreamData->m_xOrigTruncate->truncate();
     378          36 : }
     379             : 
     380             : // com::sun::star::io::XSeekable
     381      126075 : void SAL_CALL SwitchablePersistenceStream::seek( ::sal_Int64 location )
     382             :     throw (lang::IllegalArgumentException, io::IOException, uno::RuntimeException, std::exception)
     383             : {
     384      126075 :     ::osl::MutexGuard aGuard( m_aMutex );
     385             : 
     386      126075 :     if ( !m_pStreamData )
     387           0 :         throw io::NotConnectedException();
     388             : 
     389             :     // the original stream data should be provided
     390      126075 :     if ( !m_pStreamData->m_xOrigSeekable.is() )
     391           0 :         throw uno::RuntimeException();
     392             : 
     393      126075 :     m_pStreamData->m_xOrigSeekable->seek( location );
     394      126075 : }
     395             : 
     396      103652 : ::sal_Int64 SAL_CALL SwitchablePersistenceStream::getPosition(  )
     397             :     throw (io::IOException, uno::RuntimeException, std::exception)
     398             : {
     399      103652 :     ::osl::MutexGuard aGuard( m_aMutex );
     400             : 
     401      103652 :     if ( !m_pStreamData )
     402           0 :         throw io::NotConnectedException();
     403             : 
     404             :     // the original stream data should be provided
     405      103652 :     if ( !m_pStreamData->m_xOrigSeekable.is() )
     406           0 :         throw uno::RuntimeException();
     407             : 
     408      103652 :     return m_pStreamData->m_xOrigSeekable->getPosition();
     409             : }
     410             : 
     411      151276 : ::sal_Int64 SAL_CALL SwitchablePersistenceStream::getLength(  )
     412             :     throw (io::IOException, uno::RuntimeException, std::exception)
     413             : {
     414      151276 :     ::osl::MutexGuard aGuard( m_aMutex );
     415             : 
     416      151276 :     if ( !m_pStreamData )
     417           0 :         throw io::NotConnectedException();
     418             : 
     419             :     // the original stream data should be provided
     420      151276 :     if ( !m_pStreamData->m_xOrigSeekable.is() )
     421           0 :         throw uno::RuntimeException();
     422             : 
     423      151276 :     return m_pStreamData->m_xOrigSeekable->getLength();
     424             : }
     425             : 
     426         832 : void SAL_CALL SwitchablePersistenceStream::waitForCompletion()
     427             :     throw (::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException, std::exception)
     428             : {
     429         832 :     if ( !m_pStreamData )
     430           0 :         throw io::NotConnectedException();
     431             : 
     432         832 :     uno::Reference< io::XAsyncOutputMonitor > asyncOutputMonitor( m_pStreamData->m_xOrigOutStream, uno::UNO_QUERY );
     433         832 :     if ( asyncOutputMonitor.is() )
     434         234 :         asyncOutputMonitor->waitForCompletion();
     435         832 : }
     436             : 
     437             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11