LCOV - code coverage report
Current view: top level - package/source/xstor - switchpersistencestream.cxx (source / functions) Hit Total Coverage
Test: commit e02a6cb2c3e2b23b203b422e4e0680877f232636 Lines: 0 198 0.0 %
Date: 2014-04-14 Functions: 0 25 0.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           0 : struct SPStreamData_Impl
      30             : {
      31             :     sal_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             :     sal_Bool m_bInOpen;
      43             :     sal_Bool m_bOutOpen;
      44             : 
      45           0 :     SPStreamData_Impl(
      46             :             sal_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             :             sal_Bool bInOpen,
      53             :             sal_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           0 :     , m_bOutOpen( bOutOpen )
      62             :     {
      63           0 :     }
      64             : };
      65             : 
      66           0 : SwitchablePersistenceStream::SwitchablePersistenceStream(
      67             :         const uno::Reference< uno::XComponentContext >& xContext,
      68             :         const uno::Reference< io::XStream >& xStream )
      69             : : m_xContext( xContext )
      70           0 : , m_pStreamData( NULL )
      71             : {
      72           0 :     SwitchPersistenceTo( xStream );
      73           0 : }
      74             : 
      75           0 : SwitchablePersistenceStream::SwitchablePersistenceStream(
      76             :         const uno::Reference< uno::XComponentContext >& xContext,
      77             :         const uno::Reference< io::XInputStream >& xInputStream )
      78             : : m_xContext( xContext )
      79           0 : , m_pStreamData( NULL )
      80             : {
      81           0 :     SwitchPersistenceTo( xInputStream );
      82           0 : }
      83             : 
      84           0 : SwitchablePersistenceStream::~SwitchablePersistenceStream()
      85             : {
      86           0 :     CloseAll_Impl();
      87           0 : }
      88             : 
      89           0 : void SwitchablePersistenceStream::SwitchPersistenceTo( const uno::Reference< io::XStream >& xStream )
      90             : {
      91           0 :     uno::Reference< io::XTruncate > xNewTruncate( xStream, uno::UNO_QUERY_THROW );
      92           0 :     uno::Reference< io::XSeekable > xNewSeekable( xStream, uno::UNO_QUERY_THROW );
      93           0 :     uno::Reference< io::XInputStream > xNewInStream = xStream->getInputStream();
      94           0 :     uno::Reference< io::XOutputStream > xNewOutStream = xStream->getOutputStream();
      95           0 :     if ( !xNewInStream.is() || !xNewOutStream.is() )
      96           0 :         throw uno::RuntimeException();
      97             : 
      98           0 :     sal_Int64 nPos = 0;
      99           0 :     sal_Bool bInOpen = sal_False;
     100           0 :     sal_Bool bOutOpen = sal_False;
     101             : 
     102           0 :     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           0 :     xNewSeekable->seek( nPos );
     115             : 
     116           0 :     CloseAll_Impl();
     117             : 
     118             :     m_pStreamData = new SPStreamData_Impl( sal_False,
     119             :                                             xStream, xNewTruncate, xNewSeekable, xNewInStream, xNewOutStream,
     120           0 :                                             bInOpen, bOutOpen );
     121           0 : }
     122             : 
     123           0 : void SwitchablePersistenceStream::SwitchPersistenceTo( const uno::Reference< io::XInputStream >& xInputStream )
     124             : {
     125           0 :     uno::Reference< io::XStream > xNewStream;
     126           0 :     uno::Reference< io::XTruncate > xNewTruncate;
     127           0 :     uno::Reference< io::XSeekable > xNewSeekable( xInputStream, uno::UNO_QUERY_THROW );
     128           0 :     uno::Reference< io::XOutputStream > xNewOutStream;
     129           0 :     if ( !xInputStream.is() )
     130           0 :         throw uno::RuntimeException();
     131             : 
     132           0 :     sal_Int64 nPos = 0;
     133           0 :     sal_Bool bInOpen = sal_False;
     134           0 :     sal_Bool bOutOpen = sal_False;
     135             : 
     136           0 :     if ( m_pStreamData && m_pStreamData->m_xOrigSeekable.is() )
     137             :     {
     138             :         // check that the length is the same
     139           0 :         if ( m_pStreamData->m_xOrigSeekable->getLength() != xNewSeekable->getLength() )
     140           0 :             throw uno::RuntimeException();
     141             : 
     142             :         // get the current position
     143           0 :         nPos = m_pStreamData->m_xOrigSeekable->getPosition();
     144           0 :         bInOpen = m_pStreamData->m_bInOpen;
     145           0 :         bOutOpen = m_pStreamData->m_bOutOpen;
     146             :     }
     147             : 
     148           0 :     xNewSeekable->seek( nPos );
     149             : 
     150           0 :     CloseAll_Impl();
     151             : 
     152             :     m_pStreamData = new SPStreamData_Impl( sal_True,
     153             :                                             xNewStream, xNewTruncate, xNewSeekable, xInputStream, xNewOutStream,
     154           0 :                                             bInOpen, bOutOpen );
     155             : 
     156           0 : }
     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 :     sal_Bool bInOpen = m_pStreamData->m_bInOpen;
     195           0 :     sal_Bool bOutOpen = m_pStreamData->m_bOutOpen;
     196             : 
     197           0 :     CloseAll_Impl();
     198             : 
     199             :     m_pStreamData = new SPStreamData_Impl( sal_False,
     200             :                                         xTargetStream, xTargetTruncate, xTargetSeek, xTargetInStream, xTargetOutStream,
     201           0 :                                         bInOpen, bOutOpen );
     202           0 : }
     203             : 
     204           0 : void SwitchablePersistenceStream::CloseAll_Impl()
     205             : {
     206           0 :     if ( m_pStreamData )
     207             :     {
     208           0 :         delete m_pStreamData;
     209           0 :         m_pStreamData = NULL;
     210             :     }
     211           0 : }
     212             : 
     213             : // com::sun::star::io::XStream
     214           0 : uno::Reference< io::XInputStream > SAL_CALL SwitchablePersistenceStream::getInputStream(  )
     215             :     throw (uno::RuntimeException, std::exception)
     216             : {
     217           0 :     ::osl::MutexGuard aGuard( m_aMutex );
     218             : 
     219           0 :     if ( m_pStreamData )
     220           0 :         m_pStreamData->m_bInOpen = sal_True;
     221           0 :     return static_cast< io::XInputStream* >( this );
     222             : }
     223             : 
     224           0 : uno::Reference< io::XOutputStream > SAL_CALL SwitchablePersistenceStream::getOutputStream(  )
     225             :     throw (uno::RuntimeException, std::exception)
     226             : {
     227           0 :     ::osl::MutexGuard aGuard( m_aMutex );
     228             : 
     229           0 :     if ( m_pStreamData )
     230           0 :         m_pStreamData->m_bOutOpen = sal_True;
     231           0 :     return static_cast< io::XOutputStream* >( this );
     232             : }
     233             : 
     234             : // com::sun::star::io::XInputStream
     235           0 : ::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           0 :     ::osl::MutexGuard aGuard( m_aMutex );
     239             : 
     240           0 :     if ( !m_pStreamData )
     241           0 :         throw io::NotConnectedException();
     242             : 
     243             :     // the original stream data should be provided
     244           0 :     if ( !m_pStreamData->m_xOrigInStream.is() )
     245           0 :         throw uno::RuntimeException();
     246             : 
     247           0 :     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           0 : void SAL_CALL SwitchablePersistenceStream::closeInput()
     296             :     throw (io::NotConnectedException, io::IOException, uno::RuntimeException, std::exception)
     297             : {
     298           0 :     ::osl::MutexGuard aGuard( m_aMutex );
     299             : 
     300           0 :     if ( !m_pStreamData )
     301           0 :         throw io::NotConnectedException();
     302             : 
     303           0 :     m_pStreamData->m_bInOpen = sal_False;
     304           0 :     if ( !m_pStreamData->m_bOutOpen )
     305           0 :         CloseAll_Impl();
     306           0 : }
     307             : 
     308             : // com::sun::star::io::XOutputStream
     309           0 : 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           0 :     ::osl::MutexGuard aGuard( m_aMutex );
     313             : 
     314           0 :     if ( !m_pStreamData )
     315           0 :         throw io::NotConnectedException();
     316             : 
     317           0 :     if ( m_pStreamData->m_bInStreamBased )
     318           0 :         throw io::IOException();
     319             : 
     320             :     // the original stream data should be provided
     321           0 :     if ( !m_pStreamData->m_xOrigOutStream.is() )
     322           0 :         throw uno::RuntimeException();
     323             : 
     324           0 :     m_pStreamData->m_xOrigOutStream->writeBytes( aData );
     325           0 : }
     326             : 
     327           0 : void SAL_CALL SwitchablePersistenceStream::flush(  )
     328             :     throw (io::NotConnectedException, io::BufferSizeExceededException, io::IOException, uno::RuntimeException, std::exception)
     329             : {
     330           0 :     ::osl::MutexGuard aGuard( m_aMutex );
     331             : 
     332           0 :     if ( !m_pStreamData || m_pStreamData->m_bInStreamBased )
     333             :     {
     334             :         OSL_FAIL( "flush() is not acceptable!\n" );
     335           0 :         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           0 :     if ( !m_pStreamData->m_xOrigOutStream.is() )
     343           0 :         throw uno::RuntimeException();
     344             : 
     345           0 :     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 = sal_False;
     357           0 :     if ( !m_pStreamData->m_bInOpen )
     358           0 :         CloseAll_Impl();
     359           0 : }
     360             : 
     361             : // com::sun::star::io::XTruncate
     362           0 : void SAL_CALL SwitchablePersistenceStream::truncate(  )
     363             :     throw (io::IOException, uno::RuntimeException, std::exception)
     364             : {
     365           0 :     ::osl::MutexGuard aGuard( m_aMutex );
     366             : 
     367           0 :     if ( !m_pStreamData )
     368           0 :         throw io::NotConnectedException();
     369             : 
     370           0 :     if ( m_pStreamData->m_bInStreamBased )
     371           0 :         throw io::IOException();
     372             : 
     373             :     // the original stream data should be provided
     374           0 :     if ( !m_pStreamData->m_xOrigTruncate.is() )
     375           0 :         throw uno::RuntimeException();
     376             : 
     377           0 :     m_pStreamData->m_xOrigTruncate->truncate();
     378           0 : }
     379             : 
     380             : // com::sun::star::io::XSeekable
     381           0 : void SAL_CALL SwitchablePersistenceStream::seek( ::sal_Int64 location )
     382             :     throw (lang::IllegalArgumentException, io::IOException, uno::RuntimeException, std::exception)
     383             : {
     384           0 :     ::osl::MutexGuard aGuard( m_aMutex );
     385             : 
     386           0 :     if ( !m_pStreamData )
     387           0 :         throw io::NotConnectedException();
     388             : 
     389             :     // the original stream data should be provided
     390           0 :     if ( !m_pStreamData->m_xOrigSeekable.is() )
     391           0 :         throw uno::RuntimeException();
     392             : 
     393           0 :     m_pStreamData->m_xOrigSeekable->seek( location );
     394           0 : }
     395             : 
     396           0 : ::sal_Int64 SAL_CALL SwitchablePersistenceStream::getPosition(  )
     397             :     throw (io::IOException, uno::RuntimeException, std::exception)
     398             : {
     399           0 :     ::osl::MutexGuard aGuard( m_aMutex );
     400             : 
     401           0 :     if ( !m_pStreamData )
     402           0 :         throw io::NotConnectedException();
     403             : 
     404             :     // the original stream data should be provided
     405           0 :     if ( !m_pStreamData->m_xOrigSeekable.is() )
     406           0 :         throw uno::RuntimeException();
     407             : 
     408           0 :     return m_pStreamData->m_xOrigSeekable->getPosition();
     409             : }
     410             : 
     411           0 : ::sal_Int64 SAL_CALL SwitchablePersistenceStream::getLength(  )
     412             :     throw (io::IOException, uno::RuntimeException, std::exception)
     413             : {
     414           0 :     ::osl::MutexGuard aGuard( m_aMutex );
     415             : 
     416           0 :     if ( !m_pStreamData )
     417           0 :         throw io::NotConnectedException();
     418             : 
     419             :     // the original stream data should be provided
     420           0 :     if ( !m_pStreamData->m_xOrigSeekable.is() )
     421           0 :         throw uno::RuntimeException();
     422             : 
     423           0 :     return m_pStreamData->m_xOrigSeekable->getLength();
     424             : }
     425             : 
     426           0 : void SAL_CALL SwitchablePersistenceStream::waitForCompletion()
     427             :     throw (::com::sun::star::io::IOException, ::com::sun::star::uno::RuntimeException, std::exception)
     428             : {
     429           0 :     if ( !m_pStreamData )
     430           0 :         throw io::NotConnectedException();
     431             : 
     432           0 :     uno::Reference< io::XAsyncOutputMonitor > asyncOutputMonitor( m_pStreamData->m_xOrigOutStream, uno::UNO_QUERY );
     433           0 :     if ( asyncOutputMonitor.is() )
     434           0 :         asyncOutputMonitor->waitForCompletion();
     435           0 : }
     436             : 
     437             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10