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
{ namespace sun
{ namespace 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( const OUString
& 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. */
95 #pragma warning( disable : 4702)
97 BinaryOutputStream() : BinaryStreamBase( false ) {}
103 BinaryOutputStream( BinaryOutputStream
const& ) = delete;
104 BinaryOutputStream
& operator=( BinaryOutputStream
const& ) = delete;
107 template< typename Type
>
108 void BinaryOutputStream::writeArray( Type
* opnArray
, sal_Int32 nElemCount
)
110 sal_Int32 nWriteSize
= getLimitedValue
< sal_Int32
, sal_Int32
>( nElemCount
, 0, SAL_MAX_INT32
/ sizeof( Type
) ) * sizeof( Type
);
111 ByteOrderConverter::convertLittleEndianArray( opnArray
, static_cast< size_t >( nElemCount
) );
112 writeMemory( opnArray
, nWriteSize
, sizeof( Type
) );
115 template< typename Type
>
116 void BinaryOutputStream::writeArray( const Type
* opnArray
, sal_Int32 nElemCount
)
118 std::unique_ptr
<Type
[]> xArray(new Type
[nElemCount
]);
119 std::uninitialized_copy(opnArray
, opnArray
+ nElemCount
, xArray
.get());
120 writeArray(xArray
.get(), nElemCount
);
123 template< typename Type
>
124 void BinaryOutputStream::writeValue( Type nValue
)
126 ByteOrderConverter::convertLittleEndian( nValue
);
127 writeMemory( &nValue
, static_cast< sal_Int32
>( sizeof( Type
) ), sizeof( Type
) );
131 /** Wraps a UNO output stream and provides convenient access functions.
133 The binary data in the stream is written in little-endian format.
135 class BinaryXOutputStream
: public BinaryXSeekableStream
, public BinaryOutputStream
138 /** Constructs the wrapper object for the passed output stream.
141 The com.sun.star.io.XOutputStream interface of the output stream to
145 True = automatically close the wrapped output stream on destruction
146 of this wrapper or when close() is called.
148 explicit BinaryXOutputStream(
149 const css::uno::Reference
< css::io::XOutputStream
>& rxOutStrm
,
152 virtual ~BinaryXOutputStream() override
;
154 /** Flushes and closes the output stream. Does also close the wrapped UNO
155 output stream if bAutoClose has been set to true in the constructor. */
156 void close() override
;
158 /** Writes the passed data sequence. */
159 virtual void writeData( const StreamDataSequence
& rData
, size_t nAtomSize
= 1 ) override
;
161 /** Write nBytes bytes from the (preallocated!) buffer pMem. */
162 virtual void writeMemory( const void* pMem
, sal_Int32 nBytes
, size_t nAtomSize
= 1 ) override
;
165 StreamDataSequence maBuffer
; ///< Data buffer used in writeMemory() function.
166 css::uno::Reference
< css::io::XOutputStream
>
167 mxOutStrm
; ///< Reference to the output stream.
168 bool mbAutoClose
; ///< True = automatically close stream on destruction.
172 /** Wraps a StreamDataSequence and provides convenient access functions.
174 The binary data in the stream is written in little-endian format. After
175 construction, the stream points to the beginning of the passed data
176 sequence. The data sequence is expanded automatically while writing to it.
178 class OOX_DLLPUBLIC SequenceOutputStream
: public SequenceSeekableStream
, public BinaryOutputStream
181 /** Constructs the wrapper object for the passed data sequence.
184 The passed data sequence MUST live at least as long as this stream
185 wrapper. The data sequence MUST NOT be changed from outside as long
186 as this stream wrapper is used to write to it.
188 explicit SequenceOutputStream( StreamDataSequence
& rData
);
190 /** Writes the passed data sequence. */
191 virtual void writeData( const StreamDataSequence
& rData
, size_t nAtomSize
= 1 ) override
;
193 /** Write nBytes bytes from the (preallocated!) buffer pMem. */
194 virtual void writeMemory( const void* pMem
, sal_Int32 nBytes
, size_t nAtomSize
= 1 ) override
;
202 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */