Version 5.4.3.2, tag libreoffice-5.4.3.2
[LibreOffice.git] / oox / source / helper / binaryoutputstream.cxx
blobc6d0f50f67495d460cd08d045a6a825bd70e7e18
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include "oox/helper/binaryoutputstream.hxx"
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>
27 namespace oox {
29 using namespace ::com::sun::star::io;
30 using namespace ::com::sun::star::uno;
32 namespace {
34 const sal_Int32 OUTPUTSTREAM_BUFFERSIZE = 0x8000;
36 } // namespace
38 BinaryXOutputStream::BinaryXOutputStream( const Reference< XOutputStream >& rxOutStrm, bool bAutoClose ) :
39 BinaryStreamBase( Reference< XSeekable >( rxOutStrm, UNO_QUERY ).is() ),
40 BinaryXSeekableStream( Reference< XSeekable >( rxOutStrm, UNO_QUERY ) ),
41 maBuffer( OUTPUTSTREAM_BUFFERSIZE ),
42 mxOutStrm( rxOutStrm ),
43 mbAutoClose( bAutoClose && rxOutStrm.is() )
45 mbEof = !mxOutStrm.is();
48 BinaryXOutputStream::~BinaryXOutputStream()
50 close();
53 void BinaryXOutputStream::close()
55 OSL_ENSURE( !mbAutoClose || mxOutStrm.is(), "BinaryXOutputStream::close - invalid call" );
56 if( mxOutStrm.is() ) try
58 mxOutStrm->flush();
59 if ( mbAutoClose )
60 mxOutStrm->closeOutput();
62 catch( Exception& )
64 OSL_FAIL( "BinaryXOutputStream::close - closing output stream failed" );
66 mxOutStrm.clear();
67 mbAutoClose = false;
68 BinaryXSeekableStream::close();
71 void BinaryXOutputStream::writeData( const StreamDataSequence& rData, size_t /*nAtomSize*/ )
73 if( mxOutStrm.is() ) try
75 mxOutStrm->writeBytes( rData );
77 catch( Exception& )
79 OSL_FAIL( "BinaryXOutputStream::writeData - stream read error" );
83 void BinaryXOutputStream::writeMemory( const void* pMem, sal_Int32 nBytes, size_t nAtomSize )
85 if( mxOutStrm.is() && (nBytes > 0) )
87 sal_Int32 nBufferSize = getLimitedValue< sal_Int32, sal_Int32 >( nBytes, 0, (OUTPUTSTREAM_BUFFERSIZE / nAtomSize) * nAtomSize );
88 const sal_uInt8* pnMem = static_cast< const sal_uInt8* >( pMem );
89 while( nBytes > 0 )
91 sal_Int32 nWriteSize = getLimitedValue< sal_Int32, sal_Int32 >( nBytes, 0, nBufferSize );
92 maBuffer.realloc( nWriteSize );
93 memcpy( maBuffer.getArray(), pnMem, static_cast< size_t >( nWriteSize ) );
94 writeData( maBuffer, nAtomSize );
95 pnMem += nWriteSize;
96 nBytes -= nWriteSize;
101 void
102 BinaryOutputStream::writeCharArrayUC( const OUString& rString, rtl_TextEncoding eTextEnc )
104 OString sBuf( OUStringToOString( rString, eTextEnc ) );
105 sBuf = sBuf.replace( '\0', '?' );
106 writeMemory( static_cast< const void* >( sBuf.getStr() ), sBuf.getLength() );
109 void
110 BinaryOutputStream::writeUnicodeArray( const OUString& rString )
112 OUString sBuf( rString );
113 sBuf = sBuf.replace( '\0', '?' );
114 #ifdef OSL_BIGENDIAN
115 // need a non-const buffer for swapping byte order
116 sal_Unicode notConst[sBuf.getLength()];
117 memcpy( notConst, sBuf.getStr(), sizeof(sal_Unicode)*sBuf.getLength() );
118 writeArray( notConst, sBuf.getLength() );
119 #else
120 writeArray( sBuf.getStr(), sBuf.getLength() );
121 #endif
124 void BinaryOutputStream::writeCompressedUnicodeArray( const OUString& rString, bool bCompressed )
126 if ( bCompressed )
127 // ISO-8859-1 maps all byte values 0xHH to the same Unicode code point U+00HH
128 writeCharArrayUC( rString, RTL_TEXTENCODING_ISO_8859_1 );
129 else
130 writeUnicodeArray( rString );
133 SequenceOutputStream::SequenceOutputStream( StreamDataSequence& rData ) :
134 BinaryStreamBase( true ),
135 SequenceSeekableStream( rData )
139 void SequenceOutputStream::writeData( const StreamDataSequence& rData, size_t nAtomSize )
141 if( mpData && rData.hasElements() )
142 writeMemory( rData.getConstArray(), rData.getLength(), nAtomSize );
145 void SequenceOutputStream::writeMemory( const void* pMem, sal_Int32 nBytes, size_t /*nAtomSize*/ )
147 if( mpData && (nBytes > 0) )
149 if( mpData->getLength() - mnPos < nBytes )
150 const_cast< StreamDataSequence* >( mpData )->realloc( mnPos + nBytes );
151 memcpy( const_cast< StreamDataSequence* >( mpData )->getArray() + mnPos, pMem, static_cast< size_t >( nBytes ) );
152 mnPos += nBytes;
156 } // namespace oox
158 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */