Branch data 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 : 589 : BinaryXOutputStream::BinaryXOutputStream( const Reference< XOutputStream >& rxOutStrm, bool bAutoClose ) :
47 : 1178 : BinaryStreamBase( Reference< XSeekable >( rxOutStrm, UNO_QUERY ).is() ),
48 : : BinaryXSeekableStream( Reference< XSeekable >( rxOutStrm, UNO_QUERY ) ),
49 : : maBuffer( OUTPUTSTREAM_BUFFERSIZE ),
50 : : mxOutStrm( rxOutStrm ),
51 [ + - ][ + - ]: 1178 : mbAutoClose( bAutoClose && rxOutStrm.is() )
[ + - ][ + + ]
[ + - ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ + - ]
52 : : {
53 : 589 : mbEof = !mxOutStrm.is();
54 : 589 : }
55 : :
56 [ + - ][ + - ]: 1178 : BinaryXOutputStream::~BinaryXOutputStream()
[ + - ][ # # ]
[ # # ][ # # ]
57 : : {
58 [ + - ][ # # ]: 589 : close();
59 [ + - ][ - + ]: 1178 : }
[ # # ][ # # ]
[ # # ][ # # ]
60 : :
61 : 589 : void BinaryXOutputStream::close()
62 : : {
63 : : OSL_ENSURE( !mbAutoClose || mxOutStrm.is(), "BinaryXOutputStream::close - invalid call" );
64 [ + - ]: 589 : if( mxOutStrm.is() ) try
65 : : {
66 [ + - ][ + - ]: 589 : mxOutStrm->flush();
67 [ + - ][ + - ]: 589 : mxOutStrm->closeOutput();
68 : : }
69 : 0 : catch( Exception& )
70 : : {
71 : : OSL_FAIL( "BinaryXOutputStream::close - closing output stream failed" );
72 : : }
73 : 589 : mxOutStrm.clear();
74 : 589 : mbAutoClose = false;
75 : 589 : BinaryXSeekableStream::close();
76 [ # # ]: 589 : }
77 : :
78 : 879 : void BinaryXOutputStream::writeData( const StreamDataSequence& rData, size_t /*nAtomSize*/ )
79 : : {
80 [ + - ]: 879 : if( mxOutStrm.is() ) try
81 : : {
82 [ + - ][ + - ]: 879 : mxOutStrm->writeBytes( rData );
83 : : }
84 : 0 : catch( Exception& )
85 : : {
86 : : OSL_FAIL( "BinaryXOutputStream::writeData - stream read error" );
87 : : }
88 [ # # ]: 879 : }
89 : :
90 : 185 : void BinaryXOutputStream::writeMemory( const void* pMem, sal_Int32 nBytes, size_t nAtomSize )
91 : : {
92 [ + - ][ + + ]: 185 : if( mxOutStrm.is() && (nBytes > 0) )
[ + + ]
93 : : {
94 : 175 : sal_Int32 nBufferSize = getLimitedValue< sal_Int32, sal_Int32 >( nBytes, 0, (OUTPUTSTREAM_BUFFERSIZE / nAtomSize) * nAtomSize );
95 : 175 : const sal_uInt8* pnMem = reinterpret_cast< const sal_uInt8* >( pMem );
96 [ + + ]: 350 : while( nBytes > 0 )
97 : : {
98 : 175 : sal_Int32 nWriteSize = getLimitedValue< sal_Int32, sal_Int32 >( nBytes, 0, nBufferSize );
99 : 175 : maBuffer.realloc( nWriteSize );
100 : 175 : memcpy( maBuffer.getArray(), pnMem, static_cast< size_t >( nWriteSize ) );
101 : 175 : writeData( maBuffer, nAtomSize );
102 : 175 : pnMem += nWriteSize;
103 : 175 : nBytes -= nWriteSize;
104 : : }
105 : : }
106 : 185 : }
107 : :
108 : : void
109 : 20 : BinaryOutputStream::writeCharArrayUC( const OUString& rString, rtl_TextEncoding eTextEnc, bool bAllowNulChars )
110 : : {
111 [ + - ]: 20 : OString sBuf( OUStringToOString( rString, eTextEnc ) );
112 [ + - ]: 20 : if( !bAllowNulChars )
113 : 20 : sBuf.replace( '\0', '?' );
114 [ + - ]: 20 : writeMemory( static_cast< const void* >( sBuf.getStr() ), sBuf.getLength() );
115 : 20 : }
116 : :
117 : : void
118 : 5 : BinaryOutputStream::writeUnicodeArray( const ::rtl::OUString& rString, bool bAllowNulChars )
119 : : {
120 : 5 : OUString sBuf( rString );
121 [ + - ]: 5 : if( !bAllowNulChars )
122 : 5 : 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 [ + - ]: 5 : writeArray( sBuf.getStr(), sBuf.getLength() );
130 : : #endif
131 : 5 : }
132 : :
133 : : void
134 : 20 : BinaryOutputStream::writeCompressedUnicodeArray( const rtl::OUString& rString, bool bCompressed, bool bAllowNulChars )
135 : : {
136 [ + - ]: 20 : if ( bCompressed )
137 : : // ISO-8859-1 maps all byte values 0xHH to the same Unicode code point U+00HH
138 : 20 : writeCharArrayUC( rString, RTL_TEXTENCODING_ISO_8859_1, bAllowNulChars );
139 : : else
140 : 0 : writeUnicodeArray( rString, bAllowNulChars );
141 : 20 : }
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: */
|