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 .
20 #ifndef INCLUDED_OOX_HELPER_BINARYSTREAMBASE_HXX
21 #define INCLUDED_OOX_HELPER_BINARYSTREAMBASE_HXX
23 #include <com/sun/star/uno/Reference.hxx>
24 #include <com/sun/star/uno/Sequence.hxx>
25 #include <oox/dllapi.h>
26 #include <sal/types.h>
28 namespace com::sun::star
{
29 namespace io
{ class XSeekable
; }
34 typedef css::uno::Sequence
< sal_Int8
> StreamDataSequence
;
37 /** Base class for binary stream classes.
39 class OOX_DLLPUBLIC BinaryStreamBase
42 virtual ~BinaryStreamBase();
44 /** Implementations return the size of the stream, if possible.
46 This function may be implemented for some types of unseekable streams,
47 and MUST be implemented for all seekable streams.
50 The size of the stream in bytes, or -1, if not implemented.
52 virtual sal_Int64
size() const = 0;
54 /** Implementations return the current stream position, if possible.
56 This function may be implemented for some types of unseekable streams,
57 and MUST be implemented for all seekable streams.
60 The current position in the stream, or -1, if not implemented.
62 virtual sal_Int64
tell() const = 0;
64 /** Implementations seek the stream to the passed position, if
65 the stream is seekable.
67 virtual void seek( sal_Int64 nPos
) = 0;
69 /** Implementations close the stream.
71 virtual void close() = 0;
73 /** Returns true, if the implementation supports the seek() operation.
75 Implementations may still implement size() and tell() even if the
76 stream is not seekable.
78 bool isSeekable() const { return mbSeekable
; }
80 /** Returns true, if the stream position is invalid (EOF). This flag turns
81 true *after* the first attempt to seek/read beyond the stream end.
83 bool isEof() const { return mbEof
; }
85 /** Returns the size of the remaining data available in the stream, if
86 stream supports size() and tell(), otherwise -1.
88 sal_Int64
getRemaining() const;
90 /** Seeks the stream to the beginning, if stream is seekable.
92 void seekToStart() { seek( 0 ); }
94 /** Seeks the stream forward to a position that is a multiple of the passed
95 block size, if stream is seekable.
98 The size of the data blocks the streams needs to be aligned to.
101 Position in the stream the data blocks are aligned to.
103 void alignToBlock( sal_Int32 nBlockSize
, sal_Int64 nAnchorPos
);
106 explicit BinaryStreamBase( bool bSeekable
) : mbEof( false ), mbSeekable( bSeekable
) {}
109 BinaryStreamBase( const BinaryStreamBase
& ) = delete;
110 BinaryStreamBase
& operator=( const BinaryStreamBase
& ) = delete;
113 bool mbEof
; ///< End of stream flag.
116 const bool mbSeekable
; ///< True = implementation supports seeking.
120 /** Base class for binary input and output streams wrapping a UNO stream,
121 seekable via the com.sun.star.io.XSeekable interface.
123 class BinaryXSeekableStream
: public virtual BinaryStreamBase
126 virtual ~BinaryXSeekableStream() override
;
128 /** Returns the size of the stream, if wrapped stream is seekable, otherwise -1. */
129 virtual sal_Int64
size() const override
;
130 /** Returns the current stream position, if wrapped stream is seekable, otherwise -1. */
131 virtual sal_Int64
tell() const override
;
132 /** Seeks the stream to the passed position, if wrapped stream is seekable. */
133 virtual void seek( sal_Int64 nPos
) override
;
134 /** Releases the reference to the UNO XSeekable interface. */
135 virtual void close() override
;
138 explicit BinaryXSeekableStream(
139 const css::uno::Reference
< css::io::XSeekable
>& rxSeekable
);
142 css::uno::Reference
< css::io::XSeekable
>
143 mxSeekable
; ///< Stream seeking interface.
147 /** Base class for binary input and output streams wrapping a
148 StreamDataSequence, which is always seekable.
150 The wrapped data sequence MUST live at least as long as this stream
151 wrapper. The data sequence MUST NOT be changed from outside as long as this
152 stream wrapper is used to modify it.
154 class OOX_DLLPUBLIC SequenceSeekableStream
: public virtual BinaryStreamBase
157 /** Returns the size of the wrapped data sequence. */
158 virtual sal_Int64
size() const override
;
159 /** Returns the current stream position. */
160 virtual sal_Int64
tell() const override
;
161 /** Seeks the stream to the passed position. */
162 virtual void seek( sal_Int64 nPos
) override
;
163 /** Releases the reference to the data sequence. */
164 virtual void close() override
;
167 explicit SequenceSeekableStream( const StreamDataSequence
& rData
);
170 const StreamDataSequence
* mpData
; ///< Wrapped data sequence.
171 sal_Int32 mnPos
; ///< Current position in the sequence.
179 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */