2010-10-12 08:56:41 -05:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2012-07-16 11:32:30 -05:00
|
|
|
/*
|
|
|
|
* This file is part of the LibreOffice project.
|
|
|
|
*
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
*
|
|
|
|
* This file incorporates work covered by the following license notice:
|
|
|
|
*
|
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed
|
|
|
|
* with this work for additional information regarding copyright
|
|
|
|
* ownership. The ASF licenses this file to you under the Apache
|
|
|
|
* License, Version 2.0 (the "License"); you may not use this file
|
|
|
|
* except in compliance with the License. You may obtain a copy of
|
|
|
|
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
|
|
|
|
*/
|
2008-01-17 01:06:10 -06:00
|
|
|
|
|
|
|
#include "oox/helper/binaryoutputstream.hxx"
|
2010-08-05 09:01:52 -05:00
|
|
|
|
2011-02-07 10:18:11 -06:00
|
|
|
#include <com/sun/star/io/XOutputStream.hpp>
|
|
|
|
#include <com/sun/star/io/XSeekable.hpp>
|
2008-01-17 01:06:10 -06:00
|
|
|
#include <osl/diagnose.h>
|
2008-02-04 06:36:34 -06:00
|
|
|
#include <string.h>
|
2008-01-17 01:06:10 -06:00
|
|
|
|
|
|
|
namespace oox {
|
|
|
|
|
2014-02-25 12:37:34 -06:00
|
|
|
|
2010-08-05 09:01:52 -05:00
|
|
|
|
|
|
|
using namespace ::com::sun::star::io;
|
|
|
|
using namespace ::com::sun::star::uno;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2008-09-30 08:51:36 -05:00
|
|
|
const sal_Int32 OUTPUTSTREAM_BUFFERSIZE = 0x8000;
|
|
|
|
|
2010-08-05 09:01:52 -05:00
|
|
|
} // namespace
|
|
|
|
|
2014-02-25 12:37:34 -06:00
|
|
|
|
2008-09-30 08:51:36 -05:00
|
|
|
|
|
|
|
BinaryXOutputStream::BinaryXOutputStream( const Reference< XOutputStream >& rxOutStrm, bool bAutoClose ) :
|
2011-02-07 10:18:11 -06:00
|
|
|
BinaryStreamBase( Reference< XSeekable >( rxOutStrm, UNO_QUERY ).is() ),
|
2008-09-30 08:51:36 -05:00
|
|
|
BinaryXSeekableStream( Reference< XSeekable >( rxOutStrm, UNO_QUERY ) ),
|
|
|
|
maBuffer( OUTPUTSTREAM_BUFFERSIZE ),
|
2008-01-17 01:06:10 -06:00
|
|
|
mxOutStrm( rxOutStrm ),
|
2011-02-28 08:24:56 -06:00
|
|
|
mbAutoClose( bAutoClose && rxOutStrm.is() )
|
2008-01-17 01:06:10 -06:00
|
|
|
{
|
2008-09-30 08:51:36 -05:00
|
|
|
mbEof = !mxOutStrm.is();
|
2008-01-17 01:06:10 -06:00
|
|
|
}
|
|
|
|
|
2008-09-30 08:51:36 -05:00
|
|
|
BinaryXOutputStream::~BinaryXOutputStream()
|
2008-01-17 01:06:10 -06:00
|
|
|
{
|
2011-02-07 10:18:11 -06:00
|
|
|
close();
|
|
|
|
}
|
|
|
|
|
|
|
|
void BinaryXOutputStream::close()
|
|
|
|
{
|
2011-02-28 08:24:56 -06:00
|
|
|
OSL_ENSURE( !mbAutoClose || mxOutStrm.is(), "BinaryXOutputStream::close - invalid call" );
|
2011-02-07 10:18:11 -06:00
|
|
|
if( mxOutStrm.is() ) try
|
|
|
|
{
|
|
|
|
mxOutStrm->flush();
|
2013-06-11 05:29:21 -05:00
|
|
|
if ( mbAutoClose )
|
|
|
|
mxOutStrm->closeOutput();
|
2011-02-07 10:18:11 -06:00
|
|
|
}
|
|
|
|
catch( Exception& )
|
|
|
|
{
|
2011-04-26 18:31:35 -05:00
|
|
|
OSL_FAIL( "BinaryXOutputStream::close - closing output stream failed" );
|
2011-02-07 10:18:11 -06:00
|
|
|
}
|
|
|
|
mxOutStrm.clear();
|
2011-02-28 08:24:56 -06:00
|
|
|
mbAutoClose = false;
|
2011-02-07 10:18:11 -06:00
|
|
|
BinaryXSeekableStream::close();
|
2008-01-17 01:06:10 -06:00
|
|
|
}
|
|
|
|
|
2011-02-07 10:18:11 -06:00
|
|
|
void BinaryXOutputStream::writeData( const StreamDataSequence& rData, size_t /*nAtomSize*/ )
|
2008-01-17 01:06:10 -06:00
|
|
|
{
|
2011-02-07 10:18:11 -06:00
|
|
|
if( mxOutStrm.is() ) try
|
2008-01-17 01:06:10 -06:00
|
|
|
{
|
2008-09-30 08:51:36 -05:00
|
|
|
mxOutStrm->writeBytes( rData );
|
2008-01-17 01:06:10 -06:00
|
|
|
}
|
|
|
|
catch( Exception& )
|
|
|
|
{
|
2011-03-12 04:43:16 -06:00
|
|
|
OSL_FAIL( "BinaryXOutputStream::writeData - stream read error" );
|
2008-01-17 01:06:10 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-07 10:18:11 -06:00
|
|
|
void BinaryXOutputStream::writeMemory( const void* pMem, sal_Int32 nBytes, size_t nAtomSize )
|
2008-01-17 01:06:10 -06:00
|
|
|
{
|
2011-02-07 10:18:11 -06:00
|
|
|
if( mxOutStrm.is() && (nBytes > 0) )
|
2008-01-17 01:06:10 -06:00
|
|
|
{
|
2011-02-07 10:18:11 -06:00
|
|
|
sal_Int32 nBufferSize = getLimitedValue< sal_Int32, sal_Int32 >( nBytes, 0, (OUTPUTSTREAM_BUFFERSIZE / nAtomSize) * nAtomSize );
|
2008-09-30 08:51:36 -05:00
|
|
|
const sal_uInt8* pnMem = reinterpret_cast< const sal_uInt8* >( pMem );
|
2008-01-17 01:06:10 -06:00
|
|
|
while( nBytes > 0 )
|
|
|
|
{
|
2008-09-30 08:51:36 -05:00
|
|
|
sal_Int32 nWriteSize = getLimitedValue< sal_Int32, sal_Int32 >( nBytes, 0, nBufferSize );
|
|
|
|
maBuffer.realloc( nWriteSize );
|
|
|
|
memcpy( maBuffer.getArray(), pnMem, static_cast< size_t >( nWriteSize ) );
|
2011-02-07 10:18:11 -06:00
|
|
|
writeData( maBuffer, nAtomSize );
|
2008-09-30 08:51:36 -05:00
|
|
|
pnMem += nWriteSize;
|
|
|
|
nBytes -= nWriteSize;
|
2008-01-17 01:06:10 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-09-23 11:31:46 -05:00
|
|
|
|
|
|
|
void
|
|
|
|
BinaryOutputStream::writeCharArrayUC( const OUString& rString, rtl_TextEncoding eTextEnc, bool bAllowNulChars )
|
|
|
|
{
|
|
|
|
OString sBuf( OUStringToOString( rString, eTextEnc ) );
|
|
|
|
if( !bAllowNulChars )
|
2012-11-28 14:55:26 -06:00
|
|
|
sBuf = sBuf.replace( '\0', '?' );
|
2011-09-23 11:31:46 -05:00
|
|
|
writeMemory( static_cast< const void* >( sBuf.getStr() ), sBuf.getLength() );
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-04-07 05:06:47 -05:00
|
|
|
BinaryOutputStream::writeUnicodeArray( const OUString& rString, bool bAllowNulChars )
|
2011-09-23 11:31:46 -05:00
|
|
|
{
|
|
|
|
OUString sBuf( rString );
|
|
|
|
if( !bAllowNulChars )
|
2012-11-28 14:55:26 -06:00
|
|
|
sBuf = sBuf.replace( '\0', '?' );
|
2011-11-08 12:32:48 -06:00
|
|
|
#ifdef OSL_BIGENDIAN
|
|
|
|
// need a non-const buffer for swapping byte order
|
|
|
|
sal_Unicode notConst[sBuf.getLength()];
|
|
|
|
memcpy( notConst, sBuf.getStr(), sizeof(sal_Unicode)*sBuf.getLength() );
|
|
|
|
writeArray( notConst, sBuf.getLength() );
|
|
|
|
#else
|
2011-09-23 11:31:46 -05:00
|
|
|
writeArray( sBuf.getStr(), sBuf.getLength() );
|
2011-11-08 12:32:48 -06:00
|
|
|
#endif
|
2011-09-23 11:31:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-04-07 05:06:47 -05:00
|
|
|
BinaryOutputStream::writeCompressedUnicodeArray( const OUString& rString, bool bCompressed, bool bAllowNulChars )
|
2011-09-23 11:31:46 -05:00
|
|
|
{
|
|
|
|
if ( bCompressed )
|
|
|
|
// ISO-8859-1 maps all byte values 0xHH to the same Unicode code point U+00HH
|
|
|
|
writeCharArrayUC( rString, RTL_TEXTENCODING_ISO_8859_1, bAllowNulChars );
|
|
|
|
else
|
|
|
|
writeUnicodeArray( rString, bAllowNulChars );
|
|
|
|
}
|
2008-01-17 01:06:10 -06:00
|
|
|
|
2014-02-25 12:37:34 -06:00
|
|
|
|
2008-09-30 08:51:36 -05:00
|
|
|
|
|
|
|
SequenceOutputStream::SequenceOutputStream( StreamDataSequence& rData ) :
|
2011-02-07 10:18:11 -06:00
|
|
|
BinaryStreamBase( true ),
|
2008-09-30 08:51:36 -05:00
|
|
|
SequenceSeekableStream( rData )
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2011-02-07 10:18:11 -06:00
|
|
|
void SequenceOutputStream::writeData( const StreamDataSequence& rData, size_t nAtomSize )
|
2008-09-30 08:51:36 -05:00
|
|
|
{
|
2011-02-07 10:18:11 -06:00
|
|
|
if( mpData && rData.hasElements() )
|
|
|
|
writeMemory( rData.getConstArray(), rData.getLength(), nAtomSize );
|
2008-09-30 08:51:36 -05:00
|
|
|
}
|
|
|
|
|
2011-02-07 10:18:11 -06:00
|
|
|
void SequenceOutputStream::writeMemory( const void* pMem, sal_Int32 nBytes, size_t /*nAtomSize*/ )
|
2008-09-30 08:51:36 -05:00
|
|
|
{
|
2011-02-07 10:18:11 -06:00
|
|
|
if( mpData && (nBytes > 0) )
|
2008-09-30 08:51:36 -05:00
|
|
|
{
|
2011-02-07 10:18:11 -06:00
|
|
|
if( mpData->getLength() - mnPos < nBytes )
|
|
|
|
const_cast< StreamDataSequence* >( mpData )->realloc( mnPos + nBytes );
|
|
|
|
memcpy( const_cast< StreamDataSequence* >( mpData )->getArray() + mnPos, pMem, static_cast< size_t >( nBytes ) );
|
2008-09-30 08:51:36 -05:00
|
|
|
mnPos += nBytes;
|
2008-01-17 01:06:10 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-25 12:37:34 -06:00
|
|
|
|
2008-01-17 01:06:10 -06:00
|
|
|
|
|
|
|
} // namespace oox
|
|
|
|
|
2010-10-12 08:56:41 -05:00
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|