1 /**************************************************************
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 *************************************************************/
21
22
23
24 #ifndef OOX_HELPER_BINARYOUTPUTSTREAM_HXX
25 #define OOX_HELPER_BINARYOUTPUTSTREAM_HXX
26
27 #include "oox/helper/binarystreambase.hxx"
28
29 namespace com { namespace sun { namespace star {
30 namespace io { class XOutputStream; }
31 } } }
32
33 namespace oox {
34
35 // ============================================================================
36
37 /** Interface for binary output stream classes.
38
39 The binary data in the stream is written in little-endian format.
40 */
41 class BinaryOutputStream : public virtual BinaryStreamBase
42 {
43 public:
44 /** Derived classes implement writing the contents of the passed data
45 sequence.
46
47 @param nAtomSize
48 The size of the elements in the memory block, if available. Derived
49 classes may be interested in this information.
50 */
51 virtual void writeData( const StreamDataSequence& rData, size_t nAtomSize = 1 ) = 0;
52
53 /** Derived classes implement writing the contents of the (preallocated!)
54 memory buffer pMem.
55
56 @param nAtomSize
57 The size of the elements in the memory block, if available. Derived
58 classes may be interested in this information.
59 */
60 virtual void writeMemory( const void* pMem, sal_Int32 nBytes, size_t nAtomSize = 1 ) = 0;
61
62 /** Writes a value to the stream and converts it to platform byte order.
63 All data types supported by the ByteOrderConverter class can be used.
64 */
65 template< typename Type >
66 void writeValue( Type nValue );
67
68 /** Stream operator for all data types supported by the writeValue() function. */
69 template< typename Type >
operator <<(Type nValue)70 inline BinaryOutputStream& operator<<( Type nValue ) { writeValue( nValue ); return *this; }
71
72 protected:
73 /** This dummy default c'tor will never call the c'tor of the virtual base
74 class BinaryStreamBase as this class cannot be instanciated directly. */
BinaryOutputStream()75 inline explicit BinaryOutputStream() : BinaryStreamBase( false ) {}
76 };
77
78 typedef ::boost::shared_ptr< BinaryOutputStream > BinaryOutputStreamRef;
79
80 // ----------------------------------------------------------------------------
81
82 template< typename Type >
writeValue(Type nValue)83 void BinaryOutputStream::writeValue( Type nValue )
84 {
85 ByteOrderConverter::convertLittleEndian( nValue );
86 writeMemory( &nValue, static_cast< sal_Int32 >( sizeof( Type ) ), sizeof( Type ) );
87 }
88
89 // ============================================================================
90
91 /** Wraps a UNO output stream and provides convenient access functions.
92
93 The binary data in the stream is written in little-endian format.
94 */
95 class BinaryXOutputStream : public BinaryXSeekableStream, public BinaryOutputStream
96 {
97 public:
98 /** Constructs the wrapper object for the passed output stream.
99
100 @param rxOutStream
101 The com.sun.star.io.XOutputStream interface of the output stream to
102 be wrapped.
103
104 @param bAutoClose
105 True = automatically close the wrapped output stream on destruction
106 of this wrapper or when close() is called.
107 */
108 explicit BinaryXOutputStream(
109 const ::com::sun::star::uno::Reference< ::com::sun::star::io::XOutputStream >& rxOutStrm,
110 bool bAutoClose );
111
112 virtual ~BinaryXOutputStream();
113
114 /** Flushes and closes the output stream. Does also close the wrapped UNO
115 output stream if bAutoClose has been set to true in the constructor. */
116 void close();
117
118 /** Writes the passed data sequence. */
119 virtual void writeData( const StreamDataSequence& rData, size_t nAtomSize = 1 );
120
121 /** Write nBytes bytes from the (preallocated!) buffer pMem. */
122 virtual void writeMemory( const void* pMem, sal_Int32 nBytes, size_t nAtomSize = 1 );
123
124 /** Stream operator for all data types supported by the writeValue() function. */
125 template< typename Type >
operator <<(Type nValue)126 inline BinaryXOutputStream& operator<<( Type nValue ) { writeValue( nValue ); return *this; }
127
128 /** Returns the XOutputStream interface of the wrapped output stream. */
129 inline ::com::sun::star::uno::Reference< ::com::sun::star::io::XOutputStream >
getXOutputStream() const130 getXOutputStream() const { return mxOutStrm; }
131
132 private:
133 StreamDataSequence maBuffer; /// Data buffer used in writeMemory() function.
134 ::com::sun::star::uno::Reference< ::com::sun::star::io::XOutputStream >
135 mxOutStrm; /// Reference to the output stream.
136 bool mbAutoClose; /// True = automatically close stream on destruction.
137 };
138
139 // ============================================================================
140
141 /** Wraps a StreamDataSequence and provides convenient access functions.
142
143 The binary data in the stream is written in little-endian format. After
144 construction, the stream points to the beginning of the passed data
145 sequence. The data sequence is expanded automatically while writing to it.
146 */
147 class SequenceOutputStream : public SequenceSeekableStream, public BinaryOutputStream
148 {
149 public:
150 /** Constructs the wrapper object for the passed data sequence.
151
152 @attention
153 The passed data sequence MUST live at least as long as this stream
154 wrapper. The data sequence MUST NOT be changed from outside as long
155 as this stream wrapper is used to write to it.
156 */
157 explicit SequenceOutputStream( StreamDataSequence& rData );
158
159 /** Writes the passed data sequence. */
160 virtual void writeData( const StreamDataSequence& rData, size_t nAtomSize = 1 );
161
162 /** Write nBytes bytes from the (preallocated!) buffer pMem. */
163 virtual void writeMemory( const void* pMem, sal_Int32 nBytes, size_t nAtomSize = 1 );
164
165 /** Stream operator for all data types supported by the writeValue() function. */
166 template< typename Type >
operator <<(Type nValue)167 inline SequenceOutputStream& operator<<( Type nValue ) { writeValue( nValue ); return *this; }
168 };
169
170 // ============================================================================
171
172 } // namespace oox
173
174 #endif
175