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 /** This dummy default c'tor will never call the c'tor of the virtual base
92 class BinaryStreamBase as this class cannot be instantiated directly. */
93 BinaryOutputStream() : BinaryStreamBase( false ) {}
96 BinaryOutputStream( BinaryOutputStream
const& ) = delete;
97 BinaryOutputStream
& operator=( BinaryOutputStream
const& ) = delete;
100 template< typename Type
>
101 void BinaryOutputStream::writeArray( Type
* opnArray
, sal_Int32 nElemCount
)
103 sal_Int32 nWriteSize
= getLimitedValue
< sal_Int32
, sal_Int32
>( nElemCount
, 0, SAL_MAX_INT32
/ sizeof( Type
) ) * sizeof( Type
);
104 ByteOrderConverter::convertLittleEndianArray( opnArray
, static_cast< size_t >( nElemCount
) );
105 writeMemory( opnArray
, nWriteSize
, sizeof( Type
) );
108 template< typename Type
>
109 void BinaryOutputStream::writeArray( const Type
* opnArray
, sal_Int32 nElemCount
)
111 std::unique_ptr
<Type
[]> xArray(new Type
[nElemCount
]);
112 std::uninitialized_copy(opnArray
, opnArray
+ nElemCount
, xArray
.get());
113 writeArray(xArray
.get(), nElemCount
);
116 template< typename Type
>
117 void BinaryOutputStream::writeValue( Type nValue
)
119 ByteOrderConverter::convertLittleEndian( nValue
);
120 writeMemory( &nValue
, static_cast< sal_Int32
>( sizeof( Type
) ), sizeof( Type
) );
124 /** Wraps a UNO output stream and provides convenient access functions.
126 The binary data in the stream is written in little-endian format.
128 class OOX_DLLPUBLIC BinaryXOutputStream final
: public BinaryXSeekableStream
, public BinaryOutputStream
131 /** Constructs the wrapper object for the passed output stream.
134 The com.sun.star.io.XOutputStream interface of the output stream to
138 True = automatically close the wrapped output stream on destruction
139 of this wrapper or when close() is called.
141 explicit BinaryXOutputStream(
142 const css::uno::Reference
< css::io::XOutputStream
>& rxOutStrm
,
145 virtual ~BinaryXOutputStream() override
;
147 /** Flushes and closes the output stream. Does also close the wrapped UNO
148 output stream if bAutoClose has been set to true in the constructor. */
149 void close() override
;
151 /** Writes the passed data sequence. */
152 virtual void writeData( const StreamDataSequence
& rData
, size_t nAtomSize
= 1 ) override
;
154 /** Write nBytes bytes from the (preallocated!) buffer pMem. */
155 virtual void writeMemory( const void* pMem
, sal_Int32 nBytes
, size_t nAtomSize
= 1 ) override
;
158 StreamDataSequence maBuffer
; ///< Data buffer used in writeMemory() function.
159 css::uno::Reference
< css::io::XOutputStream
>
160 mxOutStrm
; ///< Reference to the output stream.
161 bool mbAutoClose
; ///< True = automatically close stream on destruction.
165 /** Wraps a StreamDataSequence and provides convenient access functions.
167 The binary data in the stream is written in little-endian format. After
168 construction, the stream points to the beginning of the passed data
169 sequence. The data sequence is expanded automatically while writing to it.
171 class SequenceOutputStream final
: public BinaryOutputStream
174 /** Constructs the wrapper object for the passed data sequence.
177 The passed data sequence MUST live at least as long as this stream
178 wrapper. The data sequence MUST NOT be changed from outside as long
179 as this stream wrapper is used to write to it.
181 explicit SequenceOutputStream( StreamDataSequence
& rData
);
183 /** Writes the passed data sequence. */
184 virtual void writeData( const StreamDataSequence
& rData
, size_t nAtomSize
= 1 ) override
;
186 /** Write nBytes bytes from the (preallocated!) buffer pMem. */
187 virtual void writeMemory( const void* pMem
, sal_Int32 nBytes
, size_t nAtomSize
= 1 ) override
;
189 /** Returns the size of the wrapped data sequence. */
190 virtual sal_Int64
size() const override
;
191 /** Returns the current stream position. */
192 virtual sal_Int64
tell() const override
;
193 /** Seeks the stream to the passed position. */
194 virtual void seek( sal_Int64 nPos
) override
;
195 /** Releases the reference to the data sequence. */
196 virtual void close() override
;
199 StreamDataSequence
* mpData
; ///< Wrapped data sequence.
200 sal_Int32 mnPos
; ///< Current position in the sequence.
208 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */