src/java.base/share/classes/java/io/DataInputStream.java
author jboes
Thu, 21 Nov 2019 09:10:21 +0000
changeset 59201 b24f4caa1411
parent 58288 48e480e56aad
permissions -rw-r--r--
8234335: Remove line break in class declaration in java.base Summary: Remove line break in class declarations where applicable Reviewed-by: rriggs, lancea
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
     2
 * Copyright (c) 1994, 2019, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.io;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * A data input stream lets an application read primitive Java data
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * types from an underlying input stream in a machine-independent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * way. An application uses a data output stream to write data that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * can later be read by a data input stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * DataInputStream is not necessarily safe for multithreaded access.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * Thread safety is optional and is the responsibility of users of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * methods in this class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * @author  Arthur van Hoff
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * @see     java.io.DataOutputStream
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 5506
diff changeset
    40
 * @since   1.0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 */
59201
b24f4caa1411 8234335: Remove line break in class declaration in java.base
jboes
parents: 58288
diff changeset
    42
public class DataInputStream extends FilterInputStream implements DataInput {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
     * Creates a DataInputStream that uses the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
     * underlying InputStream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
     * @param  in   the specified input stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    public DataInputStream(InputStream in) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        super(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * working arrays initialized on demand by readUTF
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private byte bytearr[] = new byte[80];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    private char chararr[] = new char[80];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * Reads some number of bytes from the contained input stream and
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
    62
     * stores them into the buffer array {@code b}. The number of
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * bytes actually read is returned as an integer. This method blocks
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * until input data is available, end of file is detected, or an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * exception is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     *
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
    67
     * <p>If {@code b} is null, a {@code NullPointerException} is
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
    68
     * thrown. If the length of {@code b} is zero, then no bytes are
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
    69
     * read and {@code 0} is returned; otherwise, there is an attempt
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * to read at least one byte. If no byte is available because the
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
    71
     * stream is at end of file, the value {@code -1} is returned;
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
    72
     * otherwise, at least one byte is read and stored into {@code b}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     *
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
    74
     * <p>The first byte read is stored into element {@code b[0]}, the
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
    75
     * next one into {@code b[1]}, and so on. The number of bytes read
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
    76
     * is, at most, equal to the length of {@code b}. Let {@code k}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * be the number of bytes actually read; these bytes will be stored in
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
    78
     * elements {@code b[0]} through {@code b[k-1]}, leaving
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
    79
     * elements {@code b[k]} through {@code b[b.length-1]}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * unaffected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     *
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
    82
     * <p>The {@code read(b)} method has the same effect as:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * read(b, 0, b.length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * </pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * @param      b   the buffer into which the data is read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * @return     the total number of bytes read into the buffer, or
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
    89
     *             {@code -1} if there is no more data because the end
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     *             of the stream has been reached.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
    91
     * @throws     IOException if the first byte cannot be read for any reason
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
    92
     *             other than end of file, the stream has been closed and the underlying
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
    93
     *             input stream does not support reading after close, or another I/O
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
    94
     *             error occurs.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * @see        java.io.FilterInputStream#in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * @see        java.io.InputStream#read(byte[], int, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    public final int read(byte b[]) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        return in.read(b, 0, b.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    /**
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   103
     * Reads up to {@code len} bytes of data from the contained
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * input stream into an array of bytes.  An attempt is made to read
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   105
     * as many as {@code len} bytes, but a smaller number may be read,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * possibly zero. The number of bytes actually read is returned as an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * integer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * <p> This method blocks until input data is available, end of file is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * detected, or an exception is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     *
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   112
     * <p> If {@code len} is zero, then no bytes are read and
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   113
     * {@code 0} is returned; otherwise, there is an attempt to read at
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * least one byte. If no byte is available because the stream is at end of
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   115
     * file, the value {@code -1} is returned; otherwise, at least one
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   116
     * byte is read and stored into {@code b}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     *
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   118
     * <p> The first byte read is stored into element {@code b[off]}, the
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   119
     * next one into {@code b[off+1]}, and so on. The number of bytes read
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   120
     * is, at most, equal to {@code len}. Let <i>k</i> be the number of
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * bytes actually read; these bytes will be stored in elements
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   122
     * {@code b[off]} through {@code b[off+}<i>k</i>{@code -1]},
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   123
     * leaving elements {@code b[off+}<i>k</i>{@code ]} through
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   124
     * {@code b[off+len-1]} unaffected.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     *
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   126
     * <p> In every case, elements {@code b[0]} through
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   127
     * {@code b[off]} and elements {@code b[off+len]} through
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   128
     * {@code b[b.length-1]} are unaffected.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * @param      b     the buffer into which the data is read.
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   131
     * @param      off the start offset in the destination array {@code b}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * @param      len   the maximum number of bytes read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * @return     the total number of bytes read into the buffer, or
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   134
     *             {@code -1} if there is no more data because the end
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     *             of the stream has been reached.
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   136
     * @throws     NullPointerException If {@code b} is {@code null}.
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   137
     * @throws     IndexOutOfBoundsException If {@code off} is negative,
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   138
     *             {@code len} is negative, or {@code len} is greater than
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   139
     *             {@code b.length - off}
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
   140
     * @throws     IOException if the first byte cannot be read for any reason
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
   141
     *             other than end of file, the stream has been closed and the underlying
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
   142
     *             input stream does not support reading after close, or another I/O
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
   143
     *             error occurs.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * @see        java.io.FilterInputStream#in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * @see        java.io.InputStream#read(byte[], int, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    public final int read(byte b[], int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        return in.read(b, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    /**
37591
b71bda3ce058 8154183: (spec) Spec of read(byte[],int,int) and readFully(byte[],int,int) is confusing/incomplete
bpb
parents: 32649
diff changeset
   152
     * See the general contract of the {@code readFully}
b71bda3ce058 8154183: (spec) Spec of read(byte[],int,int) and readFully(byte[],int,int) is confusing/incomplete
bpb
parents: 32649
diff changeset
   153
     * method of {@code DataInput}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * Bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * for this operation are read from the contained
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * input stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     *
37591
b71bda3ce058 8154183: (spec) Spec of read(byte[],int,int) and readFully(byte[],int,int) is confusing/incomplete
bpb
parents: 32649
diff changeset
   159
     * @param   b   the buffer into which the data is read.
b71bda3ce058 8154183: (spec) Spec of read(byte[],int,int) and readFully(byte[],int,int) is confusing/incomplete
bpb
parents: 32649
diff changeset
   160
     * @throws  NullPointerException if {@code b} is {@code null}.
b71bda3ce058 8154183: (spec) Spec of read(byte[],int,int) and readFully(byte[],int,int) is confusing/incomplete
bpb
parents: 32649
diff changeset
   161
     * @throws  EOFException  if this input stream reaches the end before
b71bda3ce058 8154183: (spec) Spec of read(byte[],int,int) and readFully(byte[],int,int) is confusing/incomplete
bpb
parents: 32649
diff changeset
   162
     *          reading all the bytes.
b71bda3ce058 8154183: (spec) Spec of read(byte[],int,int) and readFully(byte[],int,int) is confusing/incomplete
bpb
parents: 32649
diff changeset
   163
     * @throws  IOException   the stream has been closed and the contained
b71bda3ce058 8154183: (spec) Spec of read(byte[],int,int) and readFully(byte[],int,int) is confusing/incomplete
bpb
parents: 32649
diff changeset
   164
     *          input stream does not support reading after close, or
b71bda3ce058 8154183: (spec) Spec of read(byte[],int,int) and readFully(byte[],int,int) is confusing/incomplete
bpb
parents: 32649
diff changeset
   165
     *          another I/O error occurs.
b71bda3ce058 8154183: (spec) Spec of read(byte[],int,int) and readFully(byte[],int,int) is confusing/incomplete
bpb
parents: 32649
diff changeset
   166
     * @see     java.io.FilterInputStream#in
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    public final void readFully(byte b[]) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        readFully(b, 0, b.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    /**
37591
b71bda3ce058 8154183: (spec) Spec of read(byte[],int,int) and readFully(byte[],int,int) is confusing/incomplete
bpb
parents: 32649
diff changeset
   173
     * See the general contract of the {@code readFully}
b71bda3ce058 8154183: (spec) Spec of read(byte[],int,int) and readFully(byte[],int,int) is confusing/incomplete
bpb
parents: 32649
diff changeset
   174
     * method of {@code DataInput}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * Bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * for this operation are read from the contained
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * input stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * @param      b     the buffer into which the data is read.
37591
b71bda3ce058 8154183: (spec) Spec of read(byte[],int,int) and readFully(byte[],int,int) is confusing/incomplete
bpb
parents: 32649
diff changeset
   181
     * @param      off   the start offset in the data array {@code b}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * @param      len   the number of bytes to read.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
   183
     * @throws     NullPointerException if {@code b} is {@code null}.
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
   184
     * @throws     IndexOutOfBoundsException if {@code off} is negative,
37591
b71bda3ce058 8154183: (spec) Spec of read(byte[],int,int) and readFully(byte[],int,int) is confusing/incomplete
bpb
parents: 32649
diff changeset
   185
     *             {@code len} is negative, or {@code len} is greater than
b71bda3ce058 8154183: (spec) Spec of read(byte[],int,int) and readFully(byte[],int,int) is confusing/incomplete
bpb
parents: 32649
diff changeset
   186
     *             {@code b.length - off}.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
   187
     * @throws     EOFException  if this input stream reaches the end before
37591
b71bda3ce058 8154183: (spec) Spec of read(byte[],int,int) and readFully(byte[],int,int) is confusing/incomplete
bpb
parents: 32649
diff changeset
   188
     *             reading all the bytes.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
   189
     * @throws     IOException   the stream has been closed and the contained
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     *             input stream does not support reading after close, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     *             another I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * @see        java.io.FilterInputStream#in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    public final void readFully(byte b[], int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        if (len < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            throw new IndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        int n = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        while (n < len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            int count = in.read(b, off + n, len - n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            if (count < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                throw new EOFException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            n += count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    /**
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   207
     * See the general contract of the {@code skipBytes}
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   208
     * method of {@code DataInput}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * Bytes for this operation are read from the contained
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * input stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * @param      n   the number of bytes to be skipped.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * @return     the actual number of bytes skipped.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
   215
     * @throws     IOException  if the contained input stream does not support
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     *             seek, or the stream has been closed and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     *             the contained input stream does not support
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     *             reading after close, or another I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    public final int skipBytes(int n) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        int total = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        int cur = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        while ((total<n) && ((cur = (int) in.skip(n-total)) > 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            total += cur;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        return total;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    /**
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   232
     * See the general contract of the {@code readBoolean}
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   233
     * method of {@code DataInput}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * Bytes for this operation are read from the contained
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * input stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     *
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   238
     * @return     the {@code boolean} value read.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
   239
     * @throws     EOFException  if this input stream has reached the end.
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
   240
     * @throws     IOException   the stream has been closed and the contained
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     *             input stream does not support reading after close, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     *             another I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * @see        java.io.FilterInputStream#in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    public final boolean readBoolean() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        int ch = in.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        if (ch < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            throw new EOFException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        return (ch != 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    /**
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   253
     * See the general contract of the {@code readByte}
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   254
     * method of {@code DataInput}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * Bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * for this operation are read from the contained
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * input stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * @return     the next byte of this input stream as a signed 8-bit
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   261
     *             {@code byte}.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
   262
     * @throws     EOFException  if this input stream has reached the end.
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
   263
     * @throws     IOException   the stream has been closed and the contained
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     *             input stream does not support reading after close, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     *             another I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * @see        java.io.FilterInputStream#in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    public final byte readByte() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        int ch = in.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        if (ch < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            throw new EOFException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        return (byte)(ch);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    /**
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   276
     * See the general contract of the {@code readUnsignedByte}
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   277
     * method of {@code DataInput}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * Bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * for this operation are read from the contained
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * input stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * @return     the next byte of this input stream, interpreted as an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     *             unsigned 8-bit number.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
   285
     * @throws     EOFException  if this input stream has reached the end.
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
   286
     * @throws     IOException   the stream has been closed and the contained
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     *             input stream does not support reading after close, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     *             another I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * @see         java.io.FilterInputStream#in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    public final int readUnsignedByte() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        int ch = in.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        if (ch < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            throw new EOFException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        return ch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    /**
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   299
     * See the general contract of the {@code readShort}
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   300
     * method of {@code DataInput}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * Bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * for this operation are read from the contained
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * input stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * @return     the next two bytes of this input stream, interpreted as a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     *             signed 16-bit number.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
   308
     * @throws     EOFException  if this input stream reaches the end before
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     *               reading two bytes.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
   310
     * @throws     IOException   the stream has been closed and the contained
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     *             input stream does not support reading after close, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     *             another I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * @see        java.io.FilterInputStream#in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    public final short readShort() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        int ch1 = in.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        int ch2 = in.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        if ((ch1 | ch2) < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            throw new EOFException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        return (short)((ch1 << 8) + (ch2 << 0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    /**
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   324
     * See the general contract of the {@code readUnsignedShort}
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   325
     * method of {@code DataInput}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * Bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * for this operation are read from the contained
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * input stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * @return     the next two bytes of this input stream, interpreted as an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     *             unsigned 16-bit integer.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
   333
     * @throws     EOFException  if this input stream reaches the end before
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     *             reading two bytes.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
   335
     * @throws     IOException   the stream has been closed and the contained
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     *             input stream does not support reading after close, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     *             another I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * @see        java.io.FilterInputStream#in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    public final int readUnsignedShort() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        int ch1 = in.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        int ch2 = in.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        if ((ch1 | ch2) < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            throw new EOFException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        return (ch1 << 8) + (ch2 << 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    /**
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   349
     * See the general contract of the {@code readChar}
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   350
     * method of {@code DataInput}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * Bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * for this operation are read from the contained
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * input stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * @return     the next two bytes of this input stream, interpreted as a
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   357
     *             {@code char}.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
   358
     * @throws     EOFException  if this input stream reaches the end before
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     *               reading two bytes.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
   360
     * @throws     IOException   the stream has been closed and the contained
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     *             input stream does not support reading after close, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     *             another I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * @see        java.io.FilterInputStream#in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    public final char readChar() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        int ch1 = in.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        int ch2 = in.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        if ((ch1 | ch2) < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            throw new EOFException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        return (char)((ch1 << 8) + (ch2 << 0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    /**
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   374
     * See the general contract of the {@code readInt}
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   375
     * method of {@code DataInput}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * Bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     * for this operation are read from the contained
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     * input stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     * @return     the next four bytes of this input stream, interpreted as an
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   382
     *             {@code int}.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
   383
     * @throws     EOFException  if this input stream reaches the end before
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     *               reading four bytes.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
   385
     * @throws     IOException   the stream has been closed and the contained
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     *             input stream does not support reading after close, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     *             another I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     * @see        java.io.FilterInputStream#in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    public final int readInt() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        int ch1 = in.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        int ch2 = in.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        int ch3 = in.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        int ch4 = in.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        if ((ch1 | ch2 | ch3 | ch4) < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
            throw new EOFException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        return ((ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
    private byte readBuffer[] = new byte[8];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
    /**
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   403
     * See the general contract of the {@code readLong}
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   404
     * method of {@code DataInput}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     * Bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     * for this operation are read from the contained
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     * input stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     * @return     the next eight bytes of this input stream, interpreted as a
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   411
     *             {@code long}.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
   412
     * @throws     EOFException  if this input stream reaches the end before
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     *               reading eight bytes.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
   414
     * @throws     IOException   the stream has been closed and the contained
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     *             input stream does not support reading after close, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     *             another I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     * @see        java.io.FilterInputStream#in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
    public final long readLong() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        readFully(readBuffer, 0, 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        return (((long)readBuffer[0] << 56) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                ((long)(readBuffer[1] & 255) << 48) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                ((long)(readBuffer[2] & 255) << 40) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                ((long)(readBuffer[3] & 255) << 32) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                ((long)(readBuffer[4] & 255) << 24) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                ((readBuffer[5] & 255) << 16) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                ((readBuffer[6] & 255) <<  8) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                ((readBuffer[7] & 255) <<  0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
    /**
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   432
     * See the general contract of the {@code readFloat}
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   433
     * method of {@code DataInput}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     * Bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     * for this operation are read from the contained
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     * input stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     * @return     the next four bytes of this input stream, interpreted as a
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   440
     *             {@code float}.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
   441
     * @throws     EOFException  if this input stream reaches the end before
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     *               reading four bytes.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
   443
     * @throws     IOException   the stream has been closed and the contained
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     *             input stream does not support reading after close, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     *             another I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     * @see        java.io.DataInputStream#readInt()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     * @see        java.lang.Float#intBitsToFloat(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
    public final float readFloat() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        return Float.intBitsToFloat(readInt());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
    /**
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   454
     * See the general contract of the {@code readDouble}
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   455
     * method of {@code DataInput}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
     * Bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     * for this operation are read from the contained
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     * input stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     * @return     the next eight bytes of this input stream, interpreted as a
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   462
     *             {@code double}.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
   463
     * @throws     EOFException  if this input stream reaches the end before
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     *               reading eight bytes.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
   465
     * @throws     IOException   the stream has been closed and the contained
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     *             input stream does not support reading after close, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     *             another I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     * @see        java.io.DataInputStream#readLong()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     * @see        java.lang.Double#longBitsToDouble(long)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
    public final double readDouble() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        return Double.longBitsToDouble(readLong());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
    private char lineBuffer[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
    /**
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   478
     * See the general contract of the {@code readLine}
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   479
     * method of {@code DataInput}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     * Bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     * for this operation are read from the contained
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     * input stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     * @deprecated This method does not properly convert bytes to characters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     * As of JDK&nbsp;1.1, the preferred way to read lines of text is via the
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   487
     * {@code BufferedReader.readLine()} method.  Programs that use the
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   488
     * {@code DataInputStream} class to read lines can be converted to use
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   489
     * the {@code BufferedReader} class by replacing code of the form:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     *     DataInputStream d =&nbsp;new&nbsp;DataInputStream(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     * </pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     * with:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     *     BufferedReader d
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     *          =&nbsp;new&nbsp;BufferedReader(new&nbsp;InputStreamReader(in));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     * </pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     * @return     the next line of text from this input stream.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
   500
     * @throws     IOException  if an I/O error occurs.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     * @see        java.io.BufferedReader#readLine()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     * @see        java.io.FilterInputStream#in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
    @Deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
    public final String readLine() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
        char buf[] = lineBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
        if (buf == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
            buf = lineBuffer = new char[128];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
        int room = buf.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
        int offset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
        int c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
loop:   while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
            switch (c = in.read()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
              case -1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
              case '\n':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
                break loop;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
              case '\r':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
                int c2 = in.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
                if ((c2 != '\n') && (c2 != -1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
                    if (!(in instanceof PushbackInputStream)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
                        this.in = new PushbackInputStream(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
                    ((PushbackInputStream)in).unread(c2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
                break loop;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
              default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                if (--room < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
                    buf = new char[offset + 128];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
                    room = buf.length - offset - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
                    System.arraycopy(lineBuffer, 0, buf, 0, offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
                    lineBuffer = buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
                buf[offset++] = (char) c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
        if ((c == -1) && (offset == 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        return String.copyValueOf(buf, 0, offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
    /**
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   550
     * See the general contract of the {@code readUTF}
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   551
     * method of {@code DataInput}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
     * Bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
     * for this operation are read from the contained
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     * input stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     * @return     a Unicode string.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
   558
     * @throws     EOFException  if this input stream reaches the end before
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     *               reading all the bytes.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
   560
     * @throws     IOException   the stream has been closed and the contained
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
     *             input stream does not support reading after close, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
     *             another I/O error occurs.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
   563
     * @throws     UTFDataFormatException if the bytes do not represent a valid
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
     *             modified UTF-8 encoding of a string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
     * @see        java.io.DataInputStream#readUTF(java.io.DataInput)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
    public final String readUTF() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
        return readUTF(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
     * Reads from the
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   573
     * stream {@code in} a representation
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
     * of a Unicode  character string encoded in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
     * <a href="DataInput.html#modified-utf-8">modified UTF-8</a> format;
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   576
     * this string of characters is then returned as a {@code String}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
     * The details of the modified UTF-8 representation
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   578
     * are  exactly the same as for the {@code readUTF}
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   579
     * method of {@code DataInput}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     * @param      in   a data input stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     * @return     a Unicode string.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
   583
     * @throws     EOFException            if the input stream reaches the end
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     *               before all the bytes.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
   585
     * @throws     IOException   the stream has been closed and the contained
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     *             input stream does not support reading after close, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
     *             another I/O error occurs.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
   588
     * @throws     UTFDataFormatException  if the bytes do not represent a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
     *               valid modified UTF-8 encoding of a Unicode string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
     * @see        java.io.DataInputStream#readUnsignedShort()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
     */
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 25859
diff changeset
   592
    public static final String readUTF(DataInput in) throws IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
        int utflen = in.readUnsignedShort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
        byte[] bytearr = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
        char[] chararr = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
        if (in instanceof DataInputStream) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
            DataInputStream dis = (DataInputStream)in;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
            if (dis.bytearr.length < utflen){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
                dis.bytearr = new byte[utflen*2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
                dis.chararr = new char[utflen*2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
            chararr = dis.chararr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
            bytearr = dis.bytearr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
            bytearr = new byte[utflen];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
            chararr = new char[utflen];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
        int c, char2, char3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
        int count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
        int chararr_count=0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
        in.readFully(bytearr, 0, utflen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
        while (count < utflen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
            c = (int) bytearr[count] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
            if (c > 127) break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
            count++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
            chararr[chararr_count++]=(char)c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
        while (count < utflen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
            c = (int) bytearr[count] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
            switch (c >> 4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
                case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
                    /* 0xxxxxxx*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
                    count++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
                    chararr[chararr_count++]=(char)c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
                case 12: case 13:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
                    /* 110x xxxx   10xx xxxx*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
                    count += 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
                    if (count > utflen)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
                        throw new UTFDataFormatException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
                            "malformed input: partial character at end");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
                    char2 = (int) bytearr[count-1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
                    if ((char2 & 0xC0) != 0x80)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
                        throw new UTFDataFormatException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
                            "malformed input around byte " + count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
                    chararr[chararr_count++]=(char)(((c & 0x1F) << 6) |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
                                                    (char2 & 0x3F));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
                case 14:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
                    /* 1110 xxxx  10xx xxxx  10xx xxxx */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
                    count += 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
                    if (count > utflen)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
                        throw new UTFDataFormatException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
                            "malformed input: partial character at end");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
                    char2 = (int) bytearr[count-2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
                    char3 = (int) bytearr[count-1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
                    if (((char2 & 0xC0) != 0x80) || ((char3 & 0xC0) != 0x80))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
                        throw new UTFDataFormatException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
                            "malformed input around byte " + (count-1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
                    chararr[chararr_count++]=(char)(((c     & 0x0F) << 12) |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
                                                    ((char2 & 0x3F) << 6)  |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
                                                    ((char3 & 0x3F) << 0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
                default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
                    /* 10xx xxxx,  1111 xxxx */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
                    throw new UTFDataFormatException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
                        "malformed input around byte " + count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
        // The number of chars produced may be less than utflen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
        return new String(chararr, 0, chararr_count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
}