LCOV - code coverage report
Current view: top level - sax/source/tools - CachedOutputStream.hxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 38 39 97.4 %
Date: 2014-11-03 Functions: 10 11 90.9 %
Legend: Lines: hit not hit

          Line data    Source code
       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 <cstdlib>
      20             : #include <boost/shared_ptr.hpp>
      21             : 
      22             : namespace sax_fastparser {
      23             : 
      24      213072 : class ForMergeBase
      25             : {
      26             : public:
      27      213072 :     virtual ~ForMergeBase() {}
      28             :     virtual void append( const css::uno::Sequence<sal_Int8>& rWhat ) = 0;
      29             : };
      30             : 
      31             : class CachedOutputStream
      32             : {
      33             :     /// When buffer hits this size, it's written to mxOutputStream
      34             :     static const sal_Int32 mnMaximumSize = 0x10000;
      35             : 
      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;
      40             :     uno_Sequence *pSeq;
      41             :     bool mbWriteToOutStream;
      42             :     /// ForMerge structure is used for sorting elements in Writer
      43             :     boost::shared_ptr< ForMergeBase > mpForMerge;
      44             : 
      45             : public:
      46        7446 :     CachedOutputStream() : mnCacheWrittenSize(0)
      47             :                          , mpCache(mnMaximumSize)
      48        7446 :                          , pSeq(mpCache.get())
      49             :                          , mbWriteToOutStream(true)
      50       14892 :                          , mpForMerge(nullptr)
      51        7446 :     {}
      52        7446 :     ~CachedOutputStream() {}
      53             : 
      54        6812 :     css::uno::Reference< css::io::XOutputStream > getOutputStream() const
      55             :     {
      56        6812 :         return mxOutputStream;
      57             :     }
      58             : 
      59        7446 :     void setOutputStream( const css::uno::Reference< css::io::XOutputStream >& xOutputStream )
      60             :     {
      61        7446 :         mxOutputStream = xOutputStream;
      62        7446 :     }
      63             : 
      64      380134 :     void setOutput( boost::shared_ptr< ForMergeBase > pForMerge )
      65             :     {
      66      380134 :         flush();
      67      380134 :         mbWriteToOutStream = false;
      68      380134 :         mpForMerge = pForMerge;
      69      380134 :     }
      70             : 
      71       46010 :     void resetOutputToStream()
      72             :     {
      73       46010 :         flush();
      74       46010 :         mbWriteToOutStream = true;
      75       46010 :         mpForMerge.reset();
      76       46010 :     }
      77             : 
      78             :     /// cache string and if limit is hit, flush
      79    22302220 :     void writeBytes( const sal_Int8* pStr, sal_Int32 nLen )
      80             :     {
      81             :         // Write when the buffer gets big enough
      82    22302220 :         if (mnCacheWrittenSize + nLen > mnMaximumSize)
      83             :         {
      84          32 :             flush();
      85             : 
      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          32 :             if (nLen > mnMaximumSize)
      90             :             {
      91           4 :                 if (mbWriteToOutStream)
      92           4 :                     mxOutputStream->writeBytes( css::uno::Sequence<sal_Int8>(pStr, nLen) );
      93             :                 else
      94           0 :                     mpForMerge->append( css::uno::Sequence<sal_Int8>(pStr, nLen) );
      95    22302224 :                 return;
      96             :             }
      97             :         }
      98             : 
      99    22302216 :         memcpy(pSeq->elements + mnCacheWrittenSize, pStr, nLen);
     100    22302216 :         mnCacheWrittenSize += nLen;
     101             :     }
     102             : 
     103             :     /// immediately write buffer into mxOutputStream and clear
     104      975552 :     void flush()
     105             :     {
     106             :         // resize the Sequence to written size
     107      975552 :         pSeq->nElements = mnCacheWrittenSize;
     108      975552 :         if (mbWriteToOutStream)
     109       53488 :             mxOutputStream->writeBytes( mpCache );
     110             :         else
     111      922064 :             mpForMerge->append( mpCache );
     112             :         // and next time write to the beginning
     113      975552 :         mnCacheWrittenSize = 0;
     114      975552 :     }
     115             : };
     116             : 
     117             : }
     118             : 
     119             : #endif
     120             : 
     121             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10