Update ooo320-m1
[ooovba.git] / oox / source / helper / binaryoutputstream.cxx
blob474b33965fb5ae50081bd5af0faee2d69c020e9e
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: binaryoutputstream.cxx,v $
10 * $Revision: 1.4.22.2 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #include "oox/helper/binaryoutputstream.hxx"
32 #include <osl/diagnose.h>
33 #include "oox/helper/binaryinputstream.hxx"
34 #include <string.h>
36 using ::com::sun::star::uno::UNO_QUERY;
37 using ::com::sun::star::uno::Reference;
38 using ::com::sun::star::uno::Exception;
39 using ::com::sun::star::io::XOutputStream;
40 using ::com::sun::star::io::XSeekable;
42 namespace oox {
44 const sal_Int32 OUTPUTSTREAM_BUFFERSIZE = 0x8000;
46 // ============================================================================
48 void BinaryOutputStream::copyStream( BinaryInputStream& rInStrm, sal_Int64 nBytes )
50 if( nBytes > 0 )
52 sal_Int32 nBufferSize = getLimitedValue< sal_Int32, sal_Int64 >( nBytes, 0, OUTPUTSTREAM_BUFFERSIZE );
53 StreamDataSequence aBuffer( nBufferSize );
54 while( nBytes > 0 )
56 sal_Int32 nReadSize = getLimitedValue< sal_Int32, sal_Int64 >( nBytes, 0, nBufferSize );
57 sal_Int32 nBytesRead = rInStrm.readData( aBuffer, nReadSize );
58 writeData( aBuffer );
59 if( nReadSize == nBytesRead )
60 nBytes -= nReadSize;
61 else
62 nBytes = 0;
67 void BinaryOutputStream::writeAtom( const void* pMem, sal_uInt8 nSize )
69 writeMemory( pMem, nSize );
72 // ============================================================================
74 BinaryXOutputStream::BinaryXOutputStream( const Reference< XOutputStream >& rxOutStrm, bool bAutoClose ) :
75 BinaryXSeekableStream( Reference< XSeekable >( rxOutStrm, UNO_QUERY ) ),
76 maBuffer( OUTPUTSTREAM_BUFFERSIZE ),
77 mxOutStrm( rxOutStrm ),
78 mbAutoClose( bAutoClose )
80 mbEof = !mxOutStrm.is();
83 BinaryXOutputStream::~BinaryXOutputStream()
85 if( mbAutoClose )
86 close();
89 void BinaryXOutputStream::writeData( const StreamDataSequence& rData )
91 try
93 OSL_ENSURE( mxOutStrm.is(), "BinaryXOutputStream::writeData - invalid call" );
94 mxOutStrm->writeBytes( rData );
96 catch( Exception& )
98 OSL_ENSURE( false, "BinaryXOutputStream::writeData - stream read error" );
102 void BinaryXOutputStream::writeMemory( const void* pMem, sal_Int32 nBytes )
104 if( nBytes > 0 )
106 sal_Int32 nBufferSize = getLimitedValue< sal_Int32, sal_Int32 >( nBytes, 0, OUTPUTSTREAM_BUFFERSIZE );
107 const sal_uInt8* pnMem = reinterpret_cast< const sal_uInt8* >( pMem );
108 while( nBytes > 0 )
110 sal_Int32 nWriteSize = getLimitedValue< sal_Int32, sal_Int32 >( nBytes, 0, nBufferSize );
111 maBuffer.realloc( nWriteSize );
112 memcpy( maBuffer.getArray(), pnMem, static_cast< size_t >( nWriteSize ) );
113 writeData( maBuffer );
114 pnMem += nWriteSize;
115 nBytes -= nWriteSize;
120 void BinaryXOutputStream::close()
122 if( mxOutStrm.is() ) try
124 mxOutStrm->flush();
125 mxOutStrm->closeOutput();
127 catch( Exception& )
129 OSL_ENSURE( false, "BinaryXOutputStream::close - closing output stream failed" );
133 // ============================================================================
135 SequenceOutputStream::SequenceOutputStream( StreamDataSequence& rData ) :
136 SequenceSeekableStream( rData )
140 void SequenceOutputStream::writeData( const StreamDataSequence& rData )
142 if( rData.hasElements() )
143 writeMemory( rData.getConstArray(), rData.getLength() );
146 void SequenceOutputStream::writeMemory( const void* pMem, sal_Int32 nBytes )
148 if( nBytes > 0 )
150 if( mrData.getLength() - mnPos < nBytes )
151 const_cast< StreamDataSequence& >( mrData ).realloc( mnPos + nBytes );
152 memcpy( const_cast< StreamDataSequence& >( mrData ).getArray() + mnPos, pMem, static_cast< size_t >( nBytes ) );
153 mnPos += nBytes;
157 // ============================================================================
159 } // namespace oox