/src/libreoffice/include/comphelper/seqstream.hxx
Line | Count | Source |
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 | | * This file incorporates work covered by the following license notice: |
10 | | * |
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 . |
18 | | */ |
19 | | #ifndef INCLUDED_COMPHELPER_SEQSTREAM_HXX |
20 | | #define INCLUDED_COMPHELPER_SEQSTREAM_HXX |
21 | | |
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 <cppuhelper/implbase.hxx> |
28 | | #include <comphelper/comphelperdllapi.h> |
29 | | #include <comphelper/bytereader.hxx> |
30 | | #include <mutex> |
31 | | |
32 | | namespace comphelper |
33 | | { |
34 | | |
35 | | /** Base class for wrappers around memory data that want to be exposed as an XInputStream */ |
36 | | class COMPHELPER_DLLPUBLIC MemoryInputStream |
37 | | : public ::cppu::WeakImplHelper< css::io::XInputStream, css::io::XSeekable >, |
38 | | public comphelper::ByteReader |
39 | | { |
40 | | std::mutex m_aMutex; |
41 | | const sal_Int8* m_pMemoryData; |
42 | | sal_Int32 m_nMemoryDataLength; |
43 | | sal_Int32 m_nPos; |
44 | | |
45 | | public: |
46 | | MemoryInputStream(const sal_Int8* pData, sal_Int32 nDataLength); |
47 | | |
48 | | // css::io::XInputStream |
49 | | virtual sal_Int32 SAL_CALL readBytes( css::uno::Sequence<sal_Int8>& aData, sal_Int32 nBytesToRead ) override final; |
50 | | |
51 | | virtual sal_Int32 SAL_CALL readSomeBytes( css::uno::Sequence<sal_Int8>& aData, sal_Int32 nMaxBytesToRead ) override final; |
52 | | |
53 | | virtual void SAL_CALL skipBytes( sal_Int32 nBytesToSkip ) override final; |
54 | | |
55 | | virtual sal_Int32 SAL_CALL available( ) override final; |
56 | | |
57 | | virtual void SAL_CALL closeInput( ) override final; |
58 | | |
59 | | virtual void SAL_CALL seek( sal_Int64 location ) override final; |
60 | | virtual sal_Int64 SAL_CALL getPosition( ) override final; |
61 | | virtual sal_Int64 SAL_CALL getLength( ) override final; |
62 | | |
63 | | // comphelper::ByteReader |
64 | | virtual sal_Int32 readSomeBytes( sal_Int8* pData, sal_Int32 nBytesToRead ) override final; |
65 | | |
66 | | private: |
67 | | sal_Int32 avail(); |
68 | | }; |
69 | | |
70 | | |
71 | | // Stream for reading data from a sequence of bytes |
72 | | class COMPHELPER_DLLPUBLIC SequenceInputStream final |
73 | | : public MemoryInputStream |
74 | | { |
75 | | css::uno::Sequence<sal_Int8> const m_aData; |
76 | | |
77 | | public: |
78 | | SequenceInputStream(css::uno::Sequence<sal_Int8> const & rData); |
79 | | }; |
80 | | |
81 | | // don't export to avoid duplicate WeakImplHelper definitions with MSVC |
82 | | class SAL_DLLPUBLIC_TEMPLATE OSequenceOutputStream_Base |
83 | | : public ::cppu::WeakImplHelper< css::io::XOutputStream > |
84 | | {}; |
85 | | |
86 | | class UNLESS_MERGELIBS(COMPHELPER_DLLPUBLIC) OSequenceOutputStream final : public OSequenceOutputStream_Base |
87 | | { |
88 | | private: |
89 | | css::uno::Sequence< sal_Int8 >& m_rSequence; |
90 | | double m_nResizeFactor; |
91 | | sal_Int32 const m_nMinimumResize; |
92 | | sal_Int32 m_nSize; |
93 | | // the size of the virtual stream. This is not the size of the sequence, but the number of bytes written |
94 | | // into the stream at a given moment. |
95 | | |
96 | | bool m_bConnected; |
97 | | // closeOutput has been called ? |
98 | | |
99 | | std::mutex m_aMutex; |
100 | | |
101 | | void finalizeOutput(); |
102 | 0 | virtual ~OSequenceOutputStream() override { if (m_bConnected) finalizeOutput(); } |
103 | | |
104 | | public: |
105 | | /** constructs the object. Everything written into the stream through the XOutputStream methods will be forwarded |
106 | | to the sequence, reallocating it if necessary. Writing will start at offset 0 within the sequence. |
107 | | @param _rSeq a reference to the sequence which will be used for output. |
108 | | The caller is responsible for taking care of the lifetime of the stream |
109 | | object and the sequence. If you're in doubt about this, use <code>closeOutput</code> |
110 | | before destroying the sequence |
111 | | @param _nResizeFactor the factor which is used for resizing the sequence when necessary. In every |
112 | | resize step, the new sequence size will be calculated by multiplying the current |
113 | | size with this factor, rounded off to the next multiple of 4. |
114 | | @param _nMinimumResize the minimal number of bytes which is additionally allocated on resizing |
115 | | @see closeOutput |
116 | | */ |
117 | | OSequenceOutputStream( |
118 | | css::uno::Sequence< sal_Int8 >& _rSeq, |
119 | | double _nResizeFactor = 1.3, |
120 | | sal_Int32 _nMinimumResize = 128 |
121 | | ); |
122 | | |
123 | | /// same as XOutputStream::writeBytes (as expected :) |
124 | | virtual void SAL_CALL writeBytes( const css::uno::Sequence< sal_Int8 >& aData ) override; |
125 | | /// this is a dummy in this implementation, no buffering is used |
126 | | virtual void SAL_CALL flush( ) override; |
127 | | /** closes the output stream. In the case of this class, this means that the sequence used for writing is |
128 | | resized to the really used size and not used any further, every subsequent call to one of the XOutputStream |
129 | | methods will throw a <code>NotConnectedException</code>. |
130 | | */ |
131 | | virtual void SAL_CALL closeOutput( ) override; |
132 | | }; |
133 | | |
134 | | } // namespace comphelper |
135 | | |
136 | | #endif // INCLUDED_COMPHELPER_SEQSTREAM_HXX |
137 | | |
138 | | |
139 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |