KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > poi > poifs > filesystem > DocumentInputStream


1
2 /* ====================================================================
3    Copyright 2002-2004 Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16 ==================================================================== */

17         
18
19 package org.apache.poi.poifs.filesystem;
20
21 import java.io.*;
22
23 /**
24  * This class provides methods to read a DocumentEntry managed by a
25  * Filesystem instance.
26  *
27  * @author Marc Johnson (mjohnson at apache dot org)
28  */

29
30 public class DocumentInputStream
31     extends InputStream
32 {
33
34     // current offset into the Document
35
private int _current_offset;
36
37     // current marked offset into the Document (used by mark and
38
// reset)
39
private int _marked_offset;
40
41     // the Document's size
42
private int _document_size;
43
44     // have we been closed?
45
private boolean _closed;
46
47     // the actual Document
48
private POIFSDocument _document;
49
50     // buffer used to read one byte at a time
51
private byte[] _tiny_buffer;
52
53     // returned by read operations if we're at end of document
54
static private final int EOD = -1;
55
56     /**
57      * Create an InputStream from the specified DocumentEntry
58      *
59      * @param document the DocumentEntry to be read
60      *
61      * @exception IOException if the DocumentEntry cannot be opened
62      * (like, maybe it has been deleted?)
63      */

64
65     public DocumentInputStream(final DocumentEntry document)
66         throws IOException
67     {
68         _current_offset = 0;
69         _marked_offset = 0;
70         _document_size = document.getSize();
71         _closed = false;
72         _tiny_buffer = null;
73         if (document instanceof DocumentNode)
74         {
75             _document = (( DocumentNode ) document).getDocument();
76         }
77         else
78         {
79             throw new IOException("Cannot open internal document storage");
80         }
81     }
82
83     /**
84      * Create an InputStream from the specified Document
85      *
86      * @param document the Document to be read
87      *
88      * @exception IOException if the DocumentEntry cannot be opened
89      * (like, maybe it has been deleted?)
90      */

91
92     public DocumentInputStream(final POIFSDocument document)
93         throws IOException
94     {
95         _current_offset = 0;
96         _marked_offset = 0;
97         _document_size = document.getSize();
98         _closed = false;
99         _tiny_buffer = null;
100         _document = document;
101     }
102
103     /**
104      * Returns the number of bytes that can be read (or skipped over)
105      * from this input stream without blocking by the next caller of a
106      * method for this input stream. The next caller might be the same
107      * thread or or another thread.
108      *
109      * @return the number of bytes that can be read from this input
110      * stream without blocking.
111      *
112      * @exception IOException on error (such as the stream has been
113      * closed)
114      */

115
116     public int available()
117         throws IOException
118     {
119         dieIfClosed();
120         return _document_size - _current_offset;
121     }
122
123     /**
124      * Closes this input stream and releases any system resources
125      * associated with the stream.
126      *
127      * @exception IOException
128      */

129
130     public void close()
131         throws IOException
132     {
133         _closed = true;
134     }
135
136     /**
137      * Marks the current position in this input stream. A subsequent
138      * call to the reset method repositions this stream at the last
139      * marked position so that subsequent reads re-read the same
140      * bytes.
141      * <p>
142      * The readlimit arguments tells this input stream to allow that
143      * many bytes to be read before the mark position gets
144      * invalidated. This implementation, however, does not care.
145      * <p>
146      * The general contract of mark is that, if the method
147      * markSupported returns true, the stream somehow remembers all
148      * the bytes read after the call to mark and stands ready to
149      * supply those same bytes again if and whenever the method reset
150      * is called. However, the stream is not required to remember any
151      * data at all if more than readlimit bytes are read from the
152      * stream before reset is called. But this stream will.
153      *
154      * @param ignoredReadlimit the maximum limit of bytes that can be
155      * read before the mark position becomes
156      * invalid. Ignored by this
157      * implementation.
158      */

159
160     public void mark(int ignoredReadlimit)
161     {
162         _marked_offset = _current_offset;
163     }
164
165     /**
166      * Tests if this input stream supports the mark and reset methods.
167      *
168      * @return true
169      */

170
171     public boolean markSupported()
172     {
173         return true;
174     }
175
176     /**
177      * Reads the next byte of data from the input stream. The value
178      * byte is returned as an int in the range 0 to 255. If no byte is
179      * available because the end of the stream has been reached, the
180      * value -1 is returned. The definition of this method in
181      * java.io.InputStream allows this method to block, but it won't.
182      *
183      * @return the next byte of data, or -1 if the end of the stream
184      * is reached.
185      *
186      * @exception IOException
187      */

188
189     public int read()
190         throws IOException
191     {
192         dieIfClosed();
193         if (atEOD())
194         {
195             return EOD;
196         }
197         if (_tiny_buffer == null)
198         {
199             _tiny_buffer = new byte[ 1 ];
200         }
201         _document.read(_tiny_buffer, _current_offset++);
202         return ((int)_tiny_buffer[ 0 ]) & 0x000000FF;
203     }
204
205     /**
206      * Reads some number of bytes from the input stream and stores
207      * them into the buffer array b. The number of bytes actually read
208      * is returned as an integer. The definition of this method in
209      * java.io.InputStream allows this method to block, but it won't.
210      * <p>
211      * If b is null, a NullPointerException is thrown. If the length
212      * of b is zero, then no bytes are read and 0 is returned;
213      * otherwise, there is an attempt to read at least one byte. If no
214      * byte is available because the stream is at end of file, the
215      * value -1 is returned; otherwise, at least one byte is read and
216      * stored into b.
217      * <p>
218      * The first byte read is stored into element b[0], the next one
219      * into b[1], and so on. The number of bytes read is, at most,
220      * equal to the length of b. Let k be the number of bytes actually
221      * read; these bytes will be stored in elements b[0] through
222      * b[k-1], leaving elements b[k] through b[b.length-1] unaffected.
223      * <p>
224      * If the first byte cannot be read for any reason other than end
225      * of file, then an IOException is thrown. In particular, an
226      * IOException is thrown if the input stream has been closed.
227      * <p>
228      * The read(b) method for class InputStream has the same effect as:
229      * <p>
230      * <code>read(b, 0, b.length)</code>
231      *
232      * @param b the buffer into which the data is read.
233      *
234      * @return the total number of bytes read into the buffer, or -1
235      * if there is no more data because the end of the stream
236      * has been reached.
237      *
238      * @exception IOException
239      * @exception NullPointerException
240      */

241
242     public int read(final byte [] b)
243         throws IOException, NullPointerException JavaDoc
244     {
245         return read(b, 0, b.length);
246     }
247
248     /**
249      * Reads up to len bytes of data from the input stream into an
250      * array of bytes. An attempt is made to read as many as len
251      * bytes, but a smaller number may be read, possibly zero. The
252      * number of bytes actually read is returned as an integer.
253      * <p>
254      * The definition of this method in java.io.InputStream allows it
255      * to block, but it won't.
256      * <p>
257      * If b is null, a NullPointerException is thrown.
258      * <p>
259      * If off is negative, or len is negative, or off+len is greater
260      * than the length of the array b, then an
261      * IndexOutOfBoundsException is thrown.
262      * <p>
263      * If len is zero, then no bytes are read and 0 is returned;
264      * otherwise, there is an attempt to read at least one byte. If no
265      * byte is available because the stream is at end of file, the
266      * value -1 is returned; otherwise, at least one byte is read and
267      * stored into b.
268      * <p>
269      * The first byte read is stored into element b[off], the next one
270      * into b[off+1], and so on. The number of bytes read is, at most,
271      * equal to len. Let k be the number of bytes actually read; these
272      * bytes will be stored in elements b[off] through b[off+k-1],
273      * leaving elements b[off+k] through b[off+len-1] unaffected.
274      * <p>
275      * In every case, elements b[0] through b[off] and elements
276      * b[off+len] through b[b.length-1] are unaffected.
277      * <p>
278      * If the first byte cannot be read for any reason other than end
279      * of file, then an IOException is thrown. In particular, an
280      * IOException is thrown if the input stream has been closed.
281      *
282      * @param b the buffer into which the data is read.
283      * @param off the start offset in array b at which the data is
284      * written.
285      * @param len the maximum number of bytes to read.
286      *
287      * @return the total number of bytes read into the buffer, or -1
288      * if there is no more data because the end of the stream
289      * has been reached.
290      *
291      * @exception IOException
292      * @exception NullPointerException
293      * @exception IndexOutOfBoundsException
294      */

295
296     public int read(final byte [] b, final int off, final int len)
297         throws IOException, NullPointerException JavaDoc, IndexOutOfBoundsException JavaDoc
298     {
299         dieIfClosed();
300         if (b == null)
301         {
302             throw new NullPointerException JavaDoc("buffer is null");
303         }
304         if ((off < 0) || (len < 0) || (b.length < (off + len)))
305         {
306             throw new IndexOutOfBoundsException JavaDoc(
307                 "can't read past buffer boundaries");
308         }
309         if (len == 0)
310         {
311             return 0;
312         }
313         if (atEOD())
314         {
315             return EOD;
316         }
317         int limit = Math.min(available(), len);
318
319         if ((off == 0) && (limit == b.length))
320         {
321             _document.read(b, _current_offset);
322         }
323         else
324         {
325             byte[] buffer = new byte[ limit ];
326
327             _document.read(buffer, _current_offset);
328             System.arraycopy(buffer, 0, b, off, limit);
329         }
330         _current_offset += limit;
331         return limit;
332     }
333
334     /**
335      * Repositions this stream to the position at the time the mark
336      * method was last called on this input stream.
337      * <p>
338      * The general contract of reset is:
339      * <p>
340      * <ul>
341      * <li>
342      * If the method markSupported returns true, then:
343      * <ul>
344      * <li>
345      * If the method mark has not been called since the
346      * stream was created, or the number of bytes read
347      * from the stream since mark was last called is
348      * larger than the argument to mark at that last
349      * call, then an IOException might be thrown.
350      * </li>
351      * <li>
352      * If such an IOException is not thrown, then the
353      * stream is reset to a state such that all the
354      * bytes read since the most recent call to mark
355      * (or since the start of the file, if mark has not
356      * been called) will be resupplied to subsequent
357      * callers of the read method, followed by any
358      * bytes that otherwise would have been the next
359      * input data as of the time of the call to reset.
360      * </li>
361      * </ul>
362      * </li>
363      * <li>
364      * If the method markSupported returns false, then:
365      * <ul>
366      * <li>
367      * The call to reset may throw an IOException.
368      * </li>
369      * <li>
370      * If an IOException is not thrown, then the
371      * stream is reset to a fixed state that depends
372      * on the particular type of the input and how it
373      * was created. The bytes that will be supplied to
374      * subsequent callers of the read method depend on
375      * the particular type of the input stream.
376      * </li>
377      * </ul>
378      * </li>
379      * </ul>
380      * <p>
381      * All well and good ... this class's markSupported method returns
382      * true and this method does not care whether you've called mark
383      * at all, or whether you've exceeded the number of bytes
384      * specified in the last call to mark. We're basically walking a
385      * byte array ... mark and reset to your heart's content.
386      */

387
388     public void reset()
389     {
390         _current_offset = _marked_offset;
391     }
392
393     /**
394      * Skips over and discards n bytes of data from this input
395      * stream. The skip method may, for a variety of reasons, end up
396      * skipping over some smaller number of bytes, possibly 0. This
397      * may result from any of a number of conditions; reaching end of
398      * file before n bytes have been skipped is only one
399      * possibility. The actual number of bytes skipped is returned. If
400      * n is negative, no bytes are skipped.
401      *
402      * @param n the number of bytes to be skipped.
403      *
404      * @return the actual number of bytes skipped.
405      *
406      * @exception IOException
407      */

408
409     public long skip(final long n)
410         throws IOException
411     {
412         dieIfClosed();
413         if (n < 0)
414         {
415             return 0;
416         }
417         int new_offset = _current_offset + ( int ) n;
418
419         if (new_offset < _current_offset)
420         {
421
422             // wrap around in converting a VERY large long to an int
423
new_offset = _document_size;
424         }
425         else if (new_offset > _document_size)
426         {
427             new_offset = _document_size;
428         }
429         long rval = new_offset - _current_offset;
430
431         _current_offset = new_offset;
432         return rval;
433     }
434
435     private void dieIfClosed()
436         throws IOException
437     {
438         if (_closed)
439         {
440             throw new IOException(
441                 "cannot perform requested operation on a closed stream");
442         }
443     }
444
445     private boolean atEOD()
446     {
447         return _current_offset == _document_size;
448     }
449 } // end public class DocumentInputStream
450

451
Popular Tags