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 the output stream. */
115 void flush();
116
117 /** Flushes and closes the output stream. Does also close the wrapped UNO
118 output stream if bAutoClose has been set to true in the constructor. */
119 void close();
120
121 /** Writes the passed data sequence. */
122 virtual void writeData( const StreamDataSequence& rData, size_t nAtomSize = 1 );
123
124 /** Write nBytes bytes from the (preallocated!) buffer pMem. */
125 virtual void writeMemory( const void* pMem, sal_Int32 nBytes, size_t nAtomSize = 1 );
126
127 /** Stream operator for all data types supported by the writeValue() function. */
128 template< typename Type >
operator <<(Type nValue)129 inline BinaryXOutputStream& operator<<( Type nValue ) { writeValue( nValue ); return *this; }
130
131 /** Returns the XOutputStream interface of the wrapped output stream. */
132 inline ::com::sun::star::uno::Reference< ::com::sun::star::io::XOutputStream >
getXOutputStream() const133 getXOutputStream() const { return mxOutStrm; }
134
135 private:
136 StreamDataSequence maBuffer; /// Data buffer used in writeMemory() function.
137 ::com::sun::star::uno::Reference< ::com::sun::star::io::XOutputStream >
138 mxOutStrm; /// Reference to the output stream.
139 bool mbAutoClose; /// True = automatically close stream on destruction.
140 };
141
142 // ============================================================================
143
144 /** Wraps a StreamDataSequence and provides convenient access functions.
145
146 The binary data in the stream is written in little-endian format. After
147 construction, the stream points to the beginning of the passed data
148 sequence. The data sequence is expanded automatically while writing to it.
149 */
150 class SequenceOutputStream : public SequenceSeekableStream, public BinaryOutputStream
151 {
152 public:
153 /** Constructs the wrapper object for the passed data sequence.
154
155 @attention
156 The passed data sequence MUST live at least as long as this stream
157 wrapper. The data sequence MUST NOT be changed from outside as long
158 as this stream wrapper is used to write to it.
159 */
160 explicit SequenceOutputStream( StreamDataSequence& rData );
161
162 /** Writes the passed data sequence. */
163 virtual void writeData( const StreamDataSequence& rData, size_t nAtomSize = 1 );
164
165 /** Write nBytes bytes from the (preallocated!) buffer pMem. */
166 virtual void writeMemory( const void* pMem, sal_Int32 nBytes, size_t nAtomSize = 1 );
167
168 /** Stream operator for all data types supported by the writeValue() function. */
169 template< typename Type >
operator <<(Type nValue)170 inline SequenceOutputStream& operator<<( Type nValue ) { writeValue( nValue ); return *this; }
171 };
172
173 // ============================================================================
174
175 } // namespace oox
176
177 #endif
178