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 OOX_HELPER_BINARYOUTPUTSTREAM_HXX
21 #define OOX_HELPER_BINARYOUTPUTSTREAM_HXX
23 #include "oox/helper/binarystreambase.hxx"
25 namespace com
{ namespace sun
{ namespace star
{
26 namespace io
{ class XOutputStream
; }
31 // ============================================================================
33 /** Interface for binary output stream classes.
35 The binary data in the stream is written in little-endian format.
37 class BinaryOutputStream
: public virtual BinaryStreamBase
40 /** Derived classes implement writing the contents of the passed data
44 The size of the elements in the memory block, if available. Derived
45 classes may be interested in this information.
47 virtual void writeData( const StreamDataSequence
& rData
, size_t nAtomSize
= 1 ) = 0;
49 /** Derived classes implement writing the contents of the (preallocated!)
53 The size of the elements in the memory block, if available. Derived
54 classes may be interested in this information.
56 virtual void writeMemory( const void* pMem
, sal_Int32 nBytes
, size_t nAtomSize
= 1 ) = 0;
58 /** Writes a value to the stream and converts it to platform byte order.
59 All data types supported by the ByteOrderConverter class can be used.
61 template< typename Type
>
62 void writeValue( Type nValue
);
64 template< typename Type
>
65 void writeArray( Type
* opnArray
, sal_Int32 nElemCount
);
67 /** Stream operator for all data types supported by the writeValue() function. */
68 template< typename Type
>
69 inline BinaryOutputStream
& operator<<( Type nValue
) { writeValue( nValue
); return *this; }
71 void writeCompressedUnicodeArray( const OUString
& rString
, bool bCompressed
, bool bAllowNulChars
= false );
73 void writeCharArrayUC( const OUString
& rString
, rtl_TextEncoding eTextEnc
, bool bAllowNulChars
= false );
75 void writeUnicodeArray( const OUString
& rString
, bool bAllowNulChars
= false );
78 /** This dummy default c'tor will never call the c'tor of the virtual base
79 class BinaryStreamBase as this class cannot be instanciated directly. */
80 inline explicit BinaryOutputStream() : BinaryStreamBase( false ) {}
83 template< typename Type
>
84 void BinaryOutputStream::writeArray( Type
* opnArray
, sal_Int32 nElemCount
)
86 sal_Int32 nWriteSize
= getLimitedValue
< sal_Int32
, sal_Int32
>( nElemCount
, 0, SAL_MAX_INT32
/ sizeof( Type
) ) * sizeof( Type
);
87 ByteOrderConverter::convertLittleEndianArray( opnArray
, static_cast< size_t >( nElemCount
) );
88 writeMemory( opnArray
, nWriteSize
, sizeof( Type
) );
91 typedef ::boost::shared_ptr
< BinaryOutputStream
> BinaryOutputStreamRef
;
93 // ----------------------------------------------------------------------------
95 template< typename Type
>
96 void BinaryOutputStream::writeValue( Type nValue
)
98 ByteOrderConverter::convertLittleEndian( nValue
);
99 writeMemory( &nValue
, static_cast< sal_Int32
>( sizeof( Type
) ), sizeof( Type
) );
102 // ============================================================================
104 /** Wraps a UNO output stream and provides convenient access functions.
106 The binary data in the stream is written in little-endian format.
108 class BinaryXOutputStream
: public BinaryXSeekableStream
, public BinaryOutputStream
111 /** Constructs the wrapper object for the passed output stream.
114 The com.sun.star.io.XOutputStream interface of the output stream to
118 True = automatically close the wrapped output stream on destruction
119 of this wrapper or when close() is called.
121 explicit BinaryXOutputStream(
122 const ::com::sun::star::uno::Reference
< ::com::sun::star::io::XOutputStream
>& rxOutStrm
,
125 virtual ~BinaryXOutputStream();
127 /** Flushes and closes the output stream. Does also close the wrapped UNO
128 output stream if bAutoClose has been set to true in the constructor. */
131 /** Writes the passed data sequence. */
132 virtual void writeData( const StreamDataSequence
& rData
, size_t nAtomSize
= 1 );
134 /** Write nBytes bytes from the (preallocated!) buffer pMem. */
135 virtual void writeMemory( const void* pMem
, sal_Int32 nBytes
, size_t nAtomSize
= 1 );
137 /** Stream operator for all data types supported by the writeValue() function. */
138 template< typename Type
>
139 inline BinaryXOutputStream
& operator<<( Type nValue
) { writeValue( nValue
); return *this; }
141 /** Returns the XOutputStream interface of the wrapped output stream. */
142 inline ::com::sun::star::uno::Reference
< ::com::sun::star::io::XOutputStream
>
143 getXOutputStream() const { return mxOutStrm
; }
146 StreamDataSequence maBuffer
; ///< Data buffer used in writeMemory() function.
147 ::com::sun::star::uno::Reference
< ::com::sun::star::io::XOutputStream
>
148 mxOutStrm
; ///< Reference to the output stream.
149 bool mbAutoClose
; ///< True = automatically close stream on destruction.
152 // ============================================================================
154 /** Wraps a StreamDataSequence and provides convenient access functions.
156 The binary data in the stream is written in little-endian format. After
157 construction, the stream points to the beginning of the passed data
158 sequence. The data sequence is expanded automatically while writing to it.
160 class OOX_DLLPUBLIC SequenceOutputStream
: public SequenceSeekableStream
, public BinaryOutputStream
163 /** Constructs the wrapper object for the passed data sequence.
166 The passed data sequence MUST live at least as long as this stream
167 wrapper. The data sequence MUST NOT be changed from outside as long
168 as this stream wrapper is used to write to it.
170 explicit SequenceOutputStream( StreamDataSequence
& rData
);
172 /** Writes the passed data sequence. */
173 virtual void writeData( const StreamDataSequence
& rData
, size_t nAtomSize
= 1 );
175 /** Write nBytes bytes from the (preallocated!) buffer pMem. */
176 virtual void writeMemory( const void* pMem
, sal_Int32 nBytes
, size_t nAtomSize
= 1 );
178 /** Stream operator for all data types supported by the writeValue() function. */
179 template< typename Type
>
180 inline SequenceOutputStream
& operator<<( Type nValue
) { writeValue( nValue
); return *this; }
183 // ============================================================================
189 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */