office-gobmx/sax/source/tools/CachedOutputStream.hxx
Caolán McNamara 283aa103b9 cid#1545177 COPY_INSTEAD_OF_MOVE
and

cid#1545179 COPY_INSTEAD_OF_MOVE
cid#1545187 COPY_INSTEAD_OF_MOVE
cid#1545188 COPY_INSTEAD_OF_MOVE
cid#1545189 COPY_INSTEAD_OF_MOVE
cid#1545196 COPY_INSTEAD_OF_MOVE
cid#1545197 COPY_INSTEAD_OF_MOVE
cid#1545204 COPY_INSTEAD_OF_MOVE
cid#1545223 COPY_INSTEAD_OF_MOVE
cid#1545236 COPY_INSTEAD_OF_MOVE
cid#1545239 COPY_INSTEAD_OF_MOVE
cid#1545253 COPY_INSTEAD_OF_MOVE
cid#1545274 COPY_INSTEAD_OF_MOVE
cid#1545286 COPY_INSTEAD_OF_MOVE
cid#1545309 COPY_INSTEAD_OF_MOVE
cid#1545311 COPY_INSTEAD_OF_MOVE
cid#1545345 COPY_INSTEAD_OF_MOVE
cid#1545358 COPY_INSTEAD_OF_MOVE
cid#1545361 COPY_INSTEAD_OF_MOVE
cid#1545365 COPY_INSTEAD_OF_MOVE
cid#1545367 COPY_INSTEAD_OF_MOVE
cid#1545372 COPY_INSTEAD_OF_MOVE
cid#1545373 COPY_INSTEAD_OF_MOVE
cid#1545377 COPY_INSTEAD_OF_MOVE
cid#1545392 COPY_INSTEAD_OF_MOVE
cid#1545399 COPY_INSTEAD_OF_MOVE
cid#1545404 COPY_INSTEAD_OF_MOVE
cid#1545408 COPY_INSTEAD_OF_MOVE
cid#1545430 COPY_INSTEAD_OF_MOVE
cid#1545439 COPY_INSTEAD_OF_MOVE
cid#1545449 COPY_INSTEAD_OF_MOVE

Change-Id: I3afe836a0bbc8bd70937035e60eb020435e413d2
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/160539
Tested-by: Jenkins
Reviewed-by: Caolán McNamara <caolan.mcnamara@collabora.com>
2023-12-10 17:41:18 +01:00

118 lines
3.4 KiB
C++

/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#ifndef INCLUDED_SAX_SOURCE_TOOLS_CACHEDOUTPUTSTREAM_HXX
#define INCLUDED_SAX_SOURCE_TOOLS_CACHEDOUTPUTSTREAM_HXX
#include <sal/types.h>
#include <com/sun/star/io/XOutputStream.hpp>
#include <com/sun/star/uno/Sequence.hxx>
#include <cstring>
#include <memory>
namespace sax_fastparser {
class ForMergeBase
{
public:
virtual ~ForMergeBase() {}
virtual void append( const css::uno::Sequence<sal_Int8>& rWhat ) = 0;
};
class CachedOutputStream
{
/// When buffer hits this size, it's written to mxOutputStream
static const sal_Int32 mnMaximumSize = 0x100000; // 1Mbyte
/// ForMerge structure is used for sorting elements in Writer
std::shared_ptr< ForMergeBase > mpForMerge;
const css::uno::Sequence<sal_Int8> mpCache;
/// Output stream, usually writing data into files.
css::uno::Reference< css::io::XOutputStream > mxOutputStream;
uno_Sequence *pSeq;
sal_Int32 mnCacheWrittenSize;
bool mbWriteToOutStream;
public:
CachedOutputStream() : mpCache(mnMaximumSize)
, pSeq(mpCache.get())
, mnCacheWrittenSize(0)
, mbWriteToOutStream(true)
{}
const css::uno::Reference< css::io::XOutputStream >& getOutputStream() const
{
return mxOutputStream;
}
void setOutputStream( const css::uno::Reference< css::io::XOutputStream >& xOutputStream )
{
mxOutputStream = xOutputStream;
}
void setOutput(const std::shared_ptr<ForMergeBase>& pForMerge)
{
flush();
mbWriteToOutStream = false;
mpForMerge = pForMerge;
}
void resetOutputToStream()
{
flush();
mbWriteToOutStream = true;
mpForMerge.reset();
}
/// cache string and if limit is hit, flush
void writeBytes( const sal_Int8* pStr, sal_Int32 nLen )
{
// Write when the buffer gets big enough
if (mnCacheWrittenSize + nLen > mnMaximumSize)
{
flush();
// Writer does some elements sorting, so it can accumulate
// pretty big strings in FastSaxSerializer::ForMerge.
// In that case, just flush data and write immediately.
if (nLen > mnMaximumSize)
{
if (mbWriteToOutStream)
mxOutputStream->writeBytes( css::uno::Sequence<sal_Int8>(pStr, nLen) );
else
mpForMerge->append( css::uno::Sequence<sal_Int8>(pStr, nLen) );
return;
}
}
memcpy(pSeq->elements + mnCacheWrittenSize, pStr, nLen);
mnCacheWrittenSize += nLen;
}
/// immediately write buffer into mxOutputStream and clear
void flush()
{
// resize the Sequence to written size
pSeq->nElements = mnCacheWrittenSize;
if (mbWriteToOutStream)
mxOutputStream->writeBytes( mpCache );
else
mpForMerge->append( mpCache );
// and next time write to the beginning
mnCacheWrittenSize = 0;
}
};
}
#endif
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */