GPU-Calc: remove Alloc_Host_Ptr for clmem of NAN vector
[LibreOffice.git] / oox / source / helper / binaryoutputstream.cxx
blob790c250126daeb96e5a805073a07e85eac946029
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include "oox/helper/binaryoutputstream.hxx"
22 #include <com/sun/star/io/XOutputStream.hpp>
23 #include <com/sun/star/io/XSeekable.hpp>
24 #include <osl/diagnose.h>
25 #include <string.h>
27 namespace oox {
29 // ============================================================================
31 using namespace ::com::sun::star::io;
32 using namespace ::com::sun::star::uno;
34 namespace {
36 const sal_Int32 OUTPUTSTREAM_BUFFERSIZE = 0x8000;
38 } // namespace
40 // ============================================================================
42 BinaryXOutputStream::BinaryXOutputStream( const Reference< XOutputStream >& rxOutStrm, bool bAutoClose ) :
43 BinaryStreamBase( Reference< XSeekable >( rxOutStrm, UNO_QUERY ).is() ),
44 BinaryXSeekableStream( Reference< XSeekable >( rxOutStrm, UNO_QUERY ) ),
45 maBuffer( OUTPUTSTREAM_BUFFERSIZE ),
46 mxOutStrm( rxOutStrm ),
47 mbAutoClose( bAutoClose && rxOutStrm.is() )
49 mbEof = !mxOutStrm.is();
52 BinaryXOutputStream::~BinaryXOutputStream()
54 close();
57 void BinaryXOutputStream::close()
59 OSL_ENSURE( !mbAutoClose || mxOutStrm.is(), "BinaryXOutputStream::close - invalid call" );
60 if( mxOutStrm.is() ) try
62 mxOutStrm->flush();
63 if ( mbAutoClose )
64 mxOutStrm->closeOutput();
66 catch( Exception& )
68 OSL_FAIL( "BinaryXOutputStream::close - closing output stream failed" );
70 mxOutStrm.clear();
71 mbAutoClose = false;
72 BinaryXSeekableStream::close();
75 void BinaryXOutputStream::writeData( const StreamDataSequence& rData, size_t /*nAtomSize*/ )
77 if( mxOutStrm.is() ) try
79 mxOutStrm->writeBytes( rData );
81 catch( Exception& )
83 OSL_FAIL( "BinaryXOutputStream::writeData - stream read error" );
87 void BinaryXOutputStream::writeMemory( const void* pMem, sal_Int32 nBytes, size_t nAtomSize )
89 if( mxOutStrm.is() && (nBytes > 0) )
91 sal_Int32 nBufferSize = getLimitedValue< sal_Int32, sal_Int32 >( nBytes, 0, (OUTPUTSTREAM_BUFFERSIZE / nAtomSize) * nAtomSize );
92 const sal_uInt8* pnMem = reinterpret_cast< const sal_uInt8* >( pMem );
93 while( nBytes > 0 )
95 sal_Int32 nWriteSize = getLimitedValue< sal_Int32, sal_Int32 >( nBytes, 0, nBufferSize );
96 maBuffer.realloc( nWriteSize );
97 memcpy( maBuffer.getArray(), pnMem, static_cast< size_t >( nWriteSize ) );
98 writeData( maBuffer, nAtomSize );
99 pnMem += nWriteSize;
100 nBytes -= nWriteSize;
105 void
106 BinaryOutputStream::writeCharArrayUC( const OUString& rString, rtl_TextEncoding eTextEnc, bool bAllowNulChars )
108 OString sBuf( OUStringToOString( rString, eTextEnc ) );
109 if( !bAllowNulChars )
110 sBuf = sBuf.replace( '\0', '?' );
111 writeMemory( static_cast< const void* >( sBuf.getStr() ), sBuf.getLength() );
114 void
115 BinaryOutputStream::writeUnicodeArray( const OUString& rString, bool bAllowNulChars )
117 OUString sBuf( rString );
118 if( !bAllowNulChars )
119 sBuf = sBuf.replace( '\0', '?' );
120 #ifdef OSL_BIGENDIAN
121 // need a non-const buffer for swapping byte order
122 sal_Unicode notConst[sBuf.getLength()];
123 memcpy( notConst, sBuf.getStr(), sizeof(sal_Unicode)*sBuf.getLength() );
124 writeArray( notConst, sBuf.getLength() );
125 #else
126 writeArray( sBuf.getStr(), sBuf.getLength() );
127 #endif
130 void
131 BinaryOutputStream::writeCompressedUnicodeArray( const OUString& rString, bool bCompressed, bool bAllowNulChars )
133 if ( bCompressed )
134 // ISO-8859-1 maps all byte values 0xHH to the same Unicode code point U+00HH
135 writeCharArrayUC( rString, RTL_TEXTENCODING_ISO_8859_1, bAllowNulChars );
136 else
137 writeUnicodeArray( rString, bAllowNulChars );
140 // ============================================================================
142 SequenceOutputStream::SequenceOutputStream( StreamDataSequence& rData ) :
143 BinaryStreamBase( true ),
144 SequenceSeekableStream( rData )
148 void SequenceOutputStream::writeData( const StreamDataSequence& rData, size_t nAtomSize )
150 if( mpData && rData.hasElements() )
151 writeMemory( rData.getConstArray(), rData.getLength(), nAtomSize );
154 void SequenceOutputStream::writeMemory( const void* pMem, sal_Int32 nBytes, size_t /*nAtomSize*/ )
156 if( mpData && (nBytes > 0) )
158 if( mpData->getLength() - mnPos < nBytes )
159 const_cast< StreamDataSequence* >( mpData )->realloc( mnPos + nBytes );
160 memcpy( const_cast< StreamDataSequence* >( mpData )->getArray() + mnPos, pMem, static_cast< size_t >( nBytes ) );
161 mnPos += nBytes;
165 // ============================================================================
167 } // namespace oox
169 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */