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 OOX_HELPER_BINARYOUTPUTSTREAM_HXX
21 : #define OOX_HELPER_BINARYOUTPUTSTREAM_HXX
22 :
23 : #include "oox/helper/binarystreambase.hxx"
24 :
25 : namespace com { namespace sun { namespace star {
26 : namespace io { class XOutputStream; }
27 : } } }
28 :
29 : namespace oox {
30 :
31 : // ============================================================================
32 :
33 : /** Interface for binary output stream classes.
34 :
35 : The binary data in the stream is written in little-endian format.
36 : */
37 154 : class BinaryOutputStream : public virtual BinaryStreamBase
38 : {
39 : public:
40 : /** Derived classes implement writing the contents of the passed data
41 : sequence.
42 :
43 : @param nAtomSize
44 : The size of the elements in the memory block, if available. Derived
45 : classes may be interested in this information.
46 : */
47 : virtual void writeData( const StreamDataSequence& rData, size_t nAtomSize = 1 ) = 0;
48 :
49 : /** Derived classes implement writing the contents of the (preallocated!)
50 : memory buffer pMem.
51 :
52 : @param nAtomSize
53 : The size of the elements in the memory block, if available. Derived
54 : classes may be interested in this information.
55 : */
56 : virtual void writeMemory( const void* pMem, sal_Int32 nBytes, size_t nAtomSize = 1 ) = 0;
57 :
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.
60 : */
61 : template< typename Type >
62 : void writeValue( Type nValue );
63 :
64 : template< typename Type >
65 : void writeArray( Type* opnArray, sal_Int32 nElemCount );
66 :
67 : /** Stream operator for all data types supported by the writeValue() function. */
68 : template< typename Type >
69 12 : inline BinaryOutputStream& operator<<( Type nValue ) { writeValue( nValue ); return *this; }
70 :
71 : void writeCompressedUnicodeArray( const ::rtl::OUString& rString, bool bCompressed, bool bAllowNulChars = false );
72 :
73 : void writeCharArrayUC( const ::rtl::OUString& rString, rtl_TextEncoding eTextEnc, bool bAllowNulChars = false );
74 :
75 : void writeUnicodeArray( const ::rtl::OUString& rString, bool bAllowNulChars = false );
76 :
77 : protected:
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 154 : inline explicit BinaryOutputStream() : BinaryStreamBase( false ) {}
81 : };
82 :
83 : template< typename Type >
84 1 : void BinaryOutputStream::writeArray( Type* opnArray, sal_Int32 nElemCount )
85 : {
86 1 : sal_Int32 nWriteSize = getLimitedValue< sal_Int32, sal_Int32 >( nElemCount, 0, SAL_MAX_INT32 / sizeof( Type ) ) * sizeof( Type );
87 1 : ByteOrderConverter::convertLittleEndianArray( opnArray, static_cast< size_t >( nWriteSize ) );
88 1 : writeMemory( opnArray, nWriteSize, sizeof( Type ) );
89 1 : }
90 :
91 : typedef ::boost::shared_ptr< BinaryOutputStream > BinaryOutputStreamRef;
92 :
93 : // ----------------------------------------------------------------------------
94 :
95 : template< typename Type >
96 32 : void BinaryOutputStream::writeValue( Type nValue )
97 : {
98 32 : ByteOrderConverter::convertLittleEndian( nValue );
99 32 : writeMemory( &nValue, static_cast< sal_Int32 >( sizeof( Type ) ), sizeof( Type ) );
100 32 : }
101 :
102 : // ============================================================================
103 :
104 : /** Wraps a UNO output stream and provides convenient access functions.
105 :
106 : The binary data in the stream is written in little-endian format.
107 : */
108 : class BinaryXOutputStream : public BinaryXSeekableStream, public BinaryOutputStream
109 : {
110 : public:
111 : /** Constructs the wrapper object for the passed output stream.
112 :
113 : @param rxOutStream
114 : The com.sun.star.io.XOutputStream interface of the output stream to
115 : be wrapped.
116 :
117 : @param bAutoClose
118 : True = automatically close the wrapped output stream on destruction
119 : of this wrapper or when close() is called.
120 : */
121 : explicit BinaryXOutputStream(
122 : const ::com::sun::star::uno::Reference< ::com::sun::star::io::XOutputStream >& rxOutStrm,
123 : bool bAutoClose );
124 :
125 : virtual ~BinaryXOutputStream();
126 :
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. */
129 : void close();
130 :
131 : /** Writes the passed data sequence. */
132 : virtual void writeData( const StreamDataSequence& rData, size_t nAtomSize = 1 );
133 :
134 : /** Write nBytes bytes from the (preallocated!) buffer pMem. */
135 : virtual void writeMemory( const void* pMem, sal_Int32 nBytes, size_t nAtomSize = 1 );
136 :
137 : /** Stream operator for all data types supported by the writeValue() function. */
138 : template< typename Type >
139 1 : inline BinaryXOutputStream& operator<<( Type nValue ) { writeValue( nValue ); return *this; }
140 :
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; }
144 :
145 : private:
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.
150 : };
151 :
152 : // ============================================================================
153 :
154 : /** Wraps a StreamDataSequence and provides convenient access functions.
155 :
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.
159 : */
160 0 : class OOX_DLLPUBLIC SequenceOutputStream : public SequenceSeekableStream, public BinaryOutputStream
161 : {
162 : public:
163 : /** Constructs the wrapper object for the passed data sequence.
164 :
165 : @attention
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.
169 : */
170 : explicit SequenceOutputStream( StreamDataSequence& rData );
171 :
172 : /** Writes the passed data sequence. */
173 : virtual void writeData( const StreamDataSequence& rData, size_t nAtomSize = 1 );
174 :
175 : /** Write nBytes bytes from the (preallocated!) buffer pMem. */
176 : virtual void writeMemory( const void* pMem, sal_Int32 nBytes, size_t nAtomSize = 1 );
177 :
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; }
181 : };
182 :
183 : // ============================================================================
184 :
185 : } // namespace oox
186 :
187 : #endif
188 :
189 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|