1*e3508121SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*e3508121SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*e3508121SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*e3508121SAndrew Rist  * distributed with this work for additional information
6*e3508121SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*e3508121SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*e3508121SAndrew Rist  * "License"); you may not use this file except in compliance
9*e3508121SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*e3508121SAndrew Rist  *
11*e3508121SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*e3508121SAndrew Rist  *
13*e3508121SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*e3508121SAndrew Rist  * software distributed under the License is distributed on an
15*e3508121SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*e3508121SAndrew Rist  * KIND, either express or implied.  See the License for the
17*e3508121SAndrew Rist  * specific language governing permissions and limitations
18*e3508121SAndrew Rist  * under the License.
19*e3508121SAndrew Rist  *
20*e3508121SAndrew Rist  *************************************************************/
21*e3508121SAndrew Rist 
22*e3508121SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #ifndef OOX_HELPER_BINARYSTREAMBASE_HXX
25cdf0e10cSrcweir #define OOX_HELPER_BINARYSTREAMBASE_HXX
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include <com/sun/star/uno/Sequence.hxx>
28cdf0e10cSrcweir #include <boost/shared_ptr.hpp>
29cdf0e10cSrcweir #include "oox/helper/helper.hxx"
30cdf0e10cSrcweir 
31cdf0e10cSrcweir namespace com { namespace sun { namespace star {
32cdf0e10cSrcweir     namespace io { class XSeekable; }
33cdf0e10cSrcweir } } }
34cdf0e10cSrcweir 
35cdf0e10cSrcweir namespace oox {
36cdf0e10cSrcweir 
37cdf0e10cSrcweir typedef ::com::sun::star::uno::Sequence< sal_Int8 > StreamDataSequence;
38cdf0e10cSrcweir 
39cdf0e10cSrcweir // ============================================================================
40cdf0e10cSrcweir 
41cdf0e10cSrcweir /** Base class for binary stream classes.
42cdf0e10cSrcweir  */
43cdf0e10cSrcweir class BinaryStreamBase
44cdf0e10cSrcweir {
45cdf0e10cSrcweir public:
46cdf0e10cSrcweir     virtual             ~BinaryStreamBase();
47cdf0e10cSrcweir 
48cdf0e10cSrcweir     /** Implementations return the size of the stream, if possible.
49cdf0e10cSrcweir 
50cdf0e10cSrcweir         This function may be implemented for some types of unseekable streams,
51cdf0e10cSrcweir         and MUST be implemented for all seekable streams.
52cdf0e10cSrcweir 
53cdf0e10cSrcweir         @return
54cdf0e10cSrcweir             The size of the stream in bytes, or -1, if not implemented.
55cdf0e10cSrcweir      */
56cdf0e10cSrcweir     virtual sal_Int64   size() const = 0;
57cdf0e10cSrcweir 
58cdf0e10cSrcweir     /** Implementations return the current stream position, if possible.
59cdf0e10cSrcweir 
60cdf0e10cSrcweir         This function may be implemented for some types of unseekable streams,
61cdf0e10cSrcweir         and MUST be implemented for all seekable streams.
62cdf0e10cSrcweir 
63cdf0e10cSrcweir         @return
64cdf0e10cSrcweir             The current position in the stream, or -1, if not implemented.
65cdf0e10cSrcweir      */
66cdf0e10cSrcweir     virtual sal_Int64   tell() const = 0;
67cdf0e10cSrcweir 
68cdf0e10cSrcweir     /** Implementations seek the stream to the passed position, if
69cdf0e10cSrcweir         the stream is seekable.
70cdf0e10cSrcweir      */
71cdf0e10cSrcweir     virtual void        seek( sal_Int64 nPos ) = 0;
72cdf0e10cSrcweir 
73cdf0e10cSrcweir     /** Implementations close the stream.
74cdf0e10cSrcweir      */
75cdf0e10cSrcweir     virtual void        close() = 0;
76cdf0e10cSrcweir 
77cdf0e10cSrcweir     /** Returns true, if the implementation supports the seek() operation.
78cdf0e10cSrcweir 
79cdf0e10cSrcweir         Implementations may still implement size() and tell() even if the
80cdf0e10cSrcweir         stream is not seekable.
81cdf0e10cSrcweir      */
isSeekable() const82cdf0e10cSrcweir     inline bool         isSeekable() const { return mbSeekable; }
83cdf0e10cSrcweir 
84cdf0e10cSrcweir     /** Returns true, if the stream position is invalid (EOF). This flag turns
85cdf0e10cSrcweir         true *after* the first attempt to seek/read beyond the stream end.
86cdf0e10cSrcweir      */
isEof() const87cdf0e10cSrcweir     inline bool         isEof() const { return mbEof; }
88cdf0e10cSrcweir 
89cdf0e10cSrcweir     /** Returns the size of the remaining data available in the stream, if
90cdf0e10cSrcweir         stream supports size() and tell(), otherwise -1.
91cdf0e10cSrcweir      */
92cdf0e10cSrcweir     sal_Int64           getRemaining() const;
93cdf0e10cSrcweir 
94cdf0e10cSrcweir     /** Seeks the stream to the beginning, if stream is seekable.
95cdf0e10cSrcweir      */
seekToStart()96cdf0e10cSrcweir     inline void         seekToStart() { seek( 0 ); }
97cdf0e10cSrcweir 
98cdf0e10cSrcweir     /** Seeks the stream to the end, if stream is seekable.
99cdf0e10cSrcweir      */
seekToEnd()100cdf0e10cSrcweir     inline void         seekToEnd() { seek( size() ); }
101cdf0e10cSrcweir 
102cdf0e10cSrcweir     /** Seeks the stream forward to a position that is a multiple of the passed
103cdf0e10cSrcweir         block size, if stream is seekable.
104cdf0e10cSrcweir 
105cdf0e10cSrcweir         @param nBlockSize
106cdf0e10cSrcweir             The size of the data blocks the streams needs to be aligned to.
107cdf0e10cSrcweir 
108cdf0e10cSrcweir         @param nAnchorPos
109cdf0e10cSrcweir             Position in the stream the data blocks are aligned to.
110cdf0e10cSrcweir      */
111cdf0e10cSrcweir     void                alignToBlock( sal_Int32 nBlockSize, sal_Int64 nAnchorPos = 0 );
112cdf0e10cSrcweir 
113cdf0e10cSrcweir protected:
BinaryStreamBase(bool bSeekable)114cdf0e10cSrcweir     inline explicit     BinaryStreamBase( bool bSeekable ) : mbEof( false ), mbSeekable( bSeekable ) {}
115cdf0e10cSrcweir 
116cdf0e10cSrcweir private:
117cdf0e10cSrcweir                         BinaryStreamBase( const BinaryStreamBase& );
118cdf0e10cSrcweir     BinaryStreamBase&   operator=( const BinaryStreamBase& );
119cdf0e10cSrcweir 
120cdf0e10cSrcweir protected:
121cdf0e10cSrcweir     bool                mbEof;          /// End of stream flag.
122cdf0e10cSrcweir 
123cdf0e10cSrcweir private:
124cdf0e10cSrcweir     const bool          mbSeekable;     /// True = implementation supports seeking.
125cdf0e10cSrcweir };
126cdf0e10cSrcweir 
127cdf0e10cSrcweir // ============================================================================
128cdf0e10cSrcweir 
129cdf0e10cSrcweir /** Base class for binary input and output streams wrapping a UNO stream,
130cdf0e10cSrcweir     seekable via the com.sun.star.io.XSeekable interface.
131cdf0e10cSrcweir  */
132cdf0e10cSrcweir class BinaryXSeekableStream : public virtual BinaryStreamBase
133cdf0e10cSrcweir {
134cdf0e10cSrcweir public:
135cdf0e10cSrcweir     virtual             ~BinaryXSeekableStream();
136cdf0e10cSrcweir 
137cdf0e10cSrcweir     /** Returns the size of the stream, if wrapped stream is seekable, otherwise -1. */
138cdf0e10cSrcweir     virtual sal_Int64   size() const;
139cdf0e10cSrcweir     /** Returns the current stream position, if wrapped stream is seekable, otherwise -1. */
140cdf0e10cSrcweir     virtual sal_Int64   tell() const;
141cdf0e10cSrcweir     /** Seeks the stream to the passed position, if wrapped stream is seekable. */
142cdf0e10cSrcweir     virtual void        seek( sal_Int64 nPos );
143cdf0e10cSrcweir     /** Releases the reference to the UNO XSeekable interface. */
144cdf0e10cSrcweir     virtual void        close();
145cdf0e10cSrcweir 
146cdf0e10cSrcweir protected:
147cdf0e10cSrcweir     explicit            BinaryXSeekableStream(
148cdf0e10cSrcweir                             const ::com::sun::star::uno::Reference< ::com::sun::star::io::XSeekable >& rxSeekable );
149cdf0e10cSrcweir 
150cdf0e10cSrcweir private:
151cdf0e10cSrcweir     ::com::sun::star::uno::Reference< ::com::sun::star::io::XSeekable >
152cdf0e10cSrcweir                         mxSeekable;     /// Stream seeking interface.
153cdf0e10cSrcweir };
154cdf0e10cSrcweir 
155cdf0e10cSrcweir // ============================================================================
156cdf0e10cSrcweir 
157cdf0e10cSrcweir /** Base class for binary input and output streams wrapping a
158cdf0e10cSrcweir     StreamDataSequence, which is always seekable.
159cdf0e10cSrcweir 
160cdf0e10cSrcweir     The wrapped data sequence MUST live at least as long as this stream
161cdf0e10cSrcweir     wrapper. The data sequence MUST NOT be changed from outside as long as this
162cdf0e10cSrcweir     stream wrapper is used to modify it.
163cdf0e10cSrcweir  */
164cdf0e10cSrcweir class SequenceSeekableStream : public virtual BinaryStreamBase
165cdf0e10cSrcweir {
166cdf0e10cSrcweir public:
167cdf0e10cSrcweir     /** Returns the size of the wrapped data sequence. */
168cdf0e10cSrcweir     virtual sal_Int64   size() const;
169cdf0e10cSrcweir     /** Returns the current stream position. */
170cdf0e10cSrcweir     virtual sal_Int64   tell() const;
171cdf0e10cSrcweir     /** Seeks the stream to the passed position. */
172cdf0e10cSrcweir     virtual void        seek( sal_Int64 nPos );
173cdf0e10cSrcweir     /** Releases the reference to the data sequence. */
174cdf0e10cSrcweir     virtual void        close();
175cdf0e10cSrcweir 
176cdf0e10cSrcweir protected:
177cdf0e10cSrcweir     explicit            SequenceSeekableStream( const StreamDataSequence& rData );
178cdf0e10cSrcweir 
179cdf0e10cSrcweir protected:
180cdf0e10cSrcweir     const StreamDataSequence* mpData;   /// Wrapped data sequence.
181cdf0e10cSrcweir     sal_Int32           mnPos;          /// Current position in the sequence.
182cdf0e10cSrcweir };
183cdf0e10cSrcweir 
184cdf0e10cSrcweir // ============================================================================
185cdf0e10cSrcweir 
186cdf0e10cSrcweir } // namespace oox
187cdf0e10cSrcweir 
188cdf0e10cSrcweir #endif
189