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
BinaryXOutputStream(const Reference<XOutputStream> & rxOutStrm,bool bAutoClose)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
~BinaryXOutputStream()56 BinaryXOutputStream::~BinaryXOutputStream()
57 {
58 close();
59 }
60
flush()61 void BinaryXOutputStream::flush()
62 {
63 if( mxOutStrm.is() ) try
64 {
65 mxOutStrm->flush();
66 }
67 catch( Exception& )
68 {
69 OSL_ENSURE( false, "BinaryXOutputStream::flush - flushing stream failed" );
70 }
71 }
72
close()73 void BinaryXOutputStream::close()
74 {
75 OSL_ENSURE( !mbAutoClose || mxOutStrm.is(), "BinaryXOutputStream::close - invalid call" );
76 if( mxOutStrm.is() ) try
77 {
78 mxOutStrm->flush();
79 if( mbAutoClose )
80 mxOutStrm->closeOutput();
81 }
82 catch( Exception& )
83 {
84 OSL_ENSURE( false, "BinaryXOutputStream::close - closing output stream failed" );
85 }
86 mxOutStrm.clear();
87 mbAutoClose = false;
88 BinaryXSeekableStream::close();
89 }
90
writeData(const StreamDataSequence & rData,size_t)91 void BinaryXOutputStream::writeData( const StreamDataSequence& rData, size_t /*nAtomSize*/ )
92 {
93 if( mxOutStrm.is() ) try
94 {
95 mxOutStrm->writeBytes( rData );
96 }
97 catch( Exception& )
98 {
99 OSL_ENSURE( false, "BinaryXOutputStream::writeData - stream read error" );
100 }
101 }
102
writeMemory(const void * pMem,sal_Int32 nBytes,size_t nAtomSize)103 void BinaryXOutputStream::writeMemory( const void* pMem, sal_Int32 nBytes, size_t nAtomSize )
104 {
105 if( mxOutStrm.is() && (nBytes > 0) )
106 {
107 sal_Int32 nBufferSize = getLimitedValue< sal_Int32, sal_Int32 >( nBytes, 0, (OUTPUTSTREAM_BUFFERSIZE / nAtomSize) * nAtomSize );
108 const sal_uInt8* pnMem = reinterpret_cast< const sal_uInt8* >( pMem );
109 while( nBytes > 0 )
110 {
111 sal_Int32 nWriteSize = getLimitedValue< sal_Int32, sal_Int32 >( nBytes, 0, nBufferSize );
112 maBuffer.realloc( nWriteSize );
113 memcpy( maBuffer.getArray(), pnMem, static_cast< size_t >( nWriteSize ) );
114 writeData( maBuffer, nAtomSize );
115 pnMem += nWriteSize;
116 nBytes -= nWriteSize;
117 }
118 }
119 }
120
121 // ============================================================================
122
SequenceOutputStream(StreamDataSequence & rData)123 SequenceOutputStream::SequenceOutputStream( StreamDataSequence& rData ) :
124 BinaryStreamBase( true ),
125 SequenceSeekableStream( rData )
126 {
127 }
128
writeData(const StreamDataSequence & rData,size_t nAtomSize)129 void SequenceOutputStream::writeData( const StreamDataSequence& rData, size_t nAtomSize )
130 {
131 if( mpData && rData.hasElements() )
132 writeMemory( rData.getConstArray(), rData.getLength(), nAtomSize );
133 }
134
writeMemory(const void * pMem,sal_Int32 nBytes,size_t)135 void SequenceOutputStream::writeMemory( const void* pMem, sal_Int32 nBytes, size_t /*nAtomSize*/ )
136 {
137 if( mpData && (nBytes > 0) )
138 {
139 if( mpData->getLength() - mnPos < nBytes )
140 const_cast< StreamDataSequence* >( mpData )->realloc( mnPos + nBytes );
141 memcpy( const_cast< StreamDataSequence* >( mpData )->getArray() + mnPos, pMem, static_cast< size_t >( nBytes ) );
142 mnPos += nBytes;
143 }
144 }
145
146 // ============================================================================
147
148 } // namespace oox
149
150