1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/.
10 #ifndef INCLUDED_SAX_SOURCE_TOOLS_CACHEDOUTPUTSTREAM_HXX
11 #define INCLUDED_SAX_SOURCE_TOOLS_CACHEDOUTPUTSTREAM_HXX
13 #include <sal/types.h>
15 #include <com/sun/star/io/XOutputStream.hpp>
16 #include <com/sun/star/uno/Sequence.hxx>
20 #include <boost/shared_ptr.hpp>
22 namespace sax_fastparser
{
27 virtual ~ForMergeBase() {}
28 virtual void append( const css::uno::Sequence
<sal_Int8
>& rWhat
) = 0;
31 class CachedOutputStream
33 /// When buffer hits this size, it's written to mxOutputStream
34 static const sal_Int32 mnMaximumSize
= 0x10000;
36 /// Output stream, usually writing data into files.
37 css::uno::Reference
< css::io::XOutputStream
> mxOutputStream
;
38 sal_Int32 mnCacheWrittenSize
;
39 const css::uno::Sequence
<sal_Int8
> mpCache
;
41 bool mbWriteToOutStream
;
42 /// ForMerge structure is used for sorting elements in Writer
43 boost::shared_ptr
< ForMergeBase
> mpForMerge
;
46 CachedOutputStream() : mnCacheWrittenSize(0)
47 , mpCache(mnMaximumSize
)
49 , mbWriteToOutStream(true)
52 ~CachedOutputStream() {}
54 css::uno::Reference
< css::io::XOutputStream
> getOutputStream() const
56 return mxOutputStream
;
59 void setOutputStream( const css::uno::Reference
< css::io::XOutputStream
>& xOutputStream
)
61 mxOutputStream
= xOutputStream
;
64 void setOutput( boost::shared_ptr
< ForMergeBase
> pForMerge
)
67 mbWriteToOutStream
= false;
68 mpForMerge
= pForMerge
;
71 void resetOutputToStream()
74 mbWriteToOutStream
= true;
78 /// cache string and if limit is hit, flush
79 void writeBytes( const sal_Int8
* pStr
, sal_Int32 nLen
)
81 // Write when the buffer gets big enough
82 if (mnCacheWrittenSize
+ nLen
> mnMaximumSize
)
86 // Writer does some elements sorting, so it can accumulate
87 // pretty big strings in FastSaxSerializer::ForMerge.
88 // In that case, just flush data and write immediately.
89 if (nLen
> mnMaximumSize
)
91 if (mbWriteToOutStream
)
92 mxOutputStream
->writeBytes( css::uno::Sequence
<sal_Int8
>(pStr
, nLen
) );
94 mpForMerge
->append( css::uno::Sequence
<sal_Int8
>(pStr
, nLen
) );
99 memcpy(pSeq
->elements
+ mnCacheWrittenSize
, pStr
, nLen
);
100 mnCacheWrittenSize
+= nLen
;
103 /// immediately write buffer into mxOutputStream and clear
106 // resize the Sequence to written size
107 pSeq
->nElements
= mnCacheWrittenSize
;
108 if (mbWriteToOutStream
)
109 mxOutputStream
->writeBytes( mpCache
);
111 mpForMerge
->append( mpCache
);
112 // and next time write to the beginning
113 mnCacheWrittenSize
= 0;
121 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */