1ca5ec200SAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3ca5ec200SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4ca5ec200SAndrew Rist * or more contributor license agreements. See the NOTICE file 5ca5ec200SAndrew Rist * distributed with this work for additional information 6ca5ec200SAndrew Rist * regarding copyright ownership. The ASF licenses this file 7ca5ec200SAndrew Rist * to you under the Apache License, Version 2.0 (the 8ca5ec200SAndrew Rist * "License"); you may not use this file except in compliance 9ca5ec200SAndrew Rist * with the License. You may obtain a copy of the License at 10cdf0e10cSrcweir * 11ca5ec200SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12cdf0e10cSrcweir * 13ca5ec200SAndrew Rist * Unless required by applicable law or agreed to in writing, 14ca5ec200SAndrew Rist * software distributed under the License is distributed on an 15ca5ec200SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16ca5ec200SAndrew Rist * KIND, either express or implied. See the License for the 17ca5ec200SAndrew Rist * specific language governing permissions and limitations 18ca5ec200SAndrew Rist * under the License. 19cdf0e10cSrcweir * 20ca5ec200SAndrew Rist *************************************************************/ 21ca5ec200SAndrew Rist 22ca5ec200SAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir #include "oox/helper/binaryoutputstream.hxx" 25cdf0e10cSrcweir 26cdf0e10cSrcweir #include <com/sun/star/io/XOutputStream.hpp> 27cdf0e10cSrcweir #include <com/sun/star/io/XSeekable.hpp> 28cdf0e10cSrcweir #include <osl/diagnose.h> 29cdf0e10cSrcweir #include <string.h> 30cdf0e10cSrcweir 31cdf0e10cSrcweir namespace oox { 32cdf0e10cSrcweir 33cdf0e10cSrcweir // ============================================================================ 34cdf0e10cSrcweir 35cdf0e10cSrcweir using namespace ::com::sun::star::io; 36cdf0e10cSrcweir using namespace ::com::sun::star::uno; 37cdf0e10cSrcweir 38cdf0e10cSrcweir namespace { 39cdf0e10cSrcweir 40cdf0e10cSrcweir const sal_Int32 OUTPUTSTREAM_BUFFERSIZE = 0x8000; 41cdf0e10cSrcweir 42cdf0e10cSrcweir } // namespace 43cdf0e10cSrcweir 44cdf0e10cSrcweir // ============================================================================ 45cdf0e10cSrcweir 46cdf0e10cSrcweir BinaryXOutputStream::BinaryXOutputStream( const Reference< XOutputStream >& rxOutStrm, bool bAutoClose ) : 47cdf0e10cSrcweir BinaryStreamBase( Reference< XSeekable >( rxOutStrm, UNO_QUERY ).is() ), 48cdf0e10cSrcweir BinaryXSeekableStream( Reference< XSeekable >( rxOutStrm, UNO_QUERY ) ), 49cdf0e10cSrcweir maBuffer( OUTPUTSTREAM_BUFFERSIZE ), 50cdf0e10cSrcweir mxOutStrm( rxOutStrm ), 51cdf0e10cSrcweir mbAutoClose( bAutoClose && rxOutStrm.is() ) 52cdf0e10cSrcweir { 53cdf0e10cSrcweir mbEof = !mxOutStrm.is(); 54cdf0e10cSrcweir } 55cdf0e10cSrcweir 56cdf0e10cSrcweir BinaryXOutputStream::~BinaryXOutputStream() 57cdf0e10cSrcweir { 58cdf0e10cSrcweir close(); 59cdf0e10cSrcweir } 60cdf0e10cSrcweir 61*506fa58bSDamjan Jovanovic void BinaryXOutputStream::flush() 62*506fa58bSDamjan Jovanovic { 63*506fa58bSDamjan Jovanovic if( mxOutStrm.is() ) try 64*506fa58bSDamjan Jovanovic { 65*506fa58bSDamjan Jovanovic mxOutStrm->flush(); 66*506fa58bSDamjan Jovanovic } 67*506fa58bSDamjan Jovanovic catch( Exception& ) 68*506fa58bSDamjan Jovanovic { 69*506fa58bSDamjan Jovanovic OSL_ENSURE( false, "BinaryXOutputStream::flush - flushing stream failed" ); 70*506fa58bSDamjan Jovanovic } 71*506fa58bSDamjan Jovanovic } 72*506fa58bSDamjan Jovanovic 73cdf0e10cSrcweir void BinaryXOutputStream::close() 74cdf0e10cSrcweir { 75cdf0e10cSrcweir OSL_ENSURE( !mbAutoClose || mxOutStrm.is(), "BinaryXOutputStream::close - invalid call" ); 76cdf0e10cSrcweir if( mxOutStrm.is() ) try 77cdf0e10cSrcweir { 78cdf0e10cSrcweir mxOutStrm->flush(); 79cdf0e10cSrcweir if( mbAutoClose ) 80cdf0e10cSrcweir mxOutStrm->closeOutput(); 81cdf0e10cSrcweir } 82cdf0e10cSrcweir catch( Exception& ) 83cdf0e10cSrcweir { 84cdf0e10cSrcweir OSL_ENSURE( false, "BinaryXOutputStream::close - closing output stream failed" ); 85cdf0e10cSrcweir } 86cdf0e10cSrcweir mxOutStrm.clear(); 87cdf0e10cSrcweir mbAutoClose = false; 88cdf0e10cSrcweir BinaryXSeekableStream::close(); 89cdf0e10cSrcweir } 90cdf0e10cSrcweir 91cdf0e10cSrcweir void BinaryXOutputStream::writeData( const StreamDataSequence& rData, size_t /*nAtomSize*/ ) 92cdf0e10cSrcweir { 93cdf0e10cSrcweir if( mxOutStrm.is() ) try 94cdf0e10cSrcweir { 95cdf0e10cSrcweir mxOutStrm->writeBytes( rData ); 96cdf0e10cSrcweir } 97cdf0e10cSrcweir catch( Exception& ) 98cdf0e10cSrcweir { 99cdf0e10cSrcweir OSL_ENSURE( false, "BinaryXOutputStream::writeData - stream read error" ); 100cdf0e10cSrcweir } 101cdf0e10cSrcweir } 102cdf0e10cSrcweir 103cdf0e10cSrcweir void BinaryXOutputStream::writeMemory( const void* pMem, sal_Int32 nBytes, size_t nAtomSize ) 104cdf0e10cSrcweir { 105cdf0e10cSrcweir if( mxOutStrm.is() && (nBytes > 0) ) 106cdf0e10cSrcweir { 107cdf0e10cSrcweir sal_Int32 nBufferSize = getLimitedValue< sal_Int32, sal_Int32 >( nBytes, 0, (OUTPUTSTREAM_BUFFERSIZE / nAtomSize) * nAtomSize ); 108cdf0e10cSrcweir const sal_uInt8* pnMem = reinterpret_cast< const sal_uInt8* >( pMem ); 109cdf0e10cSrcweir while( nBytes > 0 ) 110cdf0e10cSrcweir { 111cdf0e10cSrcweir sal_Int32 nWriteSize = getLimitedValue< sal_Int32, sal_Int32 >( nBytes, 0, nBufferSize ); 112cdf0e10cSrcweir maBuffer.realloc( nWriteSize ); 113cdf0e10cSrcweir memcpy( maBuffer.getArray(), pnMem, static_cast< size_t >( nWriteSize ) ); 114cdf0e10cSrcweir writeData( maBuffer, nAtomSize ); 115cdf0e10cSrcweir pnMem += nWriteSize; 116cdf0e10cSrcweir nBytes -= nWriteSize; 117cdf0e10cSrcweir } 118cdf0e10cSrcweir } 119cdf0e10cSrcweir } 120cdf0e10cSrcweir 121cdf0e10cSrcweir // ============================================================================ 122cdf0e10cSrcweir 123cdf0e10cSrcweir SequenceOutputStream::SequenceOutputStream( StreamDataSequence& rData ) : 124cdf0e10cSrcweir BinaryStreamBase( true ), 125cdf0e10cSrcweir SequenceSeekableStream( rData ) 126cdf0e10cSrcweir { 127cdf0e10cSrcweir } 128cdf0e10cSrcweir 129cdf0e10cSrcweir void SequenceOutputStream::writeData( const StreamDataSequence& rData, size_t nAtomSize ) 130cdf0e10cSrcweir { 131cdf0e10cSrcweir if( mpData && rData.hasElements() ) 132cdf0e10cSrcweir writeMemory( rData.getConstArray(), rData.getLength(), nAtomSize ); 133cdf0e10cSrcweir } 134cdf0e10cSrcweir 135cdf0e10cSrcweir void SequenceOutputStream::writeMemory( const void* pMem, sal_Int32 nBytes, size_t /*nAtomSize*/ ) 136cdf0e10cSrcweir { 137cdf0e10cSrcweir if( mpData && (nBytes > 0) ) 138cdf0e10cSrcweir { 139cdf0e10cSrcweir if( mpData->getLength() - mnPos < nBytes ) 140cdf0e10cSrcweir const_cast< StreamDataSequence* >( mpData )->realloc( mnPos + nBytes ); 141cdf0e10cSrcweir memcpy( const_cast< StreamDataSequence* >( mpData )->getArray() + mnPos, pMem, static_cast< size_t >( nBytes ) ); 142cdf0e10cSrcweir mnPos += nBytes; 143cdf0e10cSrcweir } 144cdf0e10cSrcweir } 145cdf0e10cSrcweir 146cdf0e10cSrcweir // ============================================================================ 147cdf0e10cSrcweir 148cdf0e10cSrcweir } // namespace oox 149cdf0e10cSrcweir 150