1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #include "oox/helper/binaryoutputstream.hxx"
29 
30 #include <com/sun/star/io/XOutputStream.hpp>
31 #include <com/sun/star/io/XSeekable.hpp>
32 #include <osl/diagnose.h>
33 #include <string.h>
34 
35 namespace oox {
36 
37 // ============================================================================
38 
39 using namespace ::com::sun::star::io;
40 using namespace ::com::sun::star::uno;
41 
42 namespace {
43 
44 const sal_Int32 OUTPUTSTREAM_BUFFERSIZE     = 0x8000;
45 
46 } // namespace
47 
48 // ============================================================================
49 
50 BinaryXOutputStream::BinaryXOutputStream( const Reference< XOutputStream >& rxOutStrm, bool bAutoClose ) :
51     BinaryStreamBase( Reference< XSeekable >( rxOutStrm, UNO_QUERY ).is() ),
52     BinaryXSeekableStream( Reference< XSeekable >( rxOutStrm, UNO_QUERY ) ),
53     maBuffer( OUTPUTSTREAM_BUFFERSIZE ),
54     mxOutStrm( rxOutStrm ),
55     mbAutoClose( bAutoClose && rxOutStrm.is() )
56 {
57     mbEof = !mxOutStrm.is();
58 }
59 
60 BinaryXOutputStream::~BinaryXOutputStream()
61 {
62     close();
63 }
64 
65 void BinaryXOutputStream::close()
66 {
67     OSL_ENSURE( !mbAutoClose || mxOutStrm.is(), "BinaryXOutputStream::close - invalid call" );
68     if( mxOutStrm.is() ) try
69     {
70         mxOutStrm->flush();
71         if( mbAutoClose )
72             mxOutStrm->closeOutput();
73     }
74     catch( Exception& )
75     {
76         OSL_ENSURE( false, "BinaryXOutputStream::close - closing output stream failed" );
77     }
78     mxOutStrm.clear();
79     mbAutoClose = false;
80     BinaryXSeekableStream::close();
81 }
82 
83 void BinaryXOutputStream::writeData( const StreamDataSequence& rData, size_t /*nAtomSize*/ )
84 {
85     if( mxOutStrm.is() ) try
86     {
87         mxOutStrm->writeBytes( rData );
88     }
89     catch( Exception& )
90     {
91         OSL_ENSURE( false, "BinaryXOutputStream::writeData - stream read error" );
92     }
93 }
94 
95 void BinaryXOutputStream::writeMemory( const void* pMem, sal_Int32 nBytes, size_t nAtomSize )
96 {
97     if( mxOutStrm.is() && (nBytes > 0) )
98     {
99         sal_Int32 nBufferSize = getLimitedValue< sal_Int32, sal_Int32 >( nBytes, 0, (OUTPUTSTREAM_BUFFERSIZE / nAtomSize) * nAtomSize );
100         const sal_uInt8* pnMem = reinterpret_cast< const sal_uInt8* >( pMem );
101         while( nBytes > 0 )
102         {
103             sal_Int32 nWriteSize = getLimitedValue< sal_Int32, sal_Int32 >( nBytes, 0, nBufferSize );
104             maBuffer.realloc( nWriteSize );
105             memcpy( maBuffer.getArray(), pnMem, static_cast< size_t >( nWriteSize ) );
106             writeData( maBuffer, nAtomSize );
107             pnMem += nWriteSize;
108             nBytes -= nWriteSize;
109         }
110     }
111 }
112 
113 // ============================================================================
114 
115 SequenceOutputStream::SequenceOutputStream( StreamDataSequence& rData ) :
116     BinaryStreamBase( true ),
117     SequenceSeekableStream( rData )
118 {
119 }
120 
121 void SequenceOutputStream::writeData( const StreamDataSequence& rData, size_t nAtomSize )
122 {
123     if( mpData && rData.hasElements() )
124         writeMemory( rData.getConstArray(), rData.getLength(), nAtomSize );
125 }
126 
127 void SequenceOutputStream::writeMemory( const void* pMem, sal_Int32 nBytes, size_t /*nAtomSize*/ )
128 {
129     if( mpData && (nBytes > 0) )
130     {
131         if( mpData->getLength() - mnPos < nBytes )
132             const_cast< StreamDataSequence* >( mpData )->realloc( mnPos + nBytes );
133         memcpy( const_cast< StreamDataSequence* >( mpData )->getArray() + mnPos, pMem, static_cast< size_t >( nBytes ) );
134         mnPos += nBytes;
135     }
136 }
137 
138 // ============================================================================
139 
140 } // namespace oox
141 
142