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