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