Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #ifndef INCLUDED_OOX_HELPER_BINARYOUTPUTSTREAM_HXX
21 : #define INCLUDED_OOX_HELPER_BINARYOUTPUTSTREAM_HXX
22 :
23 : #include <memory>
24 : #include <boost/shared_array.hpp>
25 :
26 : #include <oox/helper/binarystreambase.hxx>
27 :
28 : namespace com { namespace sun { namespace star {
29 : namespace io { class XOutputStream; }
30 : } } }
31 :
32 : namespace oox {
33 :
34 :
35 :
36 : /** Interface for binary output stream classes.
37 :
38 : The binary data in the stream is written in little-endian format.
39 : */
40 2104 : class BinaryOutputStream : public virtual BinaryStreamBase
41 : {
42 : public:
43 : /** Derived classes implement writing the contents of the passed data
44 : sequence.
45 :
46 : @param nAtomSize
47 : The size of the elements in the memory block, if available. Derived
48 : classes may be interested in this information.
49 : */
50 : virtual void writeData( const StreamDataSequence& rData, size_t nAtomSize = 1 ) = 0;
51 :
52 : /** Derived classes implement writing the contents of the (preallocated!)
53 : memory buffer pMem.
54 :
55 : @param nAtomSize
56 : The size of the elements in the memory block, if available. Derived
57 : classes may be interested in this information.
58 : */
59 : virtual void writeMemory( const void* pMem, sal_Int32 nBytes, size_t nAtomSize = 1 ) = 0;
60 :
61 : template< typename Type >
62 : void writeArray( Type* opnArray, sal_Int32 nElemCount );
63 :
64 : template< typename Type >
65 : void writeArray( const Type* opnArray, sal_Int32 nElemCount );
66 :
67 : /** Writes a value to the stream and converts it to platform byte order.
68 : All data types supported by the ByteOrderConverter class can be used.
69 : */
70 : template< typename Type >
71 : void writeValue( Type nValue );
72 :
73 : BinaryOutputStream& WriteInt8(sal_Int8 x) { writeValue(x); return *this; }
74 : BinaryOutputStream& WriteUInt8(sal_uInt8 x) { writeValue(x); return *this; }
75 2 : BinaryOutputStream& WriteInt16(sal_Int16 x) { writeValue(x); return *this; }
76 4 : BinaryOutputStream& WriteUInt16(sal_uInt16 x) { writeValue(x); return *this; }
77 3 : BinaryOutputStream& WriteInt32(sal_Int32 x) { writeValue(x); return *this; }
78 2 : BinaryOutputStream& WriteUInt32(sal_uInt32 x) { writeValue(x); return *this; }
79 2 : BinaryOutputStream& WriteInt64(sal_Int64 x) { writeValue(x); return *this; }
80 : BinaryOutputStream& WriteUInt64(sal_uInt64 x) { writeValue(x); return *this; }
81 : BinaryOutputStream& WriteFloat(float x) { writeValue(x); return *this; }
82 : BinaryOutputStream& WriteDouble(double x) { writeValue(x); return *this; }
83 :
84 : void writeCompressedUnicodeArray( const OUString& rString, bool bCompressed, bool bAllowNulChars = false );
85 :
86 : void writeCharArrayUC( const OUString& rString, rtl_TextEncoding eTextEnc, bool bAllowNulChars = false );
87 :
88 : void writeUnicodeArray( const OUString& rString, bool bAllowNulChars = false );
89 :
90 : protected:
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 2104 : BinaryOutputStream() : BinaryStreamBase( false ) {}
94 : };
95 :
96 : template< typename Type >
97 1 : void BinaryOutputStream::writeArray( Type* opnArray, sal_Int32 nElemCount )
98 : {
99 1 : sal_Int32 nWriteSize = getLimitedValue< sal_Int32, sal_Int32 >( nElemCount, 0, SAL_MAX_INT32 / sizeof( Type ) ) * sizeof( Type );
100 1 : ByteOrderConverter::convertLittleEndianArray( opnArray, static_cast< size_t >( nElemCount ) );
101 1 : writeMemory( opnArray, nWriteSize, sizeof( Type ) );
102 1 : }
103 :
104 : template< typename Type >
105 1 : void BinaryOutputStream::writeArray( const Type* opnArray, sal_Int32 nElemCount )
106 : {
107 1 : boost::shared_array<Type> pArray(new Type[nElemCount]);
108 1 : std::uninitialized_copy(opnArray, opnArray + nElemCount, pArray.get());
109 1 : writeArray(pArray.get(), nElemCount);
110 1 : }
111 :
112 :
113 :
114 :
115 : template< typename Type >
116 25 : void BinaryOutputStream::writeValue( Type nValue )
117 : {
118 25 : ByteOrderConverter::convertLittleEndian( nValue );
119 25 : writeMemory( &nValue, static_cast< sal_Int32 >( sizeof( Type ) ), sizeof( Type ) );
120 25 : }
121 :
122 :
123 :
124 : /** Wraps a UNO output stream and provides convenient access functions.
125 :
126 : The binary data in the stream is written in little-endian format.
127 : */
128 : class BinaryXOutputStream : public BinaryXSeekableStream, public BinaryOutputStream
129 : {
130 : public:
131 : /** Constructs the wrapper object for the passed output stream.
132 :
133 : @param rxOutStream
134 : The com.sun.star.io.XOutputStream interface of the output stream to
135 : be wrapped.
136 :
137 : @param bAutoClose
138 : True = automatically close the wrapped output stream on destruction
139 : of this wrapper or when close() is called.
140 : */
141 : explicit BinaryXOutputStream(
142 : const ::com::sun::star::uno::Reference< ::com::sun::star::io::XOutputStream >& rxOutStrm,
143 : bool bAutoClose );
144 :
145 : virtual ~BinaryXOutputStream();
146 :
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() SAL_OVERRIDE;
150 :
151 : /** Writes the passed data sequence. */
152 : virtual void writeData( const StreamDataSequence& rData, size_t nAtomSize = 1 ) SAL_OVERRIDE;
153 :
154 : /** Write nBytes bytes from the (preallocated!) buffer pMem. */
155 : virtual void writeMemory( const void* pMem, sal_Int32 nBytes, size_t nAtomSize = 1 ) SAL_OVERRIDE;
156 :
157 : /** Returns the XOutputStream interface of the wrapped output stream. */
158 : ::com::sun::star::uno::Reference< ::com::sun::star::io::XOutputStream >
159 : getXOutputStream() const { return mxOutStrm; }
160 :
161 : private:
162 : StreamDataSequence maBuffer; ///< Data buffer used in writeMemory() function.
163 : ::com::sun::star::uno::Reference< ::com::sun::star::io::XOutputStream >
164 : mxOutStrm; ///< Reference to the output stream.
165 : bool mbAutoClose; ///< True = automatically close stream on destruction.
166 : };
167 :
168 :
169 :
170 : /** Wraps a StreamDataSequence and provides convenient access functions.
171 :
172 : The binary data in the stream is written in little-endian format. After
173 : construction, the stream points to the beginning of the passed data
174 : sequence. The data sequence is expanded automatically while writing to it.
175 : */
176 101 : class OOX_DLLPUBLIC SequenceOutputStream : public SequenceSeekableStream, public BinaryOutputStream
177 : {
178 : public:
179 : /** Constructs the wrapper object for the passed data sequence.
180 :
181 : @attention
182 : The passed data sequence MUST live at least as long as this stream
183 : wrapper. The data sequence MUST NOT be changed from outside as long
184 : as this stream wrapper is used to write to it.
185 : */
186 : explicit SequenceOutputStream( StreamDataSequence& rData );
187 :
188 : /** Writes the passed data sequence. */
189 : virtual void writeData( const StreamDataSequence& rData, size_t nAtomSize = 1 ) SAL_OVERRIDE;
190 :
191 : /** Write nBytes bytes from the (preallocated!) buffer pMem. */
192 : virtual void writeMemory( const void* pMem, sal_Int32 nBytes, size_t nAtomSize = 1 ) SAL_OVERRIDE;
193 : };
194 :
195 :
196 :
197 : } // namespace oox
198 :
199 : #endif
200 :
201 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|