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