KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > io > DataInputStream


1 /*
2  * @(#)DataInputStream.java 1.71 04/05/28
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package java.io;
9
10 /**
11  * A data input stream lets an application read primitive Java data
12  * types from an underlying input stream in a machine-independent
13  * way. An application uses a data output stream to write data that
14  * can later be read by a data input stream.
15  *
16  * @author Arthur van Hoff
17  * @version 1.71, 05/28/04
18  * @see java.io.DataOutputStream
19  * @since JDK1.0
20  */

21 public
22 class DataInputStream extends FilterInputStream JavaDoc implements DataInput JavaDoc {
23
24     /**
25      * Creates a DataInputStream that uses the specified
26      * underlying InputStream.
27      *
28      * @param in the specified input stream
29      */

30     public DataInputStream(InputStream JavaDoc in) {
31     super(in);
32     }
33
34     /**
35      * working arrays initialized on demand by readUTF
36      */

37     private byte bytearr[] = new byte[80];
38     private char chararr[] = new char[80];
39
40     /**
41      * Reads some number of bytes from the contained input stream and
42      * stores them into the buffer array <code>b</code>. The number of
43      * bytes actually read is returned as an integer. This method blocks
44      * until input data is available, end of file is detected, or an
45      * exception is thrown.
46      *
47      * <p>If <code>b</code> is null, a <code>NullPointerException</code> is
48      * thrown. If the length of <code>b</code> is zero, then no bytes are
49      * read and <code>0</code> is returned; otherwise, there is an attempt
50      * to read at least one byte. If no byte is available because the
51      * stream is at end of file, the value <code>-1</code> is returned;
52      * otherwise, at least one byte is read and stored into <code>b</code>.
53      *
54      * <p>The first byte read is stored into element <code>b[0]</code>, the
55      * next one into <code>b[1]</code>, and so on. The number of bytes read
56      * is, at most, equal to the length of <code>b</code>. Let <code>k</code>
57      * be the number of bytes actually read; these bytes will be stored in
58      * elements <code>b[0]</code> through <code>b[k-1]</code>, leaving
59      * elements <code>b[k]</code> through <code>b[b.length-1]</code>
60      * unaffected.
61      *
62      * <p>If the first byte cannot be read for any reason other than end of
63      * file, then an <code>IOException</code> is thrown. In particular, an
64      * <code>IOException</code> is thrown if the input stream has been closed.
65      *
66      * <p>The <code>read(b)</code> method has the same effect as:
67      * <blockquote><pre>
68      * read(b, 0, b.length)
69      * </pre></blockquote>
70      *
71      * @param b the buffer into which the data is read.
72      * @return the total number of bytes read into the buffer, or
73      * <code>-1</code> if there is no more data because the end
74      * of the stream has been reached.
75      * @exception IOException if an I/O error occurs.
76      * @see java.io.FilterInputStream#in
77      * @see java.io.InputStream#read(byte[], int, int)
78      */

79     public final int read(byte b[]) throws IOException JavaDoc {
80     return in.read(b, 0, b.length);
81     }
82
83     /**
84      * Reads up to <code>len</code> bytes of data from the contained
85      * input stream into an array of bytes. An attempt is made to read
86      * as many as <code>len</code> bytes, but a smaller number may be read,
87      * possibly zero. The number of bytes actually read is returned as an
88      * integer.
89      *
90      * <p> This method blocks until input data is available, end of file is
91      * detected, or an exception is thrown.
92      *
93      * <p> If <code>b</code> is <code>null</code>, a
94      * <code>NullPointerException</code> is thrown.
95      *
96      * <p> If <code>off</code> is negative, or <code>len</code> is negative, or
97      * <code>off+len</code> is greater than the length of the array
98      * <code>b</code>, then an <code>IndexOutOfBoundsException</code> is
99      * thrown.
100      *
101      * <p> If <code>len</code> is zero, then no bytes are read and
102      * <code>0</code> is returned; otherwise, there is an attempt to read at
103      * least one byte. If no byte is available because the stream is at end of
104      * file, the value <code>-1</code> is returned; otherwise, at least one
105      * byte is read and stored into <code>b</code>.
106      *
107      * <p> The first byte read is stored into element <code>b[off]</code>, the
108      * next one into <code>b[off+1]</code>, and so on. The number of bytes read
109      * is, at most, equal to <code>len</code>. Let <i>k</i> be the number of
110      * bytes actually read; these bytes will be stored in elements
111      * <code>b[off]</code> through <code>b[off+</code><i>k</i><code>-1]</code>,
112      * leaving elements <code>b[off+</code><i>k</i><code>]</code> through
113      * <code>b[off+len-1]</code> unaffected.
114      *
115      * <p> In every case, elements <code>b[0]</code> through
116      * <code>b[off]</code> and elements <code>b[off+len]</code> through
117      * <code>b[b.length-1]</code> are unaffected.
118      *
119      * <p> If the first byte cannot be read for any reason other than end of
120      * file, then an <code>IOException</code> is thrown. In particular, an
121      * <code>IOException</code> is thrown if the input stream has been closed.
122      *
123      * @param b the buffer into which the data is read.
124      * @param off the start offset of the data.
125      * @param len the maximum number of bytes read.
126      * @return the total number of bytes read into the buffer, or
127      * <code>-1</code> if there is no more data because the end
128      * of the stream has been reached.
129      * @exception IOException if an I/O error occurs.
130      * @see java.io.FilterInputStream#in
131      * @see java.io.InputStream#read(byte[], int, int)
132      */

133     public final int read(byte b[], int off, int len) throws IOException JavaDoc {
134     return in.read(b, off, len);
135     }
136
137     /**
138      * See the general contract of the <code>readFully</code>
139      * method of <code>DataInput</code>.
140      * <p>
141      * Bytes
142      * for this operation are read from the contained
143      * input stream.
144      *
145      * @param b the buffer into which the data is read.
146      * @exception EOFException if this input stream reaches the end before
147      * reading all the bytes.
148      * @exception IOException if an I/O error occurs.
149      * @see java.io.FilterInputStream#in
150      */

151     public final void readFully(byte b[]) throws IOException JavaDoc {
152     readFully(b, 0, b.length);
153     }
154
155     /**
156      * See the general contract of the <code>readFully</code>
157      * method of <code>DataInput</code>.
158      * <p>
159      * Bytes
160      * for this operation are read from the contained
161      * input stream.
162      *
163      * @param b the buffer into which the data is read.
164      * @param off the start offset of the data.
165      * @param len the number of bytes to read.
166      * @exception EOFException if this input stream reaches the end before
167      * reading all the bytes.
168      * @exception IOException if an I/O error occurs.
169      * @see java.io.FilterInputStream#in
170      */

171     public final void readFully(byte b[], int off, int len) throws IOException JavaDoc {
172     if (len < 0)
173         throw new IndexOutOfBoundsException JavaDoc();
174     int n = 0;
175     while (n < len) {
176         int count = in.read(b, off + n, len - n);
177         if (count < 0)
178         throw new EOFException JavaDoc();
179         n += count;
180     }
181     }
182
183     /**
184      * See the general contract of the <code>skipBytes</code>
185      * method of <code>DataInput</code>.
186      * <p>
187      * Bytes
188      * for this operation are read from the contained
189      * input stream.
190      *
191      * @param n the number of bytes to be skipped.
192      * @return the actual number of bytes skipped.
193      * @exception IOException if an I/O error occurs.
194      */

195     public final int skipBytes(int n) throws IOException JavaDoc {
196     int total = 0;
197     int cur = 0;
198
199     while ((total<n) && ((cur = (int) in.skip(n-total)) > 0)) {
200         total += cur;
201     }
202
203     return total;
204     }
205
206     /**
207      * See the general contract of the <code>readBoolean</code>
208      * method of <code>DataInput</code>.
209      * <p>
210      * Bytes
211      * for this operation are read from the contained
212      * input stream.
213      *
214      * @return the <code>boolean</code> value read.
215      * @exception EOFException if this input stream has reached the end.
216      * @exception IOException if an I/O error occurs.
217      * @see java.io.FilterInputStream#in
218      */

219     public final boolean readBoolean() throws IOException JavaDoc {
220     int ch = in.read();
221     if (ch < 0)
222         throw new EOFException JavaDoc();
223     return (ch != 0);
224     }
225
226     /**
227      * See the general contract of the <code>readByte</code>
228      * method of <code>DataInput</code>.
229      * <p>
230      * Bytes
231      * for this operation are read from the contained
232      * input stream.
233      *
234      * @return the next byte of this input stream as a signed 8-bit
235      * <code>byte</code>.
236      * @exception EOFException if this input stream has reached the end.
237      * @exception IOException if an I/O error occurs.
238      * @see java.io.FilterInputStream#in
239      */

240     public final byte readByte() throws IOException JavaDoc {
241     int ch = in.read();
242     if (ch < 0)
243         throw new EOFException JavaDoc();
244     return (byte)(ch);
245     }
246
247     /**
248      * See the general contract of the <code>readUnsignedByte</code>
249      * method of <code>DataInput</code>.
250      * <p>
251      * Bytes
252      * for this operation are read from the contained
253      * input stream.
254      *
255      * @return the next byte of this input stream, interpreted as an
256      * unsigned 8-bit number.
257      * @exception EOFException if this input stream has reached the end.
258      * @exception IOException if an I/O error occurs.
259      * @see java.io.FilterInputStream#in
260      */

261     public final int readUnsignedByte() throws IOException JavaDoc {
262     int ch = in.read();
263     if (ch < 0)
264         throw new EOFException JavaDoc();
265     return ch;
266     }
267
268     /**
269      * See the general contract of the <code>readShort</code>
270      * method of <code>DataInput</code>.
271      * <p>
272      * Bytes
273      * for this operation are read from the contained
274      * input stream.
275      *
276      * @return the next two bytes of this input stream, interpreted as a
277      * signed 16-bit number.
278      * @exception EOFException if this input stream reaches the end before
279      * reading two bytes.
280      * @exception IOException if an I/O error occurs.
281      * @see java.io.FilterInputStream#in
282      */

283     public final short readShort() throws IOException JavaDoc {
284         int ch1 = in.read();
285         int ch2 = in.read();
286         if ((ch1 | ch2) < 0)
287             throw new EOFException JavaDoc();
288         return (short)((ch1 << 8) + (ch2 << 0));
289     }
290
291     /**
292      * See the general contract of the <code>readUnsignedShort</code>
293      * method of <code>DataInput</code>.
294      * <p>
295      * Bytes
296      * for this operation are read from the contained
297      * input stream.
298      *
299      * @return the next two bytes of this input stream, interpreted as an
300      * unsigned 16-bit integer.
301      * @exception EOFException if this input stream reaches the end before
302      * reading two bytes.
303      * @exception IOException if an I/O error occurs.
304      * @see java.io.FilterInputStream#in
305      */

306     public final int readUnsignedShort() throws IOException JavaDoc {
307         int ch1 = in.read();
308         int ch2 = in.read();
309         if ((ch1 | ch2) < 0)
310             throw new EOFException JavaDoc();
311         return (ch1 << 8) + (ch2 << 0);
312     }
313
314     /**
315      * See the general contract of the <code>readChar</code>
316      * method of <code>DataInput</code>.
317      * <p>
318      * Bytes
319      * for this operation are read from the contained
320      * input stream.
321      *
322      * @return the next two bytes of this input stream as a Unicode
323      * character.
324      * @exception EOFException if this input stream reaches the end before
325      * reading two bytes.
326      * @exception IOException if an I/O error occurs.
327      * @see java.io.FilterInputStream#in
328      */

329     public final char readChar() throws IOException JavaDoc {
330         int ch1 = in.read();
331         int ch2 = in.read();
332         if ((ch1 | ch2) < 0)
333             throw new EOFException JavaDoc();
334         return (char)((ch1 << 8) + (ch2 << 0));
335     }
336
337     /**
338      * See the general contract of the <code>readInt</code>
339      * method of <code>DataInput</code>.
340      * <p>
341      * Bytes
342      * for this operation are read from the contained
343      * input stream.
344      *
345      * @return the next four bytes of this input stream, interpreted as an
346      * <code>int</code>.
347      * @exception EOFException if this input stream reaches the end before
348      * reading four bytes.
349      * @exception IOException if an I/O error occurs.
350      * @see java.io.FilterInputStream#in
351      */

352     public final int readInt() throws IOException JavaDoc {
353         int ch1 = in.read();
354         int ch2 = in.read();
355         int ch3 = in.read();
356         int ch4 = in.read();
357         if ((ch1 | ch2 | ch3 | ch4) < 0)
358             throw new EOFException JavaDoc();
359         return ((ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0));
360     }
361
362     private byte readBuffer[] = new byte[8];
363
364     /**
365      * See the general contract of the <code>readLong</code>
366      * method of <code>DataInput</code>.
367      * <p>
368      * Bytes
369      * for this operation are read from the contained
370      * input stream.
371      *
372      * @return the next eight bytes of this input stream, interpreted as a
373      * <code>long</code>.
374      * @exception EOFException if this input stream reaches the end before
375      * reading eight bytes.
376      * @exception IOException if an I/O error occurs.
377      * @see java.io.FilterInputStream#in
378      */

379     public final long readLong() throws IOException JavaDoc {
380         readFully(readBuffer, 0, 8);
381         return (((long)readBuffer[0] << 56) +
382                 ((long)(readBuffer[1] & 255) << 48) +
383                 ((long)(readBuffer[2] & 255) << 40) +
384                 ((long)(readBuffer[3] & 255) << 32) +
385                 ((long)(readBuffer[4] & 255) << 24) +
386                 ((readBuffer[5] & 255) << 16) +
387                 ((readBuffer[6] & 255) << 8) +
388                 ((readBuffer[7] & 255) << 0));
389     }
390
391     /**
392      * See the general contract of the <code>readFloat</code>
393      * method of <code>DataInput</code>.
394      * <p>
395      * Bytes
396      * for this operation are read from the contained
397      * input stream.
398      *
399      * @return the next four bytes of this input stream, interpreted as a
400      * <code>float</code>.
401      * @exception EOFException if this input stream reaches the end before
402      * reading four bytes.
403      * @exception IOException if an I/O error occurs.
404      * @see java.io.DataInputStream#readInt()
405      * @see java.lang.Float#intBitsToFloat(int)
406      */

407     public final float readFloat() throws IOException JavaDoc {
408     return Float.intBitsToFloat(readInt());
409     }
410
411     /**
412      * See the general contract of the <code>readDouble</code>
413      * method of <code>DataInput</code>.
414      * <p>
415      * Bytes
416      * for this operation are read from the contained
417      * input stream.
418      *
419      * @return the next eight bytes of this input stream, interpreted as a
420      * <code>double</code>.
421      * @exception EOFException if this input stream reaches the end before
422      * reading eight bytes.
423      * @exception IOException if an I/O error occurs.
424      * @see java.io.DataInputStream#readLong()
425      * @see java.lang.Double#longBitsToDouble(long)
426      */

427     public final double readDouble() throws IOException JavaDoc {
428     return Double.longBitsToDouble(readLong());
429     }
430
431     private char lineBuffer[];
432
433     /**
434      * See the general contract of the <code>readLine</code>
435      * method of <code>DataInput</code>.
436      * <p>
437      * Bytes
438      * for this operation are read from the contained
439      * input stream.
440      *
441      * @deprecated This method does not properly convert bytes to characters.
442      * As of JDK&nbsp;1.1, the preferred way to read lines of text is via the
443      * <code>BufferedReader.readLine()</code> method. Programs that use the
444      * <code>DataInputStream</code> class to read lines can be converted to use
445      * the <code>BufferedReader</code> class by replacing code of the form:
446      * <blockquote><pre>
447      * DataInputStream d =&nbsp;new&nbsp;DataInputStream(in);
448      * </pre></blockquote>
449      * with:
450      * <blockquote><pre>
451      * BufferedReader d
452      * =&nbsp;new&nbsp;BufferedReader(new&nbsp;InputStreamReader(in));
453      * </pre></blockquote>
454      *
455      * @return the next line of text from this input stream.
456      * @exception IOException if an I/O error occurs.
457      * @see java.io.BufferedReader#readLine()
458      * @see java.io.FilterInputStream#in
459      */

460     @Deprecated JavaDoc
461     public final String JavaDoc readLine() throws IOException JavaDoc {
462     char buf[] = lineBuffer;
463
464     if (buf == null) {
465         buf = lineBuffer = new char[128];
466     }
467
468     int room = buf.length;
469     int offset = 0;
470     int c;
471
472 loop: while (true) {
473         switch (c = in.read()) {
474           case -1:
475           case '\n':
476         break loop;
477
478           case '\r':
479         int c2 = in.read();
480         if ((c2 != '\n') && (c2 != -1)) {
481             if (!(in instanceof PushbackInputStream JavaDoc)) {
482             this.in = new PushbackInputStream JavaDoc(in);
483             }
484             ((PushbackInputStream JavaDoc)in).unread(c2);
485         }
486         break loop;
487
488           default:
489         if (--room < 0) {
490             buf = new char[offset + 128];
491             room = buf.length - offset - 1;
492             System.arraycopy(lineBuffer, 0, buf, 0, offset);
493             lineBuffer = buf;
494         }
495         buf[offset++] = (char) c;
496         break;
497         }
498     }
499     if ((c == -1) && (offset == 0)) {
500         return null;
501     }
502     return String.copyValueOf(buf, 0, offset);
503     }
504
505     /**
506      * See the general contract of the <code>readUTF</code>
507      * method of <code>DataInput</code>.
508      * <p>
509      * Bytes
510      * for this operation are read from the contained
511      * input stream.
512      *
513      * @return a Unicode string.
514      * @exception EOFException if this input stream reaches the end before
515      * reading all the bytes.
516      * @exception IOException if an I/O error occurs.
517      * @exception UTFDataFormatException if the bytes do not represent a valid
518      * modified UTF-8 encoding of a string.
519      * @see java.io.DataInputStream#readUTF(java.io.DataInput)
520      */

521     public final String JavaDoc readUTF() throws IOException JavaDoc {
522         return readUTF(this);
523     }
524
525     /**
526      * Reads from the
527      * stream <code>in</code> a representation
528      * of a Unicode character string encoded in
529      * <a HREF="DataInput.html#modified-utf-8">modified UTF-8</a> format;
530      * this string of characters is then returned as a <code>String</code>.
531      * The details of the modified UTF-8 representation
532      * are exactly the same as for the <code>readUTF</code>
533      * method of <code>DataInput</code>.
534      *
535      * @param in a data input stream.
536      * @return a Unicode string.
537      * @exception EOFException if the input stream reaches the end
538      * before all the bytes.
539      * @exception IOException if an I/O error occurs.
540      * @exception UTFDataFormatException if the bytes do not represent a
541      * valid modified UTF-8 encoding of a Unicode string.
542      * @see java.io.DataInputStream#readUnsignedShort()
543      */

544     public final static String JavaDoc readUTF(DataInput JavaDoc in) throws IOException JavaDoc {
545         int utflen = in.readUnsignedShort();
546         byte[] bytearr = null;
547         char[] chararr = null;
548         if (in instanceof DataInputStream JavaDoc) {
549             DataInputStream JavaDoc dis = (DataInputStream JavaDoc)in;
550             if (dis.bytearr.length < utflen){
551                 dis.bytearr = new byte[utflen*2];
552                 dis.chararr = new char[utflen*2];
553             }
554             chararr = dis.chararr;
555             bytearr = dis.bytearr;
556         } else {
557             bytearr = new byte[utflen];
558             chararr = new char[utflen];
559         }
560
561         int c, char2, char3;
562         int count = 0;
563         int chararr_count=0;
564
565         in.readFully(bytearr, 0, utflen);
566
567         while (count < utflen) {
568             c = (int) bytearr[count] & 0xff;
569             if (c > 127) break;
570             count++;
571             chararr[chararr_count++]=(char)c;
572         }
573
574         while (count < utflen) {
575             c = (int) bytearr[count] & 0xff;
576             switch (c >> 4) {
577                 case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
578                     /* 0xxxxxxx*/
579                     count++;
580                     chararr[chararr_count++]=(char)c;
581                     break;
582                 case 12: case 13:
583                     /* 110x xxxx 10xx xxxx*/
584                     count += 2;
585                     if (count > utflen)
586                         throw new UTFDataFormatException JavaDoc(
587                             "malformed input: partial character at end");
588                     char2 = (int) bytearr[count-1];
589                     if ((char2 & 0xC0) != 0x80)
590                         throw new UTFDataFormatException JavaDoc(
591                             "malformed input around byte " + count);
592                     chararr[chararr_count++]=(char)(((c & 0x1F) << 6) |
593                                                     (char2 & 0x3F));
594                     break;
595                 case 14:
596                     /* 1110 xxxx 10xx xxxx 10xx xxxx */
597                     count += 3;
598                     if (count > utflen)
599                         throw new UTFDataFormatException JavaDoc(
600                             "malformed input: partial character at end");
601                     char2 = (int) bytearr[count-2];
602                     char3 = (int) bytearr[count-1];
603                     if (((char2 & 0xC0) != 0x80) || ((char3 & 0xC0) != 0x80))
604                         throw new UTFDataFormatException JavaDoc(
605                             "malformed input around byte " + (count-1));
606                     chararr[chararr_count++]=(char)(((c & 0x0F) << 12) |
607                                                     ((char2 & 0x3F) << 6) |
608                                                     ((char3 & 0x3F) << 0));
609                     break;
610                 default:
611                     /* 10xx xxxx, 1111 xxxx */
612                     throw new UTFDataFormatException JavaDoc(
613                         "malformed input around byte " + count);
614             }
615         }
616         // The number of chars produced may be less than utflen
617
return new String JavaDoc(chararr, 0, chararr_count);
618     }
619 }
620
Popular Tags