LibreOffice Module oox (master)  1
binaryoutputstream.hxx
Go to the documentation of this file.
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 <cstddef>
24 #include <memory>
25 
26 #include <com/sun/star/uno/Reference.hxx>
27 #include <oox/dllapi.h>
29 #include <oox/helper/helper.hxx>
30 #include <rtl/textenc.h>
31 #include <rtl/ustring.hxx>
32 #include <sal/types.h>
33 
34 namespace com::sun::star {
35  namespace io { class XOutputStream; }
36 }
37 
38 namespace oox {
39 
40 
45 class BinaryOutputStream : public virtual BinaryStreamBase
46 {
47 public:
55  virtual void writeData( const StreamDataSequence& rData, size_t nAtomSize = 1 ) = 0;
56 
64  virtual void writeMemory( const void* pMem, sal_Int32 nBytes, size_t nAtomSize = 1 ) = 0;
65 
66  template< typename Type >
67  void writeArray( Type* opnArray, sal_Int32 nElemCount );
68 
69  template< typename Type >
70  void writeArray( const Type* opnArray, sal_Int32 nElemCount );
71 
75  template< typename Type >
76  void writeValue( Type nValue );
77 
78  BinaryOutputStream& WriteInt16(sal_Int16 x) { writeValue(x); return *this; }
79  BinaryOutputStream& WriteUInt16(sal_uInt16 x) { writeValue(x); return *this; }
80  BinaryOutputStream& WriteInt32(sal_Int32 x) { writeValue(x); return *this; }
81  BinaryOutputStream& WriteUInt32(sal_uInt32 x) { writeValue(x); return *this; }
82  BinaryOutputStream& WriteInt64(sal_Int64 x) { writeValue(x); return *this; }
83 
84  void writeCompressedUnicodeArray( const OUString& rString, bool bCompressed );
85 
86  void writeCharArrayUC( std::u16string_view rString, rtl_TextEncoding eTextEnc );
87 
88  void writeUnicodeArray( const OUString& rString );
89 
90 protected:
94 
95 private:
96  BinaryOutputStream( BinaryOutputStream const& ) = delete;
98 };
99 
100 template< typename Type >
101 void BinaryOutputStream::writeArray( Type* opnArray, sal_Int32 nElemCount )
102 {
103  sal_Int32 nWriteSize = getLimitedValue< sal_Int32, sal_Int32 >( nElemCount, 0, SAL_MAX_INT32 / sizeof( Type ) ) * sizeof( Type );
104  ByteOrderConverter::convertLittleEndianArray( opnArray, static_cast< size_t >( nElemCount ) );
105  writeMemory( opnArray, nWriteSize, sizeof( Type ) );
106 }
107 
108 template< typename Type >
109 void BinaryOutputStream::writeArray( const Type* opnArray, sal_Int32 nElemCount )
110 {
111  std::unique_ptr<Type[]> xArray(new Type[nElemCount]);
112  std::uninitialized_copy(opnArray, opnArray + nElemCount, xArray.get());
113  writeArray(xArray.get(), nElemCount);
114 }
115 
116 template< typename Type >
118 {
120  writeMemory( &nValue, static_cast< sal_Int32 >( sizeof( Type ) ), sizeof( Type ) );
121 }
122 
123 
129 {
130 public:
141  explicit BinaryXOutputStream(
142  const css::uno::Reference< css::io::XOutputStream >& rxOutStrm,
143  bool bAutoClose );
144 
145  virtual ~BinaryXOutputStream() override;
146 
149  void close() override;
150 
152  virtual void writeData( const StreamDataSequence& rData, size_t nAtomSize = 1 ) override;
153 
155  virtual void writeMemory( const void* pMem, sal_Int32 nBytes, size_t nAtomSize = 1 ) override;
156 
157 private:
159  css::uno::Reference< css::io::XOutputStream >
161  bool mbAutoClose;
162 };
163 
164 
172 {
173 public:
182 
184  virtual void writeData( const StreamDataSequence& rData, size_t nAtomSize = 1 ) override;
185 
187  virtual void writeMemory( const void* pMem, sal_Int32 nBytes, size_t nAtomSize = 1 ) override;
188 
190  virtual sal_Int64 size() const override;
192  virtual sal_Int64 tell() const override;
194  virtual void seek( sal_Int64 nPos ) override;
196  virtual void close() override;
197 
198 private:
200  sal_Int32 mnPos;
201 };
202 
203 
204 } // namespace oox
205 
206 #endif
207 
208 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Type
Base class for binary stream classes.
Base class for binary input and output streams wrapping a UNO stream, seekable via the com...
SequenceOutputStream(StreamDataSequence &rData)
Constructs the wrapper object for the passed data sequence.
exports com.sun.star. io
void writeValue(Type nValue)
Writes a value to the stream and converts it to platform byte order.
virtual sal_Int64 size() const override
Returns the size of the wrapped data sequence.
virtual void writeData(const StreamDataSequence &rData, size_t nAtomSize=1) override
Writes the passed data sequence.
const ContentProperties & rData
virtual void writeData(const StreamDataSequence &rData, size_t nAtomSize=1)=0
Derived classes implement writing the contents of the passed data sequence.
Interface for binary output stream classes.
Wraps a UNO output stream and provides convenient access functions.
BinaryOutputStream & WriteUInt32(sal_uInt32 x)
void writeCompressedUnicodeArray(const OUString &rString, bool bCompressed)
virtual void seek(sal_Int64 nPos) override
Seeks the stream to the passed position.
BinaryOutputStream()
This dummy default c'tor will never call the c'tor of the virtual base class BinaryStreamBase as this...
css::uno::Sequence< sal_Int8 > StreamDataSequence
#define SAL_MAX_INT32
BinaryOutputStream & WriteInt16(sal_Int16 x)
BinaryOutputStream & WriteInt32(sal_Int32 x)
BinaryOutputStream & operator=(BinaryOutputStream const &)=delete
virtual void writeMemory(const void *pMem, sal_Int32 nBytes, size_t nAtomSize=1)=0
Derived classes implement writing the contents of the (preallocated!) memory buffer pMem...
css::uno::Reference< css::io::XOutputStream > mxOutStrm
Reference to the output stream.
sal_Int32 mnPos
Current position in the sequence.
StreamDataSequence maBuffer
Data buffer used in writeMemory() function.
static void convertLittleEndianArray(Type *, size_t)
Definition: helper.hxx:241
bool close
void writeCharArrayUC(std::u16string_view rString, rtl_TextEncoding eTextEnc)
BinaryOutputStream & WriteUInt16(sal_uInt16 x)
virtual sal_Int64 tell() const override
Returns the current stream position.
virtual void close() override
Releases the reference to the data sequence.
void writeArray(Type *opnArray, sal_Int32 nElemCount)
#define OOX_DLLPUBLIC
Definition: dllapi.h:28
virtual void writeMemory(const void *pMem, sal_Int32 nBytes, size_t nAtomSize=1) override
Write nBytes bytes from the (preallocated!) buffer pMem.
StreamDataSequence * mpData
Wrapped data sequence.
bool mbAutoClose
True = automatically close stream on destruction.
BinaryOutputStream & WriteInt64(sal_Int64 x)
void writeUnicodeArray(const OUString &rString)
Wraps a StreamDataSequence and provides convenient access functions.
sal_uInt16 nPos
sal_Int16 nValue
static void convertLittleEndian(Type &)
Definition: helper.hxx:238