Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include "oox/helper/binaryoutputstream.hxx"
21 :
22 : #include <com/sun/star/io/XOutputStream.hpp>
23 : #include <com/sun/star/io/XSeekable.hpp>
24 : #include <osl/diagnose.h>
25 : #include <string.h>
26 :
27 : namespace oox {
28 :
29 : using namespace ::com::sun::star::io;
30 : using namespace ::com::sun::star::uno;
31 :
32 : namespace {
33 :
34 : const sal_Int32 OUTPUTSTREAM_BUFFERSIZE = 0x8000;
35 :
36 : } // namespace
37 :
38 3688 : BinaryXOutputStream::BinaryXOutputStream( const Reference< XOutputStream >& rxOutStrm, bool bAutoClose ) :
39 7376 : BinaryStreamBase( Reference< XSeekable >( rxOutStrm, UNO_QUERY ).is() ),
40 : BinaryXSeekableStream( Reference< XSeekable >( rxOutStrm, UNO_QUERY ) ),
41 : maBuffer( OUTPUTSTREAM_BUFFERSIZE ),
42 : mxOutStrm( rxOutStrm ),
43 7376 : mbAutoClose( bAutoClose && rxOutStrm.is() )
44 : {
45 3688 : mbEof = !mxOutStrm.is();
46 3688 : }
47 :
48 7376 : BinaryXOutputStream::~BinaryXOutputStream()
49 : {
50 3688 : close();
51 3688 : }
52 :
53 3688 : void BinaryXOutputStream::close()
54 : {
55 : OSL_ENSURE( !mbAutoClose || mxOutStrm.is(), "BinaryXOutputStream::close - invalid call" );
56 3688 : if( mxOutStrm.is() ) try
57 : {
58 3688 : mxOutStrm->flush();
59 3688 : if ( mbAutoClose )
60 882 : mxOutStrm->closeOutput();
61 : }
62 0 : catch( Exception& )
63 : {
64 : OSL_FAIL( "BinaryXOutputStream::close - closing output stream failed" );
65 : }
66 3688 : mxOutStrm.clear();
67 3688 : mbAutoClose = false;
68 3688 : BinaryXSeekableStream::close();
69 3688 : }
70 :
71 3778 : void BinaryXOutputStream::writeData( const StreamDataSequence& rData, size_t /*nAtomSize*/ )
72 : {
73 3778 : if( mxOutStrm.is() ) try
74 : {
75 3778 : mxOutStrm->writeBytes( rData );
76 : }
77 0 : catch( Exception& )
78 : {
79 : OSL_FAIL( "BinaryXOutputStream::writeData - stream read error" );
80 : }
81 3778 : }
82 :
83 58 : void BinaryXOutputStream::writeMemory( const void* pMem, sal_Int32 nBytes, size_t nAtomSize )
84 : {
85 58 : if( mxOutStrm.is() && (nBytes > 0) )
86 : {
87 58 : sal_Int32 nBufferSize = getLimitedValue< sal_Int32, sal_Int32 >( nBytes, 0, (OUTPUTSTREAM_BUFFERSIZE / nAtomSize) * nAtomSize );
88 58 : const sal_uInt8* pnMem = reinterpret_cast< const sal_uInt8* >( pMem );
89 174 : while( nBytes > 0 )
90 : {
91 58 : sal_Int32 nWriteSize = getLimitedValue< sal_Int32, sal_Int32 >( nBytes, 0, nBufferSize );
92 58 : maBuffer.realloc( nWriteSize );
93 58 : memcpy( maBuffer.getArray(), pnMem, static_cast< size_t >( nWriteSize ) );
94 58 : writeData( maBuffer, nAtomSize );
95 58 : pnMem += nWriteSize;
96 58 : nBytes -= nWriteSize;
97 : }
98 : }
99 58 : }
100 :
101 : void
102 4 : BinaryOutputStream::writeCharArrayUC( const OUString& rString, rtl_TextEncoding eTextEnc, bool bAllowNulChars )
103 : {
104 4 : OString sBuf( OUStringToOString( rString, eTextEnc ) );
105 4 : if( !bAllowNulChars )
106 4 : sBuf = sBuf.replace( '\0', '?' );
107 4 : writeMemory( static_cast< const void* >( sBuf.getStr() ), sBuf.getLength() );
108 4 : }
109 :
110 : void
111 2 : BinaryOutputStream::writeUnicodeArray( const OUString& rString, bool bAllowNulChars )
112 : {
113 2 : OUString sBuf( rString );
114 2 : if( !bAllowNulChars )
115 2 : sBuf = sBuf.replace( '\0', '?' );
116 : #ifdef OSL_BIGENDIAN
117 : // need a non-const buffer for swapping byte order
118 : sal_Unicode notConst[sBuf.getLength()];
119 : memcpy( notConst, sBuf.getStr(), sizeof(sal_Unicode)*sBuf.getLength() );
120 : writeArray( notConst, sBuf.getLength() );
121 : #else
122 2 : writeArray( sBuf.getStr(), sBuf.getLength() );
123 : #endif
124 2 : }
125 :
126 : void
127 4 : BinaryOutputStream::writeCompressedUnicodeArray( const OUString& rString, bool bCompressed, bool bAllowNulChars )
128 : {
129 4 : if ( bCompressed )
130 : // ISO-8859-1 maps all byte values 0xHH to the same Unicode code point U+00HH
131 4 : writeCharArrayUC( rString, RTL_TEXTENCODING_ISO_8859_1, bAllowNulChars );
132 : else
133 0 : writeUnicodeArray( rString, bAllowNulChars );
134 4 : }
135 :
136 198 : SequenceOutputStream::SequenceOutputStream( StreamDataSequence& rData ) :
137 : BinaryStreamBase( true ),
138 198 : SequenceSeekableStream( rData )
139 : {
140 198 : }
141 :
142 342 : void SequenceOutputStream::writeData( const StreamDataSequence& rData, size_t nAtomSize )
143 : {
144 342 : if( mpData && rData.hasElements() )
145 342 : writeMemory( rData.getConstArray(), rData.getLength(), nAtomSize );
146 342 : }
147 :
148 342 : void SequenceOutputStream::writeMemory( const void* pMem, sal_Int32 nBytes, size_t /*nAtomSize*/ )
149 : {
150 342 : if( mpData && (nBytes > 0) )
151 : {
152 342 : if( mpData->getLength() - mnPos < nBytes )
153 342 : const_cast< StreamDataSequence* >( mpData )->realloc( mnPos + nBytes );
154 342 : memcpy( const_cast< StreamDataSequence* >( mpData )->getArray() + mnPos, pMem, static_cast< size_t >( nBytes ) );
155 342 : mnPos += nBytes;
156 : }
157 342 : }
158 :
159 : } // namespace oox
160 :
161 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|