< prev index next >

src/java.base/share/classes/java/io/DataInputStream.java

Print this page




  38  * @author  Arthur van Hoff
  39  * @see     java.io.DataOutputStream
  40  * @since   1.0
  41  */
  42 public
  43 class DataInputStream extends FilterInputStream implements DataInput {
  44 
  45     /**
  46      * Creates a DataInputStream that uses the specified
  47      * underlying InputStream.
  48      *
  49      * @param  in   the specified input stream
  50      */
  51     public DataInputStream(InputStream in) {
  52         super(in);
  53     }
  54 
  55     /**
  56      * working arrays initialized on demand by readUTF
  57      */
  58     private byte bytearr[] = new byte[80];
  59     private char chararr[] = new char[80];
  60 
  61     /**
  62      * Reads some number of bytes from the contained input stream and
  63      * stores them into the buffer array <code>b</code>. The number of
  64      * bytes actually read is returned as an integer. This method blocks
  65      * until input data is available, end of file is detected, or an
  66      * exception is thrown.
  67      *
  68      * <p>If <code>b</code> is null, a <code>NullPointerException</code> is
  69      * thrown. If the length of <code>b</code> is zero, then no bytes are
  70      * read and <code>0</code> is returned; otherwise, there is an attempt
  71      * to read at least one byte. If no byte is available because the
  72      * stream is at end of file, the value <code>-1</code> is returned;
  73      * otherwise, at least one byte is read and stored into <code>b</code>.
  74      *
  75      * <p>The first byte read is stored into element <code>b[0]</code>, the
  76      * next one into <code>b[1]</code>, and so on. The number of bytes read
  77      * is, at most, equal to the length of <code>b</code>. Let <code>k</code>
  78      * be the number of bytes actually read; these bytes will be stored in
  79      * elements <code>b[0]</code> through <code>b[k-1]</code>, leaving
  80      * elements <code>b[k]</code> through <code>b[b.length-1]</code>
  81      * unaffected.
  82      *
  83      * <p>The <code>read(b)</code> method has the same effect as:
  84      * <blockquote><pre>
  85      * read(b, 0, b.length)
  86      * </pre></blockquote>
  87      *
  88      * @param      b   the buffer into which the data is read.
  89      * @return     the total number of bytes read into the buffer, or
  90      *             <code>-1</code> if there is no more data because the end
  91      *             of the stream has been reached.
  92      * @exception  IOException if the first byte cannot be read for any reason
  93      * other than end of file, the stream has been closed and the underlying
  94      * input stream does not support reading after close, or another I/O
  95      * error occurs.
  96      * @see        java.io.FilterInputStream#in
  97      * @see        java.io.InputStream#read(byte[], int, int)
  98      */
  99     public final int read(byte b[]) throws IOException {
 100         return in.read(b, 0, b.length);
 101     }
 102 
 103     /**
 104      * Reads up to <code>len</code> bytes of data from the contained
 105      * input stream into an array of bytes.  An attempt is made to read
 106      * as many as <code>len</code> bytes, but a smaller number may be read,
 107      * possibly zero. The number of bytes actually read is returned as an
 108      * integer.
 109      *
 110      * <p> This method blocks until input data is available, end of file is
 111      * detected, or an exception is thrown.
 112      *
 113      * <p> If <code>len</code> is zero, then no bytes are read and
 114      * <code>0</code> is returned; otherwise, there is an attempt to read at
 115      * least one byte. If no byte is available because the stream is at end of
 116      * file, the value <code>-1</code> is returned; otherwise, at least one
 117      * byte is read and stored into <code>b</code>.
 118      *
 119      * <p> The first byte read is stored into element <code>b[off]</code>, the


 128      * <code>b[off]</code> and elements <code>b[off+len]</code> through
 129      * <code>b[b.length-1]</code> are unaffected.
 130      *
 131      * @param      b     the buffer into which the data is read.
 132      * @param off the start offset in the destination array <code>b</code>
 133      * @param      len   the maximum number of bytes read.
 134      * @return     the total number of bytes read into the buffer, or
 135      *             <code>-1</code> if there is no more data because the end
 136      *             of the stream has been reached.
 137      * @exception  NullPointerException If <code>b</code> is <code>null</code>.
 138      * @exception  IndexOutOfBoundsException If <code>off</code> is negative,
 139      * <code>len</code> is negative, or <code>len</code> is greater than
 140      * <code>b.length - off</code>
 141      * @exception  IOException if the first byte cannot be read for any reason
 142      * other than end of file, the stream has been closed and the underlying
 143      * input stream does not support reading after close, or another I/O
 144      * error occurs.
 145      * @see        java.io.FilterInputStream#in
 146      * @see        java.io.InputStream#read(byte[], int, int)
 147      */
 148     public final int read(byte b[], int off, int len) throws IOException {
 149         return in.read(b, off, len);
 150     }
 151 
 152     /**
 153      * See the general contract of the {@code readFully}
 154      * method of {@code DataInput}.
 155      * <p>
 156      * Bytes
 157      * for this operation are read from the contained
 158      * input stream.
 159      *
 160      * @param   b   the buffer into which the data is read.
 161      * @throws  NullPointerException if {@code b} is {@code null}.
 162      * @throws  EOFException  if this input stream reaches the end before
 163      *          reading all the bytes.
 164      * @throws  IOException   the stream has been closed and the contained
 165      *          input stream does not support reading after close, or
 166      *          another I/O error occurs.
 167      * @see     java.io.FilterInputStream#in
 168      */
 169     public final void readFully(byte b[]) throws IOException {
 170         readFully(b, 0, b.length);
 171     }
 172 
 173     /**
 174      * See the general contract of the {@code readFully}
 175      * method of {@code DataInput}.
 176      * <p>
 177      * Bytes
 178      * for this operation are read from the contained
 179      * input stream.
 180      *
 181      * @param      b     the buffer into which the data is read.
 182      * @param      off   the start offset in the data array {@code b}.
 183      * @param      len   the number of bytes to read.
 184      * @exception  NullPointerException if {@code b} is {@code null}.
 185      * @exception  IndexOutOfBoundsException if {@code off} is negative,
 186      *             {@code len} is negative, or {@code len} is greater than
 187      *             {@code b.length - off}.
 188      * @exception  EOFException  if this input stream reaches the end before
 189      *             reading all the bytes.
 190      * @exception  IOException   the stream has been closed and the contained
 191      *             input stream does not support reading after close, or
 192      *             another I/O error occurs.
 193      * @see        java.io.FilterInputStream#in
 194      */
 195     public final void readFully(byte b[], int off, int len) throws IOException {
 196         if (len < 0)
 197             throw new IndexOutOfBoundsException();
 198         int n = 0;
 199         while (n < len) {
 200             int count = in.read(b, off + n, len - n);
 201             if (count < 0)
 202                 throw new EOFException();
 203             n += count;
 204         }
 205     }
 206 
 207     /**
 208      * See the general contract of the <code>skipBytes</code>
 209      * method of <code>DataInput</code>.
 210      * <p>
 211      * Bytes for this operation are read from the contained
 212      * input stream.
 213      *
 214      * @param      n   the number of bytes to be skipped.
 215      * @return     the actual number of bytes skipped.


 381      *
 382      * @return     the next four bytes of this input stream, interpreted as an
 383      *             <code>int</code>.
 384      * @exception  EOFException  if this input stream reaches the end before
 385      *               reading four bytes.
 386      * @exception  IOException   the stream has been closed and the contained
 387      *             input stream does not support reading after close, or
 388      *             another I/O error occurs.
 389      * @see        java.io.FilterInputStream#in
 390      */
 391     public final int readInt() throws IOException {
 392         int ch1 = in.read();
 393         int ch2 = in.read();
 394         int ch3 = in.read();
 395         int ch4 = in.read();
 396         if ((ch1 | ch2 | ch3 | ch4) < 0)
 397             throw new EOFException();
 398         return ((ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0));
 399     }
 400 
 401     private byte readBuffer[] = new byte[8];
 402 
 403     /**
 404      * See the general contract of the <code>readLong</code>
 405      * method of <code>DataInput</code>.
 406      * <p>
 407      * Bytes
 408      * for this operation are read from the contained
 409      * input stream.
 410      *
 411      * @return     the next eight bytes of this input stream, interpreted as a
 412      *             <code>long</code>.
 413      * @exception  EOFException  if this input stream reaches the end before
 414      *               reading eight bytes.
 415      * @exception  IOException   the stream has been closed and the contained
 416      *             input stream does not support reading after close, or
 417      *             another I/O error occurs.
 418      * @see        java.io.FilterInputStream#in
 419      */
 420     public final long readLong() throws IOException {
 421         readFully(readBuffer, 0, 8);


 456      * method of <code>DataInput</code>.
 457      * <p>
 458      * Bytes
 459      * for this operation are read from the contained
 460      * input stream.
 461      *
 462      * @return     the next eight bytes of this input stream, interpreted as a
 463      *             <code>double</code>.
 464      * @exception  EOFException  if this input stream reaches the end before
 465      *               reading eight bytes.
 466      * @exception  IOException   the stream has been closed and the contained
 467      *             input stream does not support reading after close, or
 468      *             another I/O error occurs.
 469      * @see        java.io.DataInputStream#readLong()
 470      * @see        java.lang.Double#longBitsToDouble(long)
 471      */
 472     public final double readDouble() throws IOException {
 473         return Double.longBitsToDouble(readLong());
 474     }
 475 
 476     private char lineBuffer[];
 477 
 478     /**
 479      * See the general contract of the <code>readLine</code>
 480      * method of <code>DataInput</code>.
 481      * <p>
 482      * Bytes
 483      * for this operation are read from the contained
 484      * input stream.
 485      *
 486      * @deprecated This method does not properly convert bytes to characters.
 487      * As of JDK&nbsp;1.1, the preferred way to read lines of text is via the
 488      * <code>BufferedReader.readLine()</code> method.  Programs that use the
 489      * <code>DataInputStream</code> class to read lines can be converted to use
 490      * the <code>BufferedReader</code> class by replacing code of the form:
 491      * <blockquote><pre>
 492      *     DataInputStream d =&nbsp;new&nbsp;DataInputStream(in);
 493      * </pre></blockquote>
 494      * with:
 495      * <blockquote><pre>
 496      *     BufferedReader d
 497      *          =&nbsp;new&nbsp;BufferedReader(new&nbsp;InputStreamReader(in));
 498      * </pre></blockquote>
 499      *
 500      * @return     the next line of text from this input stream.
 501      * @exception  IOException  if an I/O error occurs.
 502      * @see        java.io.BufferedReader#readLine()
 503      * @see        java.io.FilterInputStream#in
 504      */
 505     @Deprecated
 506     public final String readLine() throws IOException {
 507         char buf[] = lineBuffer;
 508 
 509         if (buf == null) {
 510             buf = lineBuffer = new char[128];
 511         }
 512 
 513         int room = buf.length;
 514         int offset = 0;
 515         int c;
 516 
 517 loop:   while (true) {
 518             switch (c = in.read()) {
 519               case -1:
 520               case '\n':
 521                 break loop;
 522 
 523               case '\r':
 524                 int c2 = in.read();
 525                 if ((c2 != '\n') && (c2 != -1)) {
 526                     if (!(in instanceof PushbackInputStream)) {
 527                         this.in = new PushbackInputStream(in);




  38  * @author  Arthur van Hoff
  39  * @see     java.io.DataOutputStream
  40  * @since   1.0
  41  */
  42 public
  43 class DataInputStream extends FilterInputStream implements DataInput {
  44 
  45     /**
  46      * Creates a DataInputStream that uses the specified
  47      * underlying InputStream.
  48      *
  49      * @param  in   the specified input stream
  50      */
  51     public DataInputStream(InputStream in) {
  52         super(in);
  53     }
  54 
  55     /**
  56      * working arrays initialized on demand by readUTF
  57      */
  58     private byte[] bytearr = new byte[80];
  59     private char[] chararr = new char[80];
  60 
  61     /**
  62      * Reads some number of bytes from the contained input stream and
  63      * stores them into the buffer array <code>b</code>. The number of
  64      * bytes actually read is returned as an integer. This method blocks
  65      * until input data is available, end of file is detected, or an
  66      * exception is thrown.
  67      *
  68      * <p>If <code>b</code> is null, a <code>NullPointerException</code> is
  69      * thrown. If the length of <code>b</code> is zero, then no bytes are
  70      * read and <code>0</code> is returned; otherwise, there is an attempt
  71      * to read at least one byte. If no byte is available because the
  72      * stream is at end of file, the value <code>-1</code> is returned;
  73      * otherwise, at least one byte is read and stored into <code>b</code>.
  74      *
  75      * <p>The first byte read is stored into element <code>b[0]</code>, the
  76      * next one into <code>b[1]</code>, and so on. The number of bytes read
  77      * is, at most, equal to the length of <code>b</code>. Let <code>k</code>
  78      * be the number of bytes actually read; these bytes will be stored in
  79      * elements <code>b[0]</code> through <code>b[k-1]</code>, leaving
  80      * elements <code>b[k]</code> through <code>b[b.length-1]</code>
  81      * unaffected.
  82      *
  83      * <p>The <code>read(b)</code> method has the same effect as:
  84      * <blockquote><pre>
  85      * read(b, 0, b.length)
  86      * </pre></blockquote>
  87      *
  88      * @param      b   the buffer into which the data is read.
  89      * @return     the total number of bytes read into the buffer, or
  90      *             <code>-1</code> if there is no more data because the end
  91      *             of the stream has been reached.
  92      * @exception  IOException if the first byte cannot be read for any reason
  93      * other than end of file, the stream has been closed and the underlying
  94      * input stream does not support reading after close, or another I/O
  95      * error occurs.
  96      * @see        java.io.FilterInputStream#in
  97      * @see        java.io.InputStream#read(byte[], int, int)
  98      */
  99     public final int read(byte[] b) throws IOException {
 100         return in.read(b, 0, b.length);
 101     }
 102 
 103     /**
 104      * Reads up to <code>len</code> bytes of data from the contained
 105      * input stream into an array of bytes.  An attempt is made to read
 106      * as many as <code>len</code> bytes, but a smaller number may be read,
 107      * possibly zero. The number of bytes actually read is returned as an
 108      * integer.
 109      *
 110      * <p> This method blocks until input data is available, end of file is
 111      * detected, or an exception is thrown.
 112      *
 113      * <p> If <code>len</code> is zero, then no bytes are read and
 114      * <code>0</code> is returned; otherwise, there is an attempt to read at
 115      * least one byte. If no byte is available because the stream is at end of
 116      * file, the value <code>-1</code> is returned; otherwise, at least one
 117      * byte is read and stored into <code>b</code>.
 118      *
 119      * <p> The first byte read is stored into element <code>b[off]</code>, the


 128      * <code>b[off]</code> and elements <code>b[off+len]</code> through
 129      * <code>b[b.length-1]</code> are unaffected.
 130      *
 131      * @param      b     the buffer into which the data is read.
 132      * @param off the start offset in the destination array <code>b</code>
 133      * @param      len   the maximum number of bytes read.
 134      * @return     the total number of bytes read into the buffer, or
 135      *             <code>-1</code> if there is no more data because the end
 136      *             of the stream has been reached.
 137      * @exception  NullPointerException If <code>b</code> is <code>null</code>.
 138      * @exception  IndexOutOfBoundsException If <code>off</code> is negative,
 139      * <code>len</code> is negative, or <code>len</code> is greater than
 140      * <code>b.length - off</code>
 141      * @exception  IOException if the first byte cannot be read for any reason
 142      * other than end of file, the stream has been closed and the underlying
 143      * input stream does not support reading after close, or another I/O
 144      * error occurs.
 145      * @see        java.io.FilterInputStream#in
 146      * @see        java.io.InputStream#read(byte[], int, int)
 147      */
 148     public final int read(byte[] b, int off, int len) throws IOException {
 149         return in.read(b, off, len);
 150     }
 151 
 152     /**
 153      * See the general contract of the {@code readFully}
 154      * method of {@code DataInput}.
 155      * <p>
 156      * Bytes
 157      * for this operation are read from the contained
 158      * input stream.
 159      *
 160      * @param   b   the buffer into which the data is read.
 161      * @throws  NullPointerException if {@code b} is {@code null}.
 162      * @throws  EOFException  if this input stream reaches the end before
 163      *          reading all the bytes.
 164      * @throws  IOException   the stream has been closed and the contained
 165      *          input stream does not support reading after close, or
 166      *          another I/O error occurs.
 167      * @see     java.io.FilterInputStream#in
 168      */
 169     public final void readFully(byte[] b) throws IOException {
 170         readFully(b, 0, b.length);
 171     }
 172 
 173     /**
 174      * See the general contract of the {@code readFully}
 175      * method of {@code DataInput}.
 176      * <p>
 177      * Bytes
 178      * for this operation are read from the contained
 179      * input stream.
 180      *
 181      * @param      b     the buffer into which the data is read.
 182      * @param      off   the start offset in the data array {@code b}.
 183      * @param      len   the number of bytes to read.
 184      * @exception  NullPointerException if {@code b} is {@code null}.
 185      * @exception  IndexOutOfBoundsException if {@code off} is negative,
 186      *             {@code len} is negative, or {@code len} is greater than
 187      *             {@code b.length - off}.
 188      * @exception  EOFException  if this input stream reaches the end before
 189      *             reading all the bytes.
 190      * @exception  IOException   the stream has been closed and the contained
 191      *             input stream does not support reading after close, or
 192      *             another I/O error occurs.
 193      * @see        java.io.FilterInputStream#in
 194      */
 195     public final void readFully(byte[] b, int off, int len) throws IOException {
 196         if (len < 0)
 197             throw new IndexOutOfBoundsException();
 198         int n = 0;
 199         while (n < len) {
 200             int count = in.read(b, off + n, len - n);
 201             if (count < 0)
 202                 throw new EOFException();
 203             n += count;
 204         }
 205     }
 206 
 207     /**
 208      * See the general contract of the <code>skipBytes</code>
 209      * method of <code>DataInput</code>.
 210      * <p>
 211      * Bytes for this operation are read from the contained
 212      * input stream.
 213      *
 214      * @param      n   the number of bytes to be skipped.
 215      * @return     the actual number of bytes skipped.


 381      *
 382      * @return     the next four bytes of this input stream, interpreted as an
 383      *             <code>int</code>.
 384      * @exception  EOFException  if this input stream reaches the end before
 385      *               reading four bytes.
 386      * @exception  IOException   the stream has been closed and the contained
 387      *             input stream does not support reading after close, or
 388      *             another I/O error occurs.
 389      * @see        java.io.FilterInputStream#in
 390      */
 391     public final int readInt() throws IOException {
 392         int ch1 = in.read();
 393         int ch2 = in.read();
 394         int ch3 = in.read();
 395         int ch4 = in.read();
 396         if ((ch1 | ch2 | ch3 | ch4) < 0)
 397             throw new EOFException();
 398         return ((ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0));
 399     }
 400 
 401     private byte[] readBuffer = new byte[8];
 402 
 403     /**
 404      * See the general contract of the <code>readLong</code>
 405      * method of <code>DataInput</code>.
 406      * <p>
 407      * Bytes
 408      * for this operation are read from the contained
 409      * input stream.
 410      *
 411      * @return     the next eight bytes of this input stream, interpreted as a
 412      *             <code>long</code>.
 413      * @exception  EOFException  if this input stream reaches the end before
 414      *               reading eight bytes.
 415      * @exception  IOException   the stream has been closed and the contained
 416      *             input stream does not support reading after close, or
 417      *             another I/O error occurs.
 418      * @see        java.io.FilterInputStream#in
 419      */
 420     public final long readLong() throws IOException {
 421         readFully(readBuffer, 0, 8);


 456      * method of <code>DataInput</code>.
 457      * <p>
 458      * Bytes
 459      * for this operation are read from the contained
 460      * input stream.
 461      *
 462      * @return     the next eight bytes of this input stream, interpreted as a
 463      *             <code>double</code>.
 464      * @exception  EOFException  if this input stream reaches the end before
 465      *               reading eight bytes.
 466      * @exception  IOException   the stream has been closed and the contained
 467      *             input stream does not support reading after close, or
 468      *             another I/O error occurs.
 469      * @see        java.io.DataInputStream#readLong()
 470      * @see        java.lang.Double#longBitsToDouble(long)
 471      */
 472     public final double readDouble() throws IOException {
 473         return Double.longBitsToDouble(readLong());
 474     }
 475 
 476     private char[] lineBuffer;
 477 
 478     /**
 479      * See the general contract of the <code>readLine</code>
 480      * method of <code>DataInput</code>.
 481      * <p>
 482      * Bytes
 483      * for this operation are read from the contained
 484      * input stream.
 485      *
 486      * @deprecated This method does not properly convert bytes to characters.
 487      * As of JDK&nbsp;1.1, the preferred way to read lines of text is via the
 488      * <code>BufferedReader.readLine()</code> method.  Programs that use the
 489      * <code>DataInputStream</code> class to read lines can be converted to use
 490      * the <code>BufferedReader</code> class by replacing code of the form:
 491      * <blockquote><pre>
 492      *     DataInputStream d =&nbsp;new&nbsp;DataInputStream(in);
 493      * </pre></blockquote>
 494      * with:
 495      * <blockquote><pre>
 496      *     BufferedReader d
 497      *          =&nbsp;new&nbsp;BufferedReader(new&nbsp;InputStreamReader(in));
 498      * </pre></blockquote>
 499      *
 500      * @return     the next line of text from this input stream.
 501      * @exception  IOException  if an I/O error occurs.
 502      * @see        java.io.BufferedReader#readLine()
 503      * @see        java.io.FilterInputStream#in
 504      */
 505     @Deprecated
 506     public final String readLine() throws IOException {
 507         char[] buf = lineBuffer;
 508 
 509         if (buf == null) {
 510             buf = lineBuffer = new char[128];
 511         }
 512 
 513         int room = buf.length;
 514         int offset = 0;
 515         int c;
 516 
 517 loop:   while (true) {
 518             switch (c = in.read()) {
 519               case -1:
 520               case '\n':
 521                 break loop;
 522 
 523               case '\r':
 524                 int c2 = in.read();
 525                 if ((c2 != '\n') && (c2 != -1)) {
 526                     if (!(in instanceof PushbackInputStream)) {
 527                         this.in = new PushbackInputStream(in);


< prev index next >