bump product version to 7.6.3.2-android
[LibreOffice.git] / include / comphelper / seqstream.hxx
blob1fd695bddf0db3f1aadb34022b30efc4e9a367af
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/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 #ifndef INCLUDED_COMPHELPER_SEQSTREAM_HXX
20 #define INCLUDED_COMPHELPER_SEQSTREAM_HXX
22 #include <config_options.h>
23 #include <com/sun/star/uno/Sequence.hxx>
24 #include <com/sun/star/io/XInputStream.hpp>
25 #include <com/sun/star/io/XOutputStream.hpp>
26 #include <com/sun/star/io/XSeekable.hpp>
27 #include <com/sun/star/lang/XUnoTunnel.hpp>
28 #include <cppuhelper/implbase.hxx>
29 #include <comphelper/comphelperdllapi.h>
30 #include <comphelper/bytereader.hxx>
31 #include <mutex>
33 namespace comphelper
36 /** Base class for wrappers around memory data that want to be exposed as an XInputStream */
37 class COMPHELPER_DLLPUBLIC MemoryInputStream
38 : public ::cppu::WeakImplHelper< css::io::XInputStream, css::io::XSeekable >,
39 public comphelper::ByteReader
41 std::mutex m_aMutex;
42 const sal_Int8* m_pMemoryData;
43 sal_Int32 m_nMemoryDataLength;
44 sal_Int32 m_nPos;
46 public:
47 MemoryInputStream(const sal_Int8* pData, sal_Int32 nDataLength);
49 // css::io::XInputStream
50 virtual sal_Int32 SAL_CALL readBytes( css::uno::Sequence<sal_Int8>& aData, sal_Int32 nBytesToRead ) override;
52 virtual sal_Int32 SAL_CALL readSomeBytes( css::uno::Sequence<sal_Int8>& aData, sal_Int32 nMaxBytesToRead ) override;
54 virtual void SAL_CALL skipBytes( sal_Int32 nBytesToSkip ) override;
56 virtual sal_Int32 SAL_CALL available( ) override;
58 virtual void SAL_CALL closeInput( ) override;
60 virtual void SAL_CALL seek( sal_Int64 location ) override;
61 virtual sal_Int64 SAL_CALL getPosition( ) override;
62 virtual sal_Int64 SAL_CALL getLength( ) override;
64 // comphelper::ByteReader
65 virtual sal_Int32 readSomeBytes( sal_Int8* pData, sal_Int32 nBytesToRead ) override;
67 private:
68 sal_Int32 avail();
72 // Stream for reading data from a sequence of bytes
73 class COMPHELPER_DLLPUBLIC SequenceInputStream final
74 : public MemoryInputStream
76 css::uno::Sequence<sal_Int8> const m_aData;
78 public:
79 SequenceInputStream(css::uno::Sequence<sal_Int8> const & rData);
82 // don't export to avoid duplicate WeakImplHelper definitions with MSVC
83 class SAL_DLLPUBLIC_TEMPLATE OSequenceOutputStream_Base
84 : public ::cppu::WeakImplHelper< css::io::XOutputStream >
85 {};
87 class UNLESS_MERGELIBS(COMPHELPER_DLLPUBLIC) OSequenceOutputStream final : public OSequenceOutputStream_Base
89 private:
90 css::uno::Sequence< sal_Int8 >& m_rSequence;
91 double m_nResizeFactor;
92 sal_Int32 const m_nMinimumResize;
93 sal_Int32 m_nSize;
94 // the size of the virtual stream. This is not the size of the sequence, but the number of bytes written
95 // into the stream at a given moment.
97 bool m_bConnected;
98 // closeOutput has been called ?
100 std::mutex m_aMutex;
102 void finalizeOutput();
103 virtual ~OSequenceOutputStream() override { if (m_bConnected) finalizeOutput(); }
105 public:
106 /** constructs the object. Everything written into the stream through the XOutputStream methods will be forwarded
107 to the sequence, reallocating it if necessary. Writing will start at offset 0 within the sequence.
108 @param _rSeq a reference to the sequence which will be used for output.
109 The caller is responsible for taking care of the lifetime of the stream
110 object and the sequence. If you're in doubt about this, use <code>closeOutput</code>
111 before destroying the sequence
112 @param _nResizeFactor the factor which is used for resizing the sequence when necessary. In every
113 resize step, the new sequence size will be calculated by multiplying the current
114 size with this factor, rounded off to the next multiple of 4.
115 @param _nMinimumResize the minimal number of bytes which is additionally allocated on resizing
116 @see closeOutput
118 OSequenceOutputStream(
119 css::uno::Sequence< sal_Int8 >& _rSeq,
120 double _nResizeFactor = 1.3,
121 sal_Int32 _nMinimumResize = 128
124 /// same as XOutputStream::writeBytes (as expected :)
125 virtual void SAL_CALL writeBytes( const css::uno::Sequence< sal_Int8 >& aData ) override;
126 /// this is a dummy in this implementation, no buffering is used
127 virtual void SAL_CALL flush( ) override;
128 /** closes the output stream. In the case of this class, this means that the sequence used for writing is
129 resized to the really used size and not used any further, every subsequent call to one of the XOutputStream
130 methods will throw a <code>NotConnectedException</code>.
132 virtual void SAL_CALL closeOutput( ) override;
135 } // namespace comphelper
137 #endif // INCLUDED_COMPHELPER_SEQSTREAM_HXX
140 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */