1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <cppuhelper/implbase.hxx>
28 #include <comphelper/comphelperdllapi.h>
29 #include <comphelper/bytereader.hxx>
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
41 const sal_Int8
* m_pMemoryData
;
42 sal_Int32 m_nMemoryDataLength
;
46 MemoryInputStream(const sal_Int8
* pData
, sal_Int32 nDataLength
);
48 // css::io::XInputStream
49 virtual sal_Int32 SAL_CALL
readBytes( css::uno::Sequence
<sal_Int8
>& aData
, sal_Int32 nBytesToRead
) override final
;
51 virtual sal_Int32 SAL_CALL
readSomeBytes( css::uno::Sequence
<sal_Int8
>& aData
, sal_Int32 nMaxBytesToRead
) override final
;
53 virtual void SAL_CALL
skipBytes( sal_Int32 nBytesToSkip
) override final
;
55 virtual sal_Int32 SAL_CALL
available( ) override final
;
57 virtual void SAL_CALL
closeInput( ) override final
;
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
;
63 // comphelper::ByteReader
64 virtual sal_Int32
readSomeBytes( sal_Int8
* pData
, sal_Int32 nBytesToRead
) override final
;
71 // Stream for reading data from a sequence of bytes
72 class COMPHELPER_DLLPUBLIC SequenceInputStream final
73 : public MemoryInputStream
75 css::uno::Sequence
<sal_Int8
> const m_aData
;
78 SequenceInputStream(css::uno::Sequence
<sal_Int8
> const & rData
);
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
>
86 class UNLESS_MERGELIBS(COMPHELPER_DLLPUBLIC
) OSequenceOutputStream final
: public OSequenceOutputStream_Base
89 css::uno::Sequence
< sal_Int8
>& m_rSequence
;
90 double m_nResizeFactor
;
91 sal_Int32
const m_nMinimumResize
;
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.
97 // closeOutput has been called ?
101 void finalizeOutput();
102 virtual ~OSequenceOutputStream() override
{ if (m_bConnected
) finalizeOutput(); }
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
117 OSequenceOutputStream(
118 css::uno::Sequence
< sal_Int8
>& _rSeq
,
119 double _nResizeFactor
= 1.3,
120 sal_Int32 _nMinimumResize
= 128
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>.
131 virtual void SAL_CALL
closeOutput( ) override
;
134 } // namespace comphelper
136 #endif // INCLUDED_COMPHELPER_SEQSTREAM_HXX
139 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */