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