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