LibreOffice Module package (master) 1
XBufferedThreadedStream.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_PACKAGE_SOURCE_ZIPAPI_XBUFFEREDTHREADEDSTREAM_HXX
11#define INCLUDED_PACKAGE_SOURCE_ZIPAPI_XBUFFEREDTHREADEDSTREAM_HXX
12
13#include <com/sun/star/io/XInputStream.hpp>
14
16#include <rtl/ref.hxx>
17#include <salhelper/thread.hxx>
18
19#include <queue>
20#include <mutex>
21#include <condition_variable>
22#include <exception>
23
24typedef css::uno::Sequence< sal_Int8 > Buffer;
25
26class XBufferedThreadedStream : public cppu::WeakImplHelper< css::io::XInputStream >
27{
28private:
29 const css::uno::Reference<XInputStream> mxSrcStream;
30 sal_Int64 mnPos;
31 sal_Int64 mnStreamSize;
32
35 std::queue < Buffer > maPendingBuffers;
36 std::queue < Buffer > maUsedBuffers;
37
39 std::mutex maBufferProtector;
40 std::condition_variable maBufferConsumeResume;
41 std::condition_variable maBufferProduceResume;
43
44 std::exception_ptr maSavedException;
45
46 static const size_t nBufferLowWater = 2;
47 static const size_t nBufferHighWater = 4;
48 static const size_t nBufferSize = 32 * 1024;
49
50 const Buffer& getNextBlock();
51 sal_Int64 remainingSize() const { return mnStreamSize - mnPos; }
52 bool hasBytes() const { return mnPos < mnStreamSize; }
53
54 bool canProduce() const
55 {
57 }
58
59 bool canConsume() const
60 {
61 return( mbTerminateThread || !maPendingBuffers.empty() );
62 }
63
64public:
66 const css::uno::Reference<XInputStream>& xSrcStream,
67 sal_Int64 nStreamSize /* cf. sal_Int32 available(); */ );
68
69 virtual ~XBufferedThreadedStream() override;
70
71 void produce();
72 void setTerminateThread();
73 void saveException(const std::exception_ptr& exception) { maSavedException = exception; }
74
75 // XInputStream
76 virtual sal_Int32 SAL_CALL readBytes( css::uno::Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead ) override;
77 virtual sal_Int32 SAL_CALL readSomeBytes( css::uno::Sequence< sal_Int8 >& aData, sal_Int32 nMaxBytesToRead ) override;
78 virtual void SAL_CALL skipBytes( sal_Int32 nBytesToSkip ) override;
79 virtual sal_Int32 SAL_CALL available( ) override;
80 virtual void SAL_CALL closeInput( ) override;
81};
82#endif
83
84/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
css::uno::Sequence< sal_Int8 > Buffer
void saveException(const std::exception_ptr &exception)
virtual void SAL_CALL skipBytes(sal_Int32 nBytesToSkip) override
std::condition_variable maBufferConsumeResume
mutex protecting Buffer queues.
Buffer maInUseBuffer
available size of stream
virtual sal_Int32 SAL_CALL available() override
const Buffer & getNextBlock()
Fetches next available block from maPendingBuffers for use in Reading thread.
XBufferedThreadedStream(const css::uno::Reference< XInputStream > &xSrcStream, sal_Int64 nStreamSize)
sal_Int64 mnStreamSize
position in stream
virtual sal_Int32 SAL_CALL readBytes(css::uno::Sequence< sal_Int8 > &aData, sal_Int32 nBytesToRead) override
static const size_t nBufferHighWater
std::queue< Buffer > maUsedBuffers
Buffers that are available for use.
virtual void SAL_CALL closeInput() override
int mnOffset
Buffer block in use.
std::condition_variable maBufferProduceResume
rtl::Reference< salhelper::Thread > mxUnzippingThread
virtual ~XBufferedThreadedStream() override
virtual sal_Int32 SAL_CALL readSomeBytes(css::uno::Sequence< sal_Int8 > &aData, sal_Int32 nMaxBytesToRead) override
void produce()
Reads from UnbufferedStream in a separate thread and stores the buffer blocks in maPendingBuffers que...
const css::uno::Reference< XInputStream > mxSrcStream
std::exception_ptr maSavedException
indicates the failure of one of the threads
static const size_t nBufferLowWater
exception caught during unzipping is saved to be thrown during reading
std::queue< Buffer > maPendingBuffers
position in maInUseBuffer