KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > ext > awt > image > codec > SeekableStream


1 /*
2
3    Copyright 2001,2003 The 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 package org.apache.batik.ext.awt.image.codec;
19
20 import java.io.DataInput JavaDoc;
21 import java.io.DataInputStream JavaDoc;
22 import java.io.EOFException JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.InputStream JavaDoc;
25
26 /**
27  * An abstract subclass of <code>java.io.InputStream</code> that allows seeking
28  * within the input, similar to the <code>RandomAccessFile</code> class.
29  * Additionally, the <code>DataInput</code> interface is supported and extended
30  * to include support for little-endian representations of fundamental data
31  * types.
32  *
33  * <p> In addition to the familiar methods from <code>InputStream</code>, the
34  * methods <code>getFilePointer()</code>, <code>seek()</code>, are defined as in
35  * the <code>RandomAccessFile</code> class. The <code>canSeekBackwards()</code>
36  * method will return <code>true</code> if it is permissible to seek to a
37  * position earlier in the stream than the current value of
38  * <code>getFilePointer()</code>. Some subclasses of
39  * <code>SeekableStream</code> guarantee the ability to seek backwards while
40  * others may not offer this feature in the interest of providing greater
41  * efficiency for those users who do not require it.
42  *
43  * <p> The <code>DataInput</code> interface is supported as well. This included
44  * the <code>skipBytes()</code> and <code>readFully()</code> methods and a
45  * variety of <code>read</code> methods for various data types.
46  *
47  * <p> A number of concrete subclasses of <code>SeekableStream</code> are
48  * supplied in the <code>com.sun.media.jai.codec</code> package.
49  *
50  * <p> Three classes are provided for the purpose of adapting a standard
51  * <code>InputStream</code> to the <code>SeekableStream</code> interface.
52  * <code>ForwardSeekableStream</code> does not allows seeking backwards, but is
53  * inexpensive to use. <code>FileCacheSeekableStream</code> maintains a copy of
54  * all of the data read from the input in a temporary file; this file will be
55  * discarded automatically when the <code>FileSeekableStream</code> is
56  * finalized, or when the JVM exits normally.
57  * <code>FileCacheSeekableStream</code> is intended to be reasonably efficient
58  * apart from the unavoidable use of disk space. In circumstances where the
59  * creation of a temporary file is not possible,
60  * <code>MemoryCacheSeekableStream</code> may be used.
61  * <code>MemoryCacheSeekableStream</code> creates a potentially large in-memory
62  * buffer to store the stream data and so should be avoided when possible.
63  *
64  * <p> The <code>FileSeekableStream</code> class wraps a <code>File</code> or
65  * <code>RandomAccessFile</code>. It forwards requests to the real underlying
66  * file. It performs a limited amount of caching in order to avoid excessive
67  * I/O costs.
68  *
69  * <p> The <code>SegmentedSeekableStream</code> class performs a different sort
70  * of function. It creates a <code>SeekableStream</code> from another
71  * <code>SeekableStream</code> by selecting a series of portions or "segments".
72  * Each segment starts at a specified location within the source
73  * <code>SeekableStream</code> and extends for a specified number of bytes. The
74  * <code>StreamSegmentMapper</code> interface and <code>StreamSegment</code>
75  * class may be used to compute the segment positions dynamically.
76  *
77  * <p> A convenience methods, <code>wrapInputStream</code> is provided to
78  * construct a suitable <code>SeekableStream</code> instance whose data is
79  * supplied by a given <code>InputStream</code>. The caller, by means of the
80  * <code>canSeekBackwards</code> parameter, determines whether support for
81  * seeking backwards is required.
82  *
83  */

84 public abstract class SeekableStream extends InputStream JavaDoc implements DataInput JavaDoc {
85
86     /**
87      * Returns a <code>SeekableStream</code> that will read from a
88      * given <code>InputStream</code>, optionally including support
89      * for seeking backwards. This is a convenience method that
90      * avoids the need to instantiate specific subclasses of
91      * <code>SeekableStream</code> depending on the current security
92      * model.
93      *
94      * @param is An <code>InputStream</code>.
95      * @param canSeekBackwards <code>true</code> if the ability to seek
96      * backwards in the output is required.
97      * @return An instance of <code>SeekableStream</code>.
98      */

99     public static SeekableStream wrapInputStream(InputStream JavaDoc is,
100                                                  boolean canSeekBackwards) {
101         SeekableStream stream = null;
102
103         if (canSeekBackwards) {
104             try {
105                 stream = new FileCacheSeekableStream(is);
106             } catch (Exception JavaDoc e) {
107                 stream = new MemoryCacheSeekableStream(is);
108             }
109         } else {
110             stream = new ForwardSeekableStream(is);
111         }
112         return stream;
113     }
114
115     // Methods from InputStream
116

117     /**
118      * Reads the next byte of data from the input stream. The value byte is
119      * returned as an <code>int</code> in the range <code>0</code> to
120      * <code>255</code>. If no byte is available because the end of the stream
121      * has been reached, the value <code>-1</code> is returned. This method
122      * blocks until input data is available, the end of the stream is detected,
123      * or an exception is thrown.
124      *
125      * <p> A subclass must provide an implementation of this method.
126      *
127      * @return the next byte of data, or <code>-1</code> if the end of the
128      * stream is reached.
129      * @exception IOException if an I/O error occurs.
130      */

131     public abstract int read() throws IOException JavaDoc;
132
133     /**
134      * Reads up to <code>len</code> bytes of data from the input stream into
135      * an array of bytes. An attempt is made to read as many as
136      * <code>len</code> bytes, but a smaller number may be read, possibly
137      * zero. The number of bytes actually read is returned as an integer.
138      *
139      * <p> This method blocks until input data is available, end of stream is
140      * detected, or an exception is thrown.
141      *
142      * <p> If <code>b</code> is <code>null</code>, a
143      * <code>NullPointerException</code> is thrown.
144      *
145      * <p> If <code>off</code> is negative, or <code>len</code> is negative, or
146      * <code>off+len</code> is greater than the length of the array
147      * <code>b</code>, then an <code>IndexOutOfBoundsException</code> is
148      * thrown.
149      *
150      * <p> If <code>len</code> is zero, then no bytes are read and
151      * <code>0</code> is returned; otherwise, there is an attempt to read at
152      * least one byte. If no byte is available because the stream is at end of
153      * stream, the value <code>-1</code> is returned; otherwise, at least one
154      * byte is read and stored into <code>b</code>.
155      *
156      * <p> The first byte read is stored into element <code>b[off]</code>, the
157      * next one into <code>b[off+1]</code>, and so on. The number of bytes read
158      * is, at most, equal to <code>len</code>. Let <i>k</i> be the number of
159      * bytes actually read; these bytes will be stored in elements
160      * <code>b[off]</code> through <code>b[off+</code><i>k</i><code>-1]</code>,
161      * leaving elements <code>b[off+</code><i>k</i><code>]</code> through
162      * <code>b[off+len-1]</code> unaffected.
163      *
164      * <p> In every case, elements <code>b[0]</code> through
165      * <code>b[off]</code> and elements <code>b[off+len]</code> through
166      * <code>b[b.length-1]</code> are unaffected.
167      *
168      * <p> If the first byte cannot be read for any reason other than end of
169      * stream, then an <code>IOException</code> is thrown. In particular, an
170      * <code>IOException</code> is thrown if the input stream has been closed.
171      *
172      * <p> A subclass must provide an implementation of this method.
173      *
174      * @param b the buffer into which the data is read.
175      * @param off the start offset in array <code>b</code>
176      * at which the data is written.
177      * @param len the maximum number of bytes to read.
178      * @return the total number of bytes read into the buffer, or
179      * <code>-1</code> if there is no more data because the end of
180      * the stream has been reached.
181      * @exception IOException if an I/O error occurs.
182      */

183     public abstract int read(byte[] b, int off, int len) throws IOException JavaDoc;
184     
185     // Implemented in InputStream:
186
//
187
// public int read(byte[] b) throws IOException {
188
// public long skip(long n) throws IOException
189
// public int available) throws IOException
190
// public void close() throws IOException;
191

192     /** Marked position */
193     protected long markPos = -1L;
194
195     /**
196      * Marks the current file position for later return using
197      * the <code>reset()</code> method.
198      */

199     public synchronized void mark(int readLimit) {
200         try {
201             markPos = getFilePointer();
202         } catch (IOException JavaDoc e) {
203             markPos = -1L;
204         }
205     }
206
207     /**
208      * Returns the file position to its position at the time of
209      * the immediately previous call to the <code>mark()</code>
210      * method.
211      */

212     public synchronized void reset() throws IOException JavaDoc {
213         if (markPos != -1) {
214             seek(markPos);
215         }
216     }
217
218     /**
219      * Returns <code>true</code> if marking is supported.
220      * Marking is automatically supported for <code>SeekableStream</code>
221      * subclasses that support seeking backeards. Subclasses that do
222      * not support seeking backwards but do support marking must override
223      * this method.
224      */

225     public boolean markSupported() {
226         return canSeekBackwards();
227     }
228
229     /**
230      * Returns <code>true</code> if this object supports calls to
231      * <code>seek(pos)</code> with an offset <code>pos</code> smaller
232      * than the current offset, as returned by <code>getFilePointer</code>.
233      */

234     public boolean canSeekBackwards() {
235         return false;
236     }
237
238     /**
239      * Returns the current offset in this stream.
240      *
241      * @return the offset from the beginning of the stream, in bytes,
242      * at which the next read occurs.
243      * @exception IOException if an I/O error occurs.
244      */

245     public abstract long getFilePointer() throws IOException JavaDoc;
246
247     /**
248      * Sets the offset, measured from the beginning of this
249      * stream, at which the next read occurs.
250      *
251      * <p> If <code>canSeekBackwards()</code> returns <code>false</code>,
252      * then setting <code>pos</code> to an offset smaller than
253      * the current value of <code>getFilePointer()</code> will have
254      * no effect.
255      *
256      * @param pos the offset position, measured in bytes from the
257      * beginning of the stream, at which to set the stream
258      * pointer.
259      * @exception IOException if <code>pos</code> is less than
260      * <code>0</code> or if an I/O error occurs.
261      */

262     public abstract void seek(long pos) throws IOException JavaDoc;
263
264     // Methods from RandomAccessFile
265

266     /**
267      * Reads <code>b.length</code> bytes from this stream into the byte
268      * array, starting at the current stream pointer. This method reads
269      * repeatedly from the stream until the requested number of bytes are
270      * read. This method blocks until the requested number of bytes are
271      * read, the end of the stream is detected, or an exception is thrown.
272      *
273      * @param b the buffer into which the data is read.
274      * @exception EOFException if this stream reaches the end before reading
275      * all the bytes.
276      * @exception IOException if an I/O error occurs.
277      */

278     public final void readFully(byte[] b) throws IOException JavaDoc {
279         readFully(b, 0, b.length);
280     }
281
282     /**
283      * Reads exactly <code>len</code> bytes from this stream into the byte
284      * array, starting at the current stream pointer. This method reads
285      * repeatedly from the stream until the requested number of bytes are
286      * read. This method blocks until the requested number of bytes are
287      * read, the end of the stream is detected, or an exception is thrown.
288      *
289      * @param b the buffer into which the data is read.
290      * @param off the start offset of the data.
291      * @param len the number of bytes to read.
292      * @exception EOFException if this stream reaches the end before reading
293      * all the bytes.
294      * @exception IOException if an I/O error occurs.
295      */

296     public final void readFully(byte[] b, int off, int len)
297         throws IOException JavaDoc {
298         int n = 0;
299     do {
300         int count = this.read(b, off + n, len - n);
301         if (count < 0)
302         throw new EOFException JavaDoc();
303         n += count;
304     } while (n < len);
305     }
306
307     // Methods from DataInput, plus little-endian versions
308

309     /**
310      * Attempts to skip over <code>n</code> bytes of input discarding the
311      * skipped bytes.
312      * <p>
313      *
314      * This method may skip over some smaller number of bytes, possibly zero.
315      * This may result from any of a number of conditions; reaching end of
316      * stream before <code>n</code> bytes have been skipped is only one
317      * possibility. This method never throws an <code>EOFException</code>.
318      * The actual number of bytes skipped is returned. If <code>n</code>
319      * is negative, no bytes are skipped.
320      *
321      * @param n the number of bytes to be skipped.
322      * @return the actual number of bytes skipped.
323      * @exception IOException if an I/O error occurs.
324      */

325     public int skipBytes(int n) throws IOException JavaDoc {
326         if (n <= 0) {
327             return 0;
328         }
329     return (int)skip(n);
330     }
331
332     /**
333      * Reads a <code>boolean</code> from this stream. This method reads a
334      * single byte from the stream, starting at the current stream pointer.
335      * A value of <code>0</code> represents
336      * <code>false</code>. Any other value represents <code>true</code>.
337      * This method blocks until the byte is read, the end of the stream
338      * is detected, or an exception is thrown.
339      *
340      * @return the <code>boolean</code> value read.
341      * @exception EOFException if this stream has reached the end.
342      * @exception IOException if an I/O error occurs.
343      */

344     public final boolean readBoolean() throws IOException JavaDoc {
345     int ch = this.read();
346     if (ch < 0)
347         throw new EOFException JavaDoc();
348     return (ch != 0);
349     }
350
351     /**
352      * Reads a signed eight-bit value from this stream. This method reads a
353      * byte from the stream, starting from the current stream pointer.
354      * If the byte read is <code>b</code>, where
355      * <code>0&nbsp;&lt;=&nbsp;b&nbsp;&lt;=&nbsp;255</code>,
356      * then the result is:
357      * <blockquote><pre>
358      * (byte)(b)
359      * </pre></blockquote>
360      * <p>
361      * This method blocks until the byte is read, the end of the stream
362      * is detected, or an exception is thrown.
363      *
364      * @return the next byte of this stream as a signed eight-bit
365      * <code>byte</code>.
366      * @exception EOFException if this stream has reached the end.
367      * @exception IOException if an I/O error occurs.
368      */

369     public final byte readByte() throws IOException JavaDoc {
370     int ch = this.read();
371     if (ch < 0)
372         throw new EOFException JavaDoc();
373     return (byte)(ch);
374     }
375
376     /**
377      * Reads an unsigned eight-bit number from this stream. This method reads
378      * a byte from this stream, starting at the current stream pointer,
379      * and returns that byte.
380      * <p>
381      * This method blocks until the byte is read, the end of the stream
382      * is detected, or an exception is thrown.
383      *
384      * @return the next byte of this stream, interpreted as an unsigned
385      * eight-bit number.
386      * @exception EOFException if this stream has reached the end.
387      * @exception IOException if an I/O error occurs.
388      */

389     public final int readUnsignedByte() throws IOException JavaDoc {
390     int ch = this.read();
391     if (ch < 0)
392         throw new EOFException JavaDoc();
393     return ch;
394     }
395
396     /**
397      * Reads a signed 16-bit number from this stream.
398      * The method reads two
399      * bytes from this stream, starting at the current stream pointer.
400      * If the two bytes read, in order, are
401      * <code>b1</code> and <code>b2</code>, where each of the two values is
402      * between <code>0</code> and <code>255</code>, inclusive, then the
403      * result is equal to:
404      * <blockquote><pre>
405      * (short)((b1 &lt;&lt; 8) | b2)
406      * </pre></blockquote>
407      * <p>
408      * This method blocks until the two bytes are read, the end of the
409      * stream is detected, or an exception is thrown.
410      *
411      * @return the next two bytes of this stream, interpreted as a signed
412      * 16-bit number.
413      * @exception EOFException if this stream reaches the end before reading
414      * two bytes.
415      * @exception IOException if an I/O error occurs.
416      */

417     public final short readShort() throws IOException JavaDoc {
418     int ch1 = this.read();
419     int ch2 = this.read();
420     if ((ch1 | ch2) < 0)
421         throw new EOFException JavaDoc();
422     return (short)((ch1 << 8) + (ch2 << 0));
423     }
424
425     /**
426      * Reads a signed 16-bit number from this stream in little-endian order.
427      * The method reads two
428      * bytes from this stream, starting at the current stream pointer.
429      * If the two bytes read, in order, are
430      * <code>b1</code> and <code>b2</code>, where each of the two values is
431      * between <code>0</code> and <code>255</code>, inclusive, then the
432      * result is equal to:
433      * <blockquote><pre>
434      * (short)((b2 &lt;&lt; 8) | b1)
435      * </pre></blockquote>
436      * <p>
437      * This method blocks until the two bytes are read, the end of the
438      * stream is detected, or an exception is thrown.
439      *
440      * @return the next two bytes of this stream, interpreted as a signed
441      * 16-bit number.
442      * @exception EOFException if this stream reaches the end before reading
443      * two bytes.
444      * @exception IOException if an I/O error occurs.
445      */

446     public final short readShortLE() throws IOException JavaDoc {
447     int ch1 = this.read();
448     int ch2 = this.read();
449     if ((ch1 | ch2) < 0)
450         throw new EOFException JavaDoc();
451     return (short)((ch2 << 8) + (ch1 << 0));
452     }
453
454     /**
455      * Reads an unsigned 16-bit number from this stream. This method reads
456      * two bytes from the stream, starting at the current stream pointer.
457      * If the bytes read, in order, are
458      * <code>b1</code> and <code>b2</code>, where
459      * <code>0&nbsp;&lt;=&nbsp;b1, b2&nbsp;&lt;=&nbsp;255</code>,
460      * then the result is equal to:
461      * <blockquote><pre>
462      * (b1 &lt;&lt; 8) | b2
463      * </pre></blockquote>
464      * <p>
465      * This method blocks until the two bytes are read, the end of the
466      * stream is detected, or an exception is thrown.
467      *
468      * @return the next two bytes of this stream, interpreted as an
469      * unsigned 16-bit integer.
470      * @exception EOFException if this stream reaches the end before reading
471      * two bytes.
472      * @exception IOException if an I/O error occurs.
473      */

474     public final int readUnsignedShort() throws IOException JavaDoc {
475     int ch1 = this.read();
476     int ch2 = this.read();
477     if ((ch1 | ch2) < 0)
478         throw new EOFException JavaDoc();
479     return (ch1 << 8) + (ch2 << 0);
480     }
481
482     /**
483      * Reads an unsigned 16-bit number from this stream in little-endian order.
484      * This method reads
485      * two bytes from the stream, starting at the current stream pointer.
486      * If the bytes read, in order, are
487      * <code>b1</code> and <code>b2</code>, where
488      * <code>0&nbsp;&lt;=&nbsp;b1, b2&nbsp;&lt;=&nbsp;255</code>,
489      * then the result is equal to:
490      * <blockquote><pre>
491      * (b2 &lt;&lt; 8) | b1
492      * </pre></blockquote>
493      * <p>
494      * This method blocks until the two bytes are read, the end of the
495      * stream is detected, or an exception is thrown.
496      *
497      * @return the next two bytes of this stream, interpreted as an
498      * unsigned 16-bit integer.
499      * @exception EOFException if this stream reaches the end before reading
500      * two bytes.
501      * @exception IOException if an I/O error occurs.
502      */

503     public final int readUnsignedShortLE() throws IOException JavaDoc {
504     int ch1 = this.read();
505     int ch2 = this.read();
506     if ((ch1 | ch2) < 0)
507         throw new EOFException JavaDoc();
508     return (ch2 << 8) + (ch1 << 0);
509     }
510
511     /**
512      * Reads a Unicode character from this stream. This method reads two
513      * bytes from the stream, starting at the current stream pointer.
514      * If the bytes read, in order, are
515      * <code>b1</code> and <code>b2</code>, where
516      * <code>0&nbsp;&lt;=&nbsp;b1,&nbsp;b2&nbsp;&lt;=&nbsp;255</code>,
517      * then the result is equal to:
518      * <blockquote><pre>
519      * (char)((b1 &lt;&lt; 8) | b2)
520      * </pre></blockquote>
521      * <p>
522      * This method blocks until the two bytes are read, the end of the
523      * stream is detected, or an exception is thrown.
524      *
525      * @return the next two bytes of this stream as a Unicode character.
526      * @exception EOFException if this stream reaches the end before reading
527      * two bytes.
528      * @exception IOException if an I/O error occurs.
529      */

530     public final char readChar() throws IOException JavaDoc {
531     int ch1 = this.read();
532     int ch2 = this.read();
533     if ((ch1 | ch2) < 0)
534         throw new EOFException JavaDoc();
535     return (char)((ch1 << 8) + (ch2 << 0));
536     }
537
538     /**
539      * Reads a Unicode character from this stream in little-endian order.
540      * This method reads two
541      * bytes from the stream, starting at the current stream pointer.
542      * If the bytes read, in order, are
543      * <code>b1</code> and <code>b2</code>, where
544      * <code>0&nbsp;&lt;=&nbsp;b1,&nbsp;b2&nbsp;&lt;=&nbsp;255</code>,
545      * then the result is equal to:
546      * <blockquote><pre>
547      * (char)((b2 &lt;&lt; 8) | b1)
548      * </pre></blockquote>
549      * <p>
550      * This method blocks until the two bytes are read, the end of the
551      * stream is detected, or an exception is thrown.
552      *
553      * @return the next two bytes of this stream as a Unicode character.
554      * @exception EOFException if this stream reaches the end before reading
555      * two bytes.
556      * @exception IOException if an I/O error occurs.
557      */

558     public final char readCharLE() throws IOException JavaDoc {
559     int ch1 = this.read();
560     int ch2 = this.read();
561     if ((ch1 | ch2) < 0)
562         throw new EOFException JavaDoc();
563     return (char)((ch2 << 8) + (ch1 << 0));
564     }
565
566     /**
567      * Reads a signed 32-bit integer from this stream. This method reads 4
568      * bytes from the stream, starting at the current stream pointer.
569      * If the bytes read, in order, are <code>b1</code>,
570      * <code>b2</code>, <code>b3</code>, and <code>b4</code>, where
571      * <code>0&nbsp;&lt;=&nbsp;b1, b2, b3, b4&nbsp;&lt;=&nbsp;255</code>,
572      * then the result is equal to:
573      * <blockquote><pre>
574      * (b1 &lt;&lt; 24) | (b2 &lt;&lt; 16) + (b3 &lt;&lt; 8) + b4
575      * </pre></blockquote>
576      * <p>
577      * This method blocks until the four bytes are read, the end of the
578      * stream is detected, or an exception is thrown.
579      *
580      * @return the next four bytes of this stream, interpreted as an
581      * <code>int</code>.
582      * @exception EOFException if this stream reaches the end before reading
583      * four bytes.
584      * @exception IOException if an I/O error occurs.
585      */

586     public final int readInt() throws IOException JavaDoc {
587     int ch1 = this.read();
588     int ch2 = this.read();
589     int ch3 = this.read();
590     int ch4 = this.read();
591     if ((ch1 | ch2 | ch3 | ch4) < 0)
592         throw new EOFException JavaDoc();
593     return ((ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0));
594     }
595
596     /**
597      * Reads a signed 32-bit integer from this stream in little-endian order.
598      * This method reads 4
599      * bytes from the stream, starting at the current stream pointer.
600      * If the bytes read, in order, are <code>b1</code>,
601      * <code>b2</code>, <code>b3</code>, and <code>b4</code>, where
602      * <code>0&nbsp;&lt;=&nbsp;b1, b2, b3, b4&nbsp;&lt;=&nbsp;255</code>,
603      * then the result is equal to:
604      * <blockquote><pre>
605      * (b4 &lt;&lt; 24) | (b3 &lt;&lt; 16) + (b2 &lt;&lt; 8) + b1
606      * </pre></blockquote>
607      * <p>
608      * This method blocks until the four bytes are read, the end of the
609      * stream is detected, or an exception is thrown.
610      *
611      * @return the next four bytes of this stream, interpreted as an
612      * <code>int</code>.
613      * @exception EOFException if this stream reaches the end before reading
614      * four bytes.
615      * @exception IOException if an I/O error occurs.
616      */

617     public final int readIntLE() throws IOException JavaDoc {
618     int ch1 = this.read();
619     int ch2 = this.read();
620     int ch3 = this.read();
621     int ch4 = this.read();
622     if ((ch1 | ch2 | ch3 | ch4) < 0)
623         throw new EOFException JavaDoc();
624     return ((ch4 << 24) + (ch3 << 16) + (ch2 << 8) + (ch1 << 0));
625     }
626
627     /**
628      * Reads an unsigned 32-bit integer from this stream. This method reads 4
629      * bytes from the stream, starting at the current stream pointer.
630      * If the bytes read, in order, are <code>b1</code>,
631      * <code>b2</code>, <code>b3</code>, and <code>b4</code>, where
632      * <code>0&nbsp;&lt;=&nbsp;b1, b2, b3, b4&nbsp;&lt;=&nbsp;255</code>,
633      * then the result is equal to:
634      * <blockquote><pre>
635      * (b1 &lt;&lt; 24) | (b2 &lt;&lt; 16) + (b3 &lt;&lt; 8) + b4
636      * </pre></blockquote>
637      * <p>
638      * This method blocks until the four bytes are read, the end of the
639      * stream is detected, or an exception is thrown.
640      *
641      * @return the next four bytes of this stream, interpreted as a
642      * <code>long</code>.
643      * @exception EOFException if this stream reaches the end before reading
644      * four bytes.
645      * @exception IOException if an I/O error occurs.
646      */

647     public final long readUnsignedInt() throws IOException JavaDoc {
648     long ch1 = this.read();
649     long ch2 = this.read();
650     long ch3 = this.read();
651     long ch4 = this.read();
652     if ((ch1 | ch2 | ch3 | ch4) < 0)
653         throw new EOFException JavaDoc();
654     return ((ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0));
655     }
656
657     private byte[] ruileBuf = new byte[4];
658
659     /**
660      * Reads an unsigned 32-bit integer from this stream in little-endian
661      * order. This method reads 4
662      * bytes from the stream, starting at the current stream pointer.
663      * If the bytes read, in order, are <code>b1</code>,
664      * <code>b2</code>, <code>b3</code>, and <code>b4</code>, where
665      * <code>0&nbsp;&lt;=&nbsp;b1, b2, b3, b4&nbsp;&lt;=&nbsp;255</code>,
666      * then the result is equal to:
667      * <blockquote><pre>
668      * (b4 &lt;&lt; 24) | (b3 &lt;&lt; 16) + (b2 &lt;&lt; 8) + b1
669      * </pre></blockquote>
670      * <p>
671      * This method blocks until the four bytes are read, the end of the
672      * stream is detected, or an exception is thrown.
673      *
674      * @return the next four bytes of this stream, interpreted as a
675      * <code>long</code>.
676      * @exception EOFException if this stream reaches the end before reading
677      * four bytes.
678      * @exception IOException if an I/O error occurs.
679      */

680     public final long readUnsignedIntLE() throws IOException JavaDoc {
681         this.readFully(ruileBuf);
682     long ch1 = (ruileBuf[0] & 0xff);
683     long ch2 = (ruileBuf[1] & 0xff);
684     long ch3 = (ruileBuf[2] & 0xff);
685     long ch4 = (ruileBuf[3] & 0xff);
686
687     return ((ch4 << 24) + (ch3 << 16) + (ch2 << 8) + (ch1 << 0));
688     }
689
690     /**
691      * Reads a signed 64-bit integer from this stream. This method reads eight
692      * bytes from the stream, starting at the current stream pointer.
693      * If the bytes read, in order, are
694      * <code>b1</code>, <code>b2</code>, <code>b3</code>,
695      * <code>b4</code>, <code>b5</code>, <code>b6</code>,
696      * <code>b7</code>, and <code>b8,</code> where:
697      * <blockquote><pre>
698      * 0 &lt;= b1, b2, b3, b4, b5, b6, b7, b8 &lt;=255,
699      * </pre></blockquote>
700      * <p>
701      * then the result is equal to:
702      * <p><blockquote><pre>
703      * ((long)b1 &lt;&lt; 56) + ((long)b2 &lt;&lt; 48)
704      * + ((long)b3 &lt;&lt; 40) + ((long)b4 &lt;&lt; 32)
705      * + ((long)b5 &lt;&lt; 24) + ((long)b6 &lt;&lt; 16)
706      * + ((long)b7 &lt;&lt; 8) + b8
707      * </pre></blockquote>
708      * <p>
709      * This method blocks until the eight bytes are read, the end of the
710      * stream is detected, or an exception is thrown.
711      *
712      * @return the next eight bytes of this stream, interpreted as a
713      * <code>long</code>.
714      * @exception EOFException if this stream reaches the end before reading
715      * eight bytes.
716      * @exception IOException if an I/O error occurs.
717      */

718     public final long readLong() throws IOException JavaDoc {
719     return ((long)(readInt()) << 32) + (readInt() & 0xFFFFFFFFL);
720     }
721
722     /**
723      * Reads a signed 64-bit integer from this stream in little-endian
724      * order. This method reads eight
725      * bytes from the stream, starting at the current stream pointer.
726      * If the bytes read, in order, are
727      * <code>b1</code>, <code>b2</code>, <code>b3</code>,
728      * <code>b4</code>, <code>b5</code>, <code>b6</code>,
729      * <code>b7</code>, and <code>b8,</code> where:
730      * <blockquote><pre>
731      * 0 &lt;= b1, b2, b3, b4, b5, b6, b7, b8 &lt;=255,
732      * </pre></blockquote>
733      * <p>
734      * then the result is equal to:
735      * <p><blockquote><pre>
736      * ((long)b1 &lt;&lt; 56) + ((long)b2 &lt;&lt; 48)
737      * + ((long)b3 &lt;&lt; 40) + ((long)b4 &lt;&lt; 32)
738      * + ((long)b5 &lt;&lt; 24) + ((long)b6 &lt;&lt; 16)
739      * + ((long)b7 &lt;&lt; 8) + b8
740      * </pre></blockquote>
741      * <p>
742      * This method blocks until the eight bytes are read, the end of the
743      * stream is detected, or an exception is thrown.
744      *
745      * @return the next eight bytes of this stream, interpreted as a
746      * <code>long</code>.
747      * @exception EOFException if this stream reaches the end before reading
748      * eight bytes.
749      * @exception IOException if an I/O error occurs.
750      */

751     public final long readLongLE() throws IOException JavaDoc {
752         int i1 = readIntLE();
753         int i2 = readIntLE();
754     return ((long)i2 << 32) + (i1 & 0xFFFFFFFFL);
755     }
756
757     /**
758      * Reads a <code>float</code> from this stream. This method reads an
759      * <code>int</code> value, starting at the current stream pointer,
760      * as if by the <code>readInt</code> method
761      * and then converts that <code>int</code> to a <code>float</code>
762      * using the <code>intBitsToFloat</code> method in class
763      * <code>Float</code>.
764      * <p>
765      * This method blocks until the four bytes are read, the end of the
766      * stream is detected, or an exception is thrown.
767      *
768      * @return the next four bytes of this stream, interpreted as a
769      * <code>float</code>.
770      * @exception EOFException if this stream reaches the end before reading
771      * four bytes.
772      * @exception IOException if an I/O error occurs.
773      */

774     public final float readFloat() throws IOException JavaDoc {
775     return Float.intBitsToFloat(readInt());
776     }
777
778     /**
779      * Reads a <code>float</code> from this stream in little-endian order.
780      * This method reads an
781      * <code>int</code> value, starting at the current stream pointer,
782      * as if by the <code>readInt</code> method
783      * and then converts that <code>int</code> to a <code>float</code>
784      * using the <code>intBitsToFloat</code> method in class
785      * <code>Float</code>.
786      * <p>
787      * This method blocks until the four bytes are read, the end of the
788      * stream is detected, or an exception is thrown.
789      *
790      * @return the next four bytes of this stream, interpreted as a
791      * <code>float</code>.
792      * @exception EOFException if this stream reaches the end before reading
793      * four bytes.
794      * @exception IOException if an I/O error occurs.
795      */

796     public final float readFloatLE() throws IOException JavaDoc {
797     return Float.intBitsToFloat(readIntLE());
798     }
799
800     /**
801      * Reads a <code>double</code> from this stream. This method reads a
802      * <code>long</code> value, starting at the current stream pointer,
803      * as if by the <code>readLong</code> method
804      * and then converts that <code>long</code> to a <code>double</code>
805      * using the <code>longBitsToDouble</code> method in
806      * class <code>Double</code>.
807      * <p>
808      * This method blocks until the eight bytes are read, the end of the
809      * stream is detected, or an exception is thrown.
810      *
811      * @return the next eight bytes of this stream, interpreted as a
812      * <code>double</code>.
813      * @exception EOFException if this stream reaches the end before reading
814      * eight bytes.
815      * @exception IOException if an I/O error occurs.
816      */

817     public final double readDouble() throws IOException JavaDoc {
818     return Double.longBitsToDouble(readLong());
819     }
820
821     /**
822      * Reads a <code>double</code> from this stream in little-endian order.
823      * This method reads a
824      * <code>long</code> value, starting at the current stream pointer,
825      * as if by the <code>readLong</code> method
826      * and then converts that <code>long</code> to a <code>double</code>
827      * using the <code>longBitsToDouble</code> method in
828      * class <code>Double</code>.
829      * <p>
830      * This method blocks until the eight bytes are read, the end of the
831      * stream is detected, or an exception is thrown.
832      *
833      * @return the next eight bytes of this stream, interpreted as a
834      * <code>double</code>.
835      * @exception EOFException if this stream reaches the end before reading
836      * eight bytes.
837      * @exception IOException if an I/O error occurs.
838      */

839     public final double readDoubleLE() throws IOException JavaDoc {
840     return Double.longBitsToDouble(readLongLE());
841     }
842
843     /**
844      * Reads the next line of text from this stream. This method successively
845      * reads bytes from the stream, starting at the current stream pointer,
846      * until it reaches a line terminator or the end
847      * of the stream. Each byte is converted into a character by taking the
848      * byte's value for the lower eight bits of the character and setting the
849      * high eight bits of the character to zero. This method does not,
850      * therefore, support the full Unicode character set.
851      *
852      * <p> A line of text is terminated by a carriage-return character
853      * (<code>'&#92;r'</code>), a newline character (<code>'&#92;n'</code>), a
854      * carriage-return character immediately followed by a newline character,
855      * or the end of the stream. Line-terminating characters are discarded and
856      * are not included as part of the string returned.
857      *
858      * <p> This method blocks until a newline character is read, a carriage
859      * return and the byte following it are read (to see if it is a newline),
860      * the end of the stream is reached, or an exception is thrown.
861      *
862      * @return the next line of text from this stream, or null if end
863      * of stream is encountered before even one byte is read.
864      * @exception IOException if an I/O error occurs.
865      */

866     public final String JavaDoc readLine() throws IOException JavaDoc {
867     StringBuffer JavaDoc input = new StringBuffer JavaDoc();
868     int c = -1;
869     boolean eol = false;
870
871     while (!eol) {
872         switch (c = read()) {
873         case -1:
874         case '\n':
875         eol = true;
876         break;
877         case '\r':
878         eol = true;
879         long cur = getFilePointer();
880         if ((read()) != '\n') {
881             seek(cur);
882         }
883         break;
884         default:
885         input.append((char)c);
886         break;
887         }
888     }
889
890     if ((c == -1) && (input.length() == 0)) {
891         return null;
892     }
893     return input.toString();
894     }
895
896     /**
897      * Reads in a string from this stream. The string has been encoded
898      * using a modified UTF-8 format.
899      * <p>
900      * The first two bytes are read, starting from the current stream
901      * pointer, as if by
902      * <code>readUnsignedShort</code>. This value gives the number of
903      * following bytes that are in the encoded string, not
904      * the length of the resulting string. The following bytes are then
905      * interpreted as bytes encoding characters in the UTF-8 format
906      * and are converted into characters.
907      * <p>
908      * This method blocks until all the bytes are read, the end of the
909      * stream is detected, or an exception is thrown.
910      *
911      * @return a Unicode string.
912      * @exception EOFException if this stream reaches the end before
913      * reading all the bytes.
914      * @exception IOException if an I/O error occurs.
915      * @exception UTFDataFormatException if the bytes do not represent
916      * valid UTF-8 encoding of a Unicode string.
917      */

918     public final String JavaDoc readUTF() throws IOException JavaDoc {
919     return DataInputStream.readUTF(this);
920     }
921
922     /**
923      * Releases any system resources associated with this stream
924      * by calling the <code>close()</code> method.
925      */

926     protected void finalize() throws Throwable JavaDoc {
927         super.finalize();
928         close();
929     }
930 }
931
Popular Tags