Version 5.4.3.2, tag libreoffice-5.4.3.2
[LibreOffice.git] / include / comphelper / seqstream.hxx
blobae7502f776e4c433a3e9a8ff0a8e837b988beb55
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 <com/sun/star/uno/Sequence.hxx>
23 #include <com/sun/star/io/XInputStream.hpp>
24 #include <com/sun/star/io/XOutputStream.hpp>
25 #include <com/sun/star/io/XSeekable.hpp>
26 #include <osl/mutex.hxx>
27 #include <cppuhelper/implbase.hxx>
28 #include <comphelper/comphelperdllapi.h>
30 namespace comphelper
34 // SequenceInputStream
35 // stream for reading data from a sequence of bytes
38 class COMPHELPER_DLLPUBLIC SequenceInputStream
39 : public ::cppu::WeakImplHelper< css::io::XInputStream, css::io::XSeekable >
41 ::osl::Mutex m_aMutex;
42 css::uno::Sequence<sal_Int8> m_aData;
43 sal_Int32 m_nPos;
45 public:
46 SequenceInputStream(css::uno::Sequence<sal_Int8> const & rData);
48 // css::io::XInputStream
49 virtual sal_Int32 SAL_CALL readBytes( css::uno::Sequence<sal_Int8>& aData, sal_Int32 nBytesToRead ) override;
51 virtual sal_Int32 SAL_CALL readSomeBytes( css::uno::Sequence<sal_Int8>& aData, sal_Int32 nMaxBytesToRead ) override;
53 virtual void SAL_CALL skipBytes( sal_Int32 nBytesToSkip ) override;
55 virtual sal_Int32 SAL_CALL available( ) override;
57 virtual void SAL_CALL closeInput( ) override;
59 virtual void SAL_CALL seek( sal_Int64 location ) override;
60 virtual sal_Int64 SAL_CALL getPosition( ) override;
61 virtual sal_Int64 SAL_CALL getLength( ) override;
63 private:
64 inline sal_Int32 avail();
67 // don't export to avoid duplicate WeakImplHelper definitions with MSVC
68 class SAL_DLLPUBLIC_TEMPLATE OSequenceOutputStream_Base
69 : public ::cppu::WeakImplHelper< css::io::XOutputStream >
70 {};
72 class COMPHELPER_DLLPUBLIC OSequenceOutputStream : public OSequenceOutputStream_Base
74 private:
75 void finalizeOutput();
76 protected:
77 css::uno::Sequence< sal_Int8 >& m_rSequence;
78 double m_nResizeFactor;
79 sal_Int32 m_nMinimumResize;
80 sal_Int32 m_nSize;
81 // the size of the virtual stream. This is not the size of the sequence, but the number of bytes written
82 // into the stream at a given moment.
84 bool m_bConnected;
85 // closeOutput has been called ?
87 ::osl::Mutex m_aMutex;
89 protected:
90 virtual ~OSequenceOutputStream() override { if (m_bConnected) finalizeOutput(); }
92 public:
93 /** constructs the object. Everything written into the stream through the XOutputStream methods will be forwarded
94 to the sequence, reallocating it if necessary. Writing will start at offset 0 within the sequence.
95 @param _rSeq a reference to the sequence which will be used for output.
96 The caller is responsible for taking care of the lifetime of the stream
97 object and the sequence. If you're in doubt about this, use <code>closeOutput</code>
98 before destroying the sequence
99 @param _nResizeFactor the factor which is used for resizing the sequence when necessary. In every
100 resize step, the new sequence size will be calculated by multiplying the current
101 size with this factor, rounded off to the next multiple of 4.
102 @param _nMinimumResize the minimal number of bytes which is additionally allocated on resizing
103 @see closeOutput
105 OSequenceOutputStream(
106 css::uno::Sequence< sal_Int8 >& _rSeq,
107 double _nResizeFactor = 1.3,
108 sal_Int32 _nMinimumResize = 128
111 /// same as XOutputStream::writeBytes (as expected :)
112 virtual void SAL_CALL writeBytes( const css::uno::Sequence< sal_Int8 >& aData ) override;
113 /// this is a dummy in this implementation, no buffering is used
114 virtual void SAL_CALL flush( ) override;
115 /** closes the output stream. In the case of this class, this means that the sequence used for writing is
116 resized to the really used size and not used any further, every subsequent call to one of the XOutputStream
117 methods will throw a <code>NotConnectedException</code>.
119 virtual void SAL_CALL closeOutput( ) override;
122 } // namespace comphelper
124 #endif // INCLUDED_COMPHELPER_SEQSTREAM_HXX
127 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */