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 : // ============================================================================
30 :
31 : using namespace ::com::sun::star::io;
32 : using namespace ::com::sun::star::uno;
33 :
34 : using ::rtl::OString;
35 : using ::rtl::OUString;
36 : using ::rtl::OUStringToOString;
37 :
38 : namespace {
39 :
40 : const sal_Int32 OUTPUTSTREAM_BUFFERSIZE = 0x8000;
41 :
42 : } // namespace
43 :
44 : // ============================================================================
45 :
46 346 : BinaryXOutputStream::BinaryXOutputStream( const Reference< XOutputStream >& rxOutStrm, bool bAutoClose ) :
47 692 : BinaryStreamBase( Reference< XSeekable >( rxOutStrm, UNO_QUERY ).is() ),
48 : BinaryXSeekableStream( Reference< XSeekable >( rxOutStrm, UNO_QUERY ) ),
49 : maBuffer( OUTPUTSTREAM_BUFFERSIZE ),
50 : mxOutStrm( rxOutStrm ),
51 692 : mbAutoClose( bAutoClose && rxOutStrm.is() )
52 : {
53 346 : mbEof = !mxOutStrm.is();
54 346 : }
55 :
56 1038 : BinaryXOutputStream::~BinaryXOutputStream()
57 : {
58 346 : close();
59 692 : }
60 :
61 346 : void BinaryXOutputStream::close()
62 : {
63 : OSL_ENSURE( !mbAutoClose || mxOutStrm.is(), "BinaryXOutputStream::close - invalid call" );
64 346 : if( mxOutStrm.is() ) try
65 : {
66 346 : mxOutStrm->flush();
67 346 : mxOutStrm->closeOutput();
68 : }
69 0 : catch( Exception& )
70 : {
71 : OSL_FAIL( "BinaryXOutputStream::close - closing output stream failed" );
72 : }
73 346 : mxOutStrm.clear();
74 346 : mbAutoClose = false;
75 346 : BinaryXSeekableStream::close();
76 346 : }
77 :
78 462 : void BinaryXOutputStream::writeData( const StreamDataSequence& rData, size_t /*nAtomSize*/ )
79 : {
80 462 : if( mxOutStrm.is() ) try
81 : {
82 462 : mxOutStrm->writeBytes( rData );
83 : }
84 0 : catch( Exception& )
85 : {
86 : OSL_FAIL( "BinaryXOutputStream::writeData - stream read error" );
87 : }
88 462 : }
89 :
90 74 : void BinaryXOutputStream::writeMemory( const void* pMem, sal_Int32 nBytes, size_t nAtomSize )
91 : {
92 74 : if( mxOutStrm.is() && (nBytes > 0) )
93 : {
94 70 : sal_Int32 nBufferSize = getLimitedValue< sal_Int32, sal_Int32 >( nBytes, 0, (OUTPUTSTREAM_BUFFERSIZE / nAtomSize) * nAtomSize );
95 70 : const sal_uInt8* pnMem = reinterpret_cast< const sal_uInt8* >( pMem );
96 210 : while( nBytes > 0 )
97 : {
98 70 : sal_Int32 nWriteSize = getLimitedValue< sal_Int32, sal_Int32 >( nBytes, 0, nBufferSize );
99 70 : maBuffer.realloc( nWriteSize );
100 70 : memcpy( maBuffer.getArray(), pnMem, static_cast< size_t >( nWriteSize ) );
101 70 : writeData( maBuffer, nAtomSize );
102 70 : pnMem += nWriteSize;
103 70 : nBytes -= nWriteSize;
104 : }
105 : }
106 74 : }
107 :
108 : void
109 8 : BinaryOutputStream::writeCharArrayUC( const OUString& rString, rtl_TextEncoding eTextEnc, bool bAllowNulChars )
110 : {
111 8 : OString sBuf( OUStringToOString( rString, eTextEnc ) );
112 8 : if( !bAllowNulChars )
113 8 : sBuf = sBuf.replace( '\0', '?' );
114 8 : writeMemory( static_cast< const void* >( sBuf.getStr() ), sBuf.getLength() );
115 8 : }
116 :
117 : void
118 2 : BinaryOutputStream::writeUnicodeArray( const ::rtl::OUString& rString, bool bAllowNulChars )
119 : {
120 2 : OUString sBuf( rString );
121 2 : if( !bAllowNulChars )
122 2 : sBuf = sBuf.replace( '\0', '?' );
123 : #ifdef OSL_BIGENDIAN
124 : // need a non-const buffer for swapping byte order
125 : sal_Unicode notConst[sBuf.getLength()];
126 : memcpy( notConst, sBuf.getStr(), sizeof(sal_Unicode)*sBuf.getLength() );
127 : writeArray( notConst, sBuf.getLength() );
128 : #else
129 2 : writeArray( sBuf.getStr(), sBuf.getLength() );
130 : #endif
131 2 : }
132 :
133 : void
134 8 : BinaryOutputStream::writeCompressedUnicodeArray( const rtl::OUString& rString, bool bCompressed, bool bAllowNulChars )
135 : {
136 8 : if ( bCompressed )
137 : // ISO-8859-1 maps all byte values 0xHH to the same Unicode code point U+00HH
138 8 : writeCharArrayUC( rString, RTL_TEXTENCODING_ISO_8859_1, bAllowNulChars );
139 : else
140 0 : writeUnicodeArray( rString, bAllowNulChars );
141 8 : }
142 :
143 : // ============================================================================
144 :
145 0 : SequenceOutputStream::SequenceOutputStream( StreamDataSequence& rData ) :
146 : BinaryStreamBase( true ),
147 0 : SequenceSeekableStream( rData )
148 : {
149 0 : }
150 :
151 0 : void SequenceOutputStream::writeData( const StreamDataSequence& rData, size_t nAtomSize )
152 : {
153 0 : if( mpData && rData.hasElements() )
154 0 : writeMemory( rData.getConstArray(), rData.getLength(), nAtomSize );
155 0 : }
156 :
157 0 : void SequenceOutputStream::writeMemory( const void* pMem, sal_Int32 nBytes, size_t /*nAtomSize*/ )
158 : {
159 0 : if( mpData && (nBytes > 0) )
160 : {
161 0 : if( mpData->getLength() - mnPos < nBytes )
162 0 : const_cast< StreamDataSequence* >( mpData )->realloc( mnPos + nBytes );
163 0 : memcpy( const_cast< StreamDataSequence* >( mpData )->getArray() + mnPos, pMem, static_cast< size_t >( nBytes ) );
164 0 : mnPos += nBytes;
165 : }
166 0 : }
167 :
168 : // ============================================================================
169 :
170 : } // namespace oox
171 :
172 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|