LibreOffice Module sax (master) 1
CachedOutputStream.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
10#ifndef INCLUDED_SAX_SOURCE_TOOLS_CACHEDOUTPUTSTREAM_HXX
11#define INCLUDED_SAX_SOURCE_TOOLS_CACHEDOUTPUTSTREAM_HXX
12
13#include <sal/types.h>
14
15#include <com/sun/star/io/XOutputStream.hpp>
16#include <com/sun/star/uno/Sequence.hxx>
17
18#include <cstring>
19#include <memory>
20
21namespace sax_fastparser {
22
24{
25public:
26 virtual ~ForMergeBase() {}
27 virtual void append( const css::uno::Sequence<sal_Int8>& rWhat ) = 0;
28};
29
31{
33 static const sal_Int32 mnMaximumSize = 0x100000; // 1Mbyte
34
36 std::shared_ptr< ForMergeBase > mpForMerge;
37 const css::uno::Sequence<sal_Int8> mpCache;
39 css::uno::Reference< css::io::XOutputStream > mxOutputStream;
40 uno_Sequence *pSeq;
43
44public:
46 , pSeq(mpCache.get())
48 , mbWriteToOutStream(true)
49 {}
50
51 const css::uno::Reference< css::io::XOutputStream >& getOutputStream() const
52 {
53 return mxOutputStream;
54 }
55
56 void setOutputStream( const css::uno::Reference< css::io::XOutputStream >& xOutputStream )
57 {
58 mxOutputStream = xOutputStream;
59 }
60
61 void setOutput( std::shared_ptr< ForMergeBase > pForMerge )
62 {
63 flush();
64 mbWriteToOutStream = false;
65 mpForMerge = pForMerge;
66 }
67
69 {
70 flush();
71 mbWriteToOutStream = true;
72 mpForMerge.reset();
73 }
74
76 void writeBytes( const sal_Int8* pStr, sal_Int32 nLen )
77 {
78 // Write when the buffer gets big enough
80 {
81 flush();
82
83 // Writer does some elements sorting, so it can accumulate
84 // pretty big strings in FastSaxSerializer::ForMerge.
85 // In that case, just flush data and write immediately.
86 if (nLen > mnMaximumSize)
87 {
89 mxOutputStream->writeBytes( css::uno::Sequence<sal_Int8>(pStr, nLen) );
90 else
91 mpForMerge->append( css::uno::Sequence<sal_Int8>(pStr, nLen) );
92 return;
93 }
94 }
95
96 memcpy(pSeq->elements + mnCacheWrittenSize, pStr, nLen);
97 mnCacheWrittenSize += nLen;
98 }
99
101 void flush()
102 {
103 // resize the Sequence to written size
104 pSeq->nElements = mnCacheWrittenSize;
106 mxOutputStream->writeBytes( mpCache );
107 else
108 mpForMerge->append( mpCache );
109 // and next time write to the beginning
111 }
112};
113
114}
115
116#endif
117
118/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
static const sal_Int32 mnMaximumSize
When buffer hits this size, it's written to mxOutputStream.
css::uno::Reference< css::io::XOutputStream > mxOutputStream
Output stream, usually writing data into files.
std::shared_ptr< ForMergeBase > mpForMerge
ForMerge structure is used for sorting elements in Writer.
void setOutput(std::shared_ptr< ForMergeBase > pForMerge)
void writeBytes(const sal_Int8 *pStr, sal_Int32 nLen)
cache string and if limit is hit, flush
const css::uno::Reference< css::io::XOutputStream > & getOutputStream() const
const css::uno::Sequence< sal_Int8 > mpCache
void flush()
immediately write buffer into mxOutputStream and clear
void setOutputStream(const css::uno::Reference< css::io::XOutputStream > &xOutputStream)
virtual void append(const css::uno::Sequence< sal_Int8 > &rWhat)=0
css::uno::Reference< css::linguistic2::XProofreadingIterator > get(css::uno::Reference< css::uno::XComponentContext > const &context)
signed char sal_Int8