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