license: libvisio is MPLv2
[LibreOffice.git] / include / comphelper / seqstream.hxx
blob656b2a25899e469842248e2e8c96518fc24280dc
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> const 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 final : public OSequenceOutputStream_Base
74 private:
75 void finalizeOutput();
76 css::uno::Sequence< sal_Int8 >& m_rSequence;
77 double m_nResizeFactor;
78 sal_Int32 const m_nMinimumResize;
79 sal_Int32 m_nSize;
80 // the size of the virtual stream. This is not the size of the sequence, but the number of bytes written
81 // into the stream at a given moment.
83 bool m_bConnected;
84 // closeOutput has been called ?
86 ::osl::Mutex m_aMutex;
88 virtual ~OSequenceOutputStream() override { if (m_bConnected) finalizeOutput(); }
90 public:
91 /** constructs the object. Everything written into the stream through the XOutputStream methods will be forwarded
92 to the sequence, reallocating it if necessary. Writing will start at offset 0 within the sequence.
93 @param _rSeq a reference to the sequence which will be used for output.
94 The caller is responsible for taking care of the lifetime of the stream
95 object and the sequence. If you're in doubt about this, use <code>closeOutput</code>
96 before destroying the sequence
97 @param _nResizeFactor the factor which is used for resizing the sequence when necessary. In every
98 resize step, the new sequence size will be calculated by multiplying the current
99 size with this factor, rounded off to the next multiple of 4.
100 @param _nMinimumResize the minimal number of bytes which is additionally allocated on resizing
101 @see closeOutput
103 OSequenceOutputStream(
104 css::uno::Sequence< sal_Int8 >& _rSeq,
105 double _nResizeFactor = 1.3,
106 sal_Int32 _nMinimumResize = 128
109 /// same as XOutputStream::writeBytes (as expected :)
110 virtual void SAL_CALL writeBytes( const css::uno::Sequence< sal_Int8 >& aData ) override;
111 /// this is a dummy in this implementation, no buffering is used
112 virtual void SAL_CALL flush( ) override;
113 /** closes the output stream. In the case of this class, this means that the sequence used for writing is
114 resized to the really used size and not used any further, every subsequent call to one of the XOutputStream
115 methods will throw a <code>NotConnectedException</code>.
117 virtual void SAL_CALL closeOutput( ) override;
120 } // namespace comphelper
122 #endif // INCLUDED_COMPHELPER_SEQSTREAM_HXX
125 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */