Version 6.1.4.1, tag libreoffice-6.1.4.1
[LibreOffice.git] / package / source / zipapi / XBufferedThreadedStream.hxx
blobb99864fbb268b5d775d162df8dea7e0335bca54c
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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/.
8 */
10 #ifndef INCLUDED_PACKAGE_SOURCE_ZIPAPI_XBUFFEREDTHREADEDSTREAM_HXX
11 #define INCLUDED_PACKAGE_SOURCE_ZIPAPI_XBUFFEREDTHREADEDSTREAM_HXX
13 #include <salhelper/thread.hxx>
14 #include "XUnbufferedStream.hxx"
15 #include <queue>
16 #include <vector>
17 #include <mutex>
18 #include <condition_variable>
20 typedef css::uno::Sequence< sal_Int8 > Buffer;
22 class XBufferedThreadedStream : public cppu::WeakImplHelper< css::io::XInputStream >
24 private:
25 const css::uno::Reference<XInputStream> mxSrcStream;
26 sal_Int64 mnPos; /// position in stream
27 sal_Int64 mnStreamSize; /// available size of stream
29 Buffer maInUseBuffer; /// Buffer block in use
30 int mnOffset; /// position in maInUseBuffer
31 std::queue < Buffer > maPendingBuffers; /// Buffers that are available for use
32 std::queue < Buffer > maUsedBuffers;
34 rtl::Reference< salhelper::Thread > mxUnzippingThread;
35 std::mutex maBufferProtector; /// mutex protecting Buffer queues.
36 std::condition_variable maBufferConsumeResume;
37 std::condition_variable maBufferProduceResume;
38 bool mbTerminateThread; /// indicates the failure of one of the threads
40 css::uno::Any maSavedException; /// exception caught during unzipping is saved to be thrown during reading
42 static const size_t nBufferLowWater = 2;
43 static const size_t nBufferHighWater = 4;
44 static const size_t nBufferSize = 32 * 1024;
46 const Buffer& getNextBlock();
47 sal_Int64 remainingSize() const { return mnStreamSize - mnPos; }
48 bool hasBytes() const { return mnPos < mnStreamSize; }
50 bool canProduce() const
52 return( mbTerminateThread || maPendingBuffers.size() < nBufferHighWater );
55 bool canConsume() const
57 return( mbTerminateThread || !maPendingBuffers.empty() );
60 public:
61 XBufferedThreadedStream(
62 const css::uno::Reference<XInputStream>& xSrcStream,
63 sal_Int64 nStreamSize /* cf. sal_Int32 available(); */ );
65 virtual ~XBufferedThreadedStream() override;
67 void produce();
68 void setTerminateThread();
69 void saveException(const css::uno::Any &rAny) { maSavedException = rAny; }
71 // XInputStream
72 virtual sal_Int32 SAL_CALL readBytes( css::uno::Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead ) override;
73 virtual sal_Int32 SAL_CALL readSomeBytes( css::uno::Sequence< sal_Int8 >& aData, sal_Int32 nMaxBytesToRead ) override;
74 virtual void SAL_CALL skipBytes( sal_Int32 nBytesToSkip ) override;
75 virtual sal_Int32 SAL_CALL available( ) override;
76 virtual void SAL_CALL closeInput( ) override;
78 #endif
80 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */