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 #ifndef INCLUDED_OOX_HELPER_BINARYOUTPUTSTREAM_HXX
21 #define INCLUDED_OOX_HELPER_BINARYOUTPUTSTREAM_HXX
26 #include <com/sun/star/uno/Reference.hxx>
27 #include <oox/dllapi.h>
28 #include <oox/helper/binarystreambase.hxx>
29 #include <oox/helper/helper.hxx>
30 #include <rtl/textenc.h>
31 #include <rtl/ustring.hxx>
32 #include <sal/types.h>
34 namespace com::sun::star
{
35 namespace io
{ class XOutputStream
; }
41 /** Interface for binary output stream classes.
43 The binary data in the stream is written in little-endian format.
45 class BinaryOutputStream
: public virtual BinaryStreamBase
48 /** Derived classes implement writing the contents of the passed data
52 The size of the elements in the memory block, if available. Derived
53 classes may be interested in this information.
55 virtual void writeData( const StreamDataSequence
& rData
, size_t nAtomSize
= 1 ) = 0;
57 /** Derived classes implement writing the contents of the (preallocated!)
61 The size of the elements in the memory block, if available. Derived
62 classes may be interested in this information.
64 virtual void writeMemory( const void* pMem
, sal_Int32 nBytes
, size_t nAtomSize
= 1 ) = 0;
66 template< typename Type
>
67 void writeArray( Type
* opnArray
, sal_Int32 nElemCount
);
69 template< typename Type
>
70 void writeArray( const Type
* opnArray
, sal_Int32 nElemCount
);
72 /** Writes a value to the stream and converts it to platform byte order.
73 All data types supported by the ByteOrderConverter class can be used.
75 template< typename Type
>
76 void writeValue( Type nValue
);
78 BinaryOutputStream
& WriteInt16(sal_Int16 x
) { writeValue(x
); return *this; }
79 BinaryOutputStream
& WriteUInt16(sal_uInt16 x
) { writeValue(x
); return *this; }
80 BinaryOutputStream
& WriteInt32(sal_Int32 x
) { writeValue(x
); return *this; }
81 BinaryOutputStream
& WriteUInt32(sal_uInt32 x
) { writeValue(x
); return *this; }
82 BinaryOutputStream
& WriteInt64(sal_Int64 x
) { writeValue(x
); return *this; }
84 void writeCompressedUnicodeArray( const OUString
& rString
, bool bCompressed
);
86 void writeCharArrayUC( std::u16string_view rString
, rtl_TextEncoding eTextEnc
);
88 void writeUnicodeArray( const OUString
& rString
);
91 BinaryOutputStream() = default;
94 BinaryOutputStream( BinaryOutputStream
const& ) = delete;
95 BinaryOutputStream
& operator=( BinaryOutputStream
const& ) = delete;
98 template< typename Type
>
99 void BinaryOutputStream::writeArray( Type
* opnArray
, sal_Int32 nElemCount
)
101 sal_Int32 nWriteSize
= getLimitedValue
< sal_Int32
, sal_Int32
>( nElemCount
, 0, SAL_MAX_INT32
/ sizeof( Type
) ) * sizeof( Type
);
102 ByteOrderConverter::convertLittleEndianArray( opnArray
, static_cast< size_t >( nElemCount
) );
103 writeMemory( opnArray
, nWriteSize
, sizeof( Type
) );
106 template< typename Type
>
107 void BinaryOutputStream::writeArray( const Type
* opnArray
, sal_Int32 nElemCount
)
109 std::unique_ptr
<Type
[]> xArray(new Type
[nElemCount
]);
110 std::uninitialized_copy(opnArray
, opnArray
+ nElemCount
, xArray
.get());
111 writeArray(xArray
.get(), nElemCount
);
114 template< typename Type
>
115 void BinaryOutputStream::writeValue( Type nValue
)
117 ByteOrderConverter::convertLittleEndian( nValue
);
118 writeMemory( &nValue
, static_cast< sal_Int32
>( sizeof( Type
) ), sizeof( Type
) );
122 /** Wraps a UNO output stream and provides convenient access functions.
124 The binary data in the stream is written in little-endian format.
126 class OOX_DLLPUBLIC BinaryXOutputStream final
: public BinaryXSeekableStream
, public BinaryOutputStream
129 /** Constructs the wrapper object for the passed output stream.
132 The com.sun.star.io.XOutputStream interface of the output stream to
136 True = automatically close the wrapped output stream on destruction
137 of this wrapper or when close() is called.
139 explicit BinaryXOutputStream(
140 const css::uno::Reference
< css::io::XOutputStream
>& rxOutStrm
,
143 virtual ~BinaryXOutputStream() override
;
145 /** Flushes and closes the output stream. Does also close the wrapped UNO
146 output stream if bAutoClose has been set to true in the constructor. */
147 void close() override
;
149 /** Writes the passed data sequence. */
150 virtual void writeData( const StreamDataSequence
& rData
, size_t nAtomSize
= 1 ) override
;
152 /** Write nBytes bytes from the (preallocated!) buffer pMem. */
153 virtual void writeMemory( const void* pMem
, sal_Int32 nBytes
, size_t nAtomSize
= 1 ) override
;
156 StreamDataSequence maBuffer
; ///< Data buffer used in writeMemory() function.
157 css::uno::Reference
< css::io::XOutputStream
>
158 mxOutStrm
; ///< Reference to the output stream.
159 bool mbAutoClose
; ///< True = automatically close stream on destruction.
166 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */