bump product version to 7.6.3.2-android
[LibreOffice.git] / package / source / zipapi / XBufferedThreadedStream.hxx
blobbeb1cd33c7004e64a8d92271963bacfe53821a59
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 <com/sun/star/io/XInputStream.hpp>
15 #include <cppuhelper/implbase.hxx>
16 #include <rtl/ref.hxx>
17 #include <salhelper/thread.hxx>
19 #include <queue>
20 #include <mutex>
21 #include <condition_variable>
22 #include <exception>
24 typedef css::uno::Sequence< sal_Int8 > Buffer;
26 class XBufferedThreadedStream : public cppu::WeakImplHelper< css::io::XInputStream >
28 private:
29 const css::uno::Reference<XInputStream> mxSrcStream;
30 sal_Int64 mnPos; /// position in stream
31 sal_Int64 mnStreamSize; /// available size of stream
33 Buffer maInUseBuffer; /// Buffer block in use
34 int mnOffset; /// position in maInUseBuffer
35 std::queue < Buffer > maPendingBuffers; /// Buffers that are available for use
36 std::queue < Buffer > maUsedBuffers;
38 rtl::Reference< salhelper::Thread > mxUnzippingThread;
39 std::mutex maBufferProtector; /// mutex protecting Buffer queues.
40 std::condition_variable maBufferConsumeResume;
41 std::condition_variable maBufferProduceResume;
42 bool mbTerminateThread; /// indicates the failure of one of the threads
44 std::exception_ptr maSavedException; /// exception caught during unzipping is saved to be thrown during reading
46 static const size_t nBufferLowWater = 2;
47 static const size_t nBufferHighWater = 4;
48 static const size_t nBufferSize = 32 * 1024;
50 const Buffer& getNextBlock();
51 sal_Int64 remainingSize() const { return mnStreamSize - mnPos; }
52 bool hasBytes() const { return mnPos < mnStreamSize; }
54 bool canProduce() const
56 return( mbTerminateThread || maPendingBuffers.size() < nBufferHighWater );
59 bool canConsume() const
61 return( mbTerminateThread || !maPendingBuffers.empty() );
64 public:
65 XBufferedThreadedStream(
66 const css::uno::Reference<XInputStream>& xSrcStream,
67 sal_Int64 nStreamSize /* cf. sal_Int32 available(); */ );
69 virtual ~XBufferedThreadedStream() override;
71 void produce();
72 void setTerminateThread();
73 void saveException(const std::exception_ptr& exception) { maSavedException = exception; }
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;
82 #endif
84 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */