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