1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
29 // ============================================================================
31 using namespace ::com::sun::star::io
;
32 using namespace ::com::sun::star::uno
;
36 const sal_Int32 OUTPUTSTREAM_BUFFERSIZE
= 0x8000;
40 // ============================================================================
42 BinaryXOutputStream::BinaryXOutputStream( const Reference
< XOutputStream
>& rxOutStrm
, bool bAutoClose
) :
43 BinaryStreamBase( Reference
< XSeekable
>( rxOutStrm
, UNO_QUERY
).is() ),
44 BinaryXSeekableStream( Reference
< XSeekable
>( rxOutStrm
, UNO_QUERY
) ),
45 maBuffer( OUTPUTSTREAM_BUFFERSIZE
),
46 mxOutStrm( rxOutStrm
),
47 mbAutoClose( bAutoClose
&& rxOutStrm
.is() )
49 mbEof
= !mxOutStrm
.is();
52 BinaryXOutputStream::~BinaryXOutputStream()
57 void BinaryXOutputStream::close()
59 OSL_ENSURE( !mbAutoClose
|| mxOutStrm
.is(), "BinaryXOutputStream::close - invalid call" );
60 if( mxOutStrm
.is() ) try
63 mxOutStrm
->closeOutput();
67 OSL_FAIL( "BinaryXOutputStream::close - closing output stream failed" );
71 BinaryXSeekableStream::close();
74 void BinaryXOutputStream::writeData( const StreamDataSequence
& rData
, size_t /*nAtomSize*/ )
76 if( mxOutStrm
.is() ) try
78 mxOutStrm
->writeBytes( rData
);
82 OSL_FAIL( "BinaryXOutputStream::writeData - stream read error" );
86 void BinaryXOutputStream::writeMemory( const void* pMem
, sal_Int32 nBytes
, size_t nAtomSize
)
88 if( mxOutStrm
.is() && (nBytes
> 0) )
90 sal_Int32 nBufferSize
= getLimitedValue
< sal_Int32
, sal_Int32
>( nBytes
, 0, (OUTPUTSTREAM_BUFFERSIZE
/ nAtomSize
) * nAtomSize
);
91 const sal_uInt8
* pnMem
= reinterpret_cast< const sal_uInt8
* >( pMem
);
94 sal_Int32 nWriteSize
= getLimitedValue
< sal_Int32
, sal_Int32
>( nBytes
, 0, nBufferSize
);
95 maBuffer
.realloc( nWriteSize
);
96 memcpy( maBuffer
.getArray(), pnMem
, static_cast< size_t >( nWriteSize
) );
97 writeData( maBuffer
, nAtomSize
);
105 BinaryOutputStream::writeCharArrayUC( const OUString
& rString
, rtl_TextEncoding eTextEnc
, bool bAllowNulChars
)
107 OString
sBuf( OUStringToOString( rString
, eTextEnc
) );
108 if( !bAllowNulChars
)
109 sBuf
= sBuf
.replace( '\0', '?' );
110 writeMemory( static_cast< const void* >( sBuf
.getStr() ), sBuf
.getLength() );
114 BinaryOutputStream::writeUnicodeArray( const OUString
& rString
, bool bAllowNulChars
)
116 OUString
sBuf( rString
);
117 if( !bAllowNulChars
)
118 sBuf
= sBuf
.replace( '\0', '?' );
120 // need a non-const buffer for swapping byte order
121 sal_Unicode notConst
[sBuf
.getLength()];
122 memcpy( notConst
, sBuf
.getStr(), sizeof(sal_Unicode
)*sBuf
.getLength() );
123 writeArray( notConst
, sBuf
.getLength() );
125 writeArray( sBuf
.getStr(), sBuf
.getLength() );
130 BinaryOutputStream::writeCompressedUnicodeArray( const OUString
& rString
, bool bCompressed
, bool bAllowNulChars
)
133 // ISO-8859-1 maps all byte values 0xHH to the same Unicode code point U+00HH
134 writeCharArrayUC( rString
, RTL_TEXTENCODING_ISO_8859_1
, bAllowNulChars
);
136 writeUnicodeArray( rString
, bAllowNulChars
);
139 // ============================================================================
141 SequenceOutputStream::SequenceOutputStream( StreamDataSequence
& rData
) :
142 BinaryStreamBase( true ),
143 SequenceSeekableStream( rData
)
147 void SequenceOutputStream::writeData( const StreamDataSequence
& rData
, size_t nAtomSize
)
149 if( mpData
&& rData
.hasElements() )
150 writeMemory( rData
.getConstArray(), rData
.getLength(), nAtomSize
);
153 void SequenceOutputStream::writeMemory( const void* pMem
, sal_Int32 nBytes
, size_t /*nAtomSize*/ )
155 if( mpData
&& (nBytes
> 0) )
157 if( mpData
->getLength() - mnPos
< nBytes
)
158 const_cast< StreamDataSequence
* >( mpData
)->realloc( mnPos
+ nBytes
);
159 memcpy( const_cast< StreamDataSequence
* >( mpData
)->getArray() + mnPos
, pMem
, static_cast< size_t >( nBytes
) );
164 // ============================================================================
168 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */