1.1 --- a/emul/mini/src/main/java/java/io/DataInputStream.java Mon Feb 25 19:00:08 2013 +0100
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,700 +0,0 @@
1.4 -/*
1.5 - * Copyright (c) 1994, 2006, Oracle and/or its affiliates. All rights reserved.
1.6 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1.7 - *
1.8 - * This code is free software; you can redistribute it and/or modify it
1.9 - * under the terms of the GNU General Public License version 2 only, as
1.10 - * published by the Free Software Foundation. Oracle designates this
1.11 - * particular file as subject to the "Classpath" exception as provided
1.12 - * by Oracle in the LICENSE file that accompanied this code.
1.13 - *
1.14 - * This code is distributed in the hope that it will be useful, but WITHOUT
1.15 - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1.16 - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1.17 - * version 2 for more details (a copy is included in the LICENSE file that
1.18 - * accompanied this code).
1.19 - *
1.20 - * You should have received a copy of the GNU General Public License version
1.21 - * 2 along with this work; if not, write to the Free Software Foundation,
1.22 - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1.23 - *
1.24 - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1.25 - * or visit www.oracle.com if you need additional information or have any
1.26 - * questions.
1.27 - */
1.28 -
1.29 -package java.io;
1.30 -
1.31 -import org.apidesign.bck2brwsr.core.JavaScriptBody;
1.32 -import org.apidesign.bck2brwsr.emul.lang.System;
1.33 -
1.34 -/**
1.35 - * A data input stream lets an application read primitive Java data
1.36 - * types from an underlying input stream in a machine-independent
1.37 - * way. An application uses a data output stream to write data that
1.38 - * can later be read by a data input stream.
1.39 - * <p>
1.40 - * DataInputStream is not necessarily safe for multithreaded access.
1.41 - * Thread safety is optional and is the responsibility of users of
1.42 - * methods in this class.
1.43 - *
1.44 - * @author Arthur van Hoff
1.45 - * @see java.io.DataOutputStream
1.46 - * @since JDK1.0
1.47 - */
1.48 -public
1.49 -class DataInputStream extends FilterInputStream implements DataInput {
1.50 -
1.51 - /**
1.52 - * Creates a DataInputStream that uses the specified
1.53 - * underlying InputStream.
1.54 - *
1.55 - * @param in the specified input stream
1.56 - */
1.57 - public DataInputStream(InputStream in) {
1.58 - super(in);
1.59 - }
1.60 -
1.61 - /**
1.62 - * working arrays initialized on demand by readUTF
1.63 - */
1.64 - private byte bytearr[] = new byte[80];
1.65 - private char chararr[] = new char[80];
1.66 -
1.67 - /**
1.68 - * Reads some number of bytes from the contained input stream and
1.69 - * stores them into the buffer array <code>b</code>. The number of
1.70 - * bytes actually read is returned as an integer. This method blocks
1.71 - * until input data is available, end of file is detected, or an
1.72 - * exception is thrown.
1.73 - *
1.74 - * <p>If <code>b</code> is null, a <code>NullPointerException</code> is
1.75 - * thrown. If the length of <code>b</code> is zero, then no bytes are
1.76 - * read and <code>0</code> is returned; otherwise, there is an attempt
1.77 - * to read at least one byte. If no byte is available because the
1.78 - * stream is at end of file, the value <code>-1</code> is returned;
1.79 - * otherwise, at least one byte is read and stored into <code>b</code>.
1.80 - *
1.81 - * <p>The first byte read is stored into element <code>b[0]</code>, the
1.82 - * next one into <code>b[1]</code>, and so on. The number of bytes read
1.83 - * is, at most, equal to the length of <code>b</code>. Let <code>k</code>
1.84 - * be the number of bytes actually read; these bytes will be stored in
1.85 - * elements <code>b[0]</code> through <code>b[k-1]</code>, leaving
1.86 - * elements <code>b[k]</code> through <code>b[b.length-1]</code>
1.87 - * unaffected.
1.88 - *
1.89 - * <p>The <code>read(b)</code> method has the same effect as:
1.90 - * <blockquote><pre>
1.91 - * read(b, 0, b.length)
1.92 - * </pre></blockquote>
1.93 - *
1.94 - * @param b the buffer into which the data is read.
1.95 - * @return the total number of bytes read into the buffer, or
1.96 - * <code>-1</code> if there is no more data because the end
1.97 - * of the stream has been reached.
1.98 - * @exception IOException if the first byte cannot be read for any reason
1.99 - * other than end of file, the stream has been closed and the underlying
1.100 - * input stream does not support reading after close, or another I/O
1.101 - * error occurs.
1.102 - * @see java.io.FilterInputStream#in
1.103 - * @see java.io.InputStream#read(byte[], int, int)
1.104 - */
1.105 - public final int read(byte b[]) throws IOException {
1.106 - return in.read(b, 0, b.length);
1.107 - }
1.108 -
1.109 - /**
1.110 - * Reads up to <code>len</code> bytes of data from the contained
1.111 - * input stream into an array of bytes. An attempt is made to read
1.112 - * as many as <code>len</code> bytes, but a smaller number may be read,
1.113 - * possibly zero. The number of bytes actually read is returned as an
1.114 - * integer.
1.115 - *
1.116 - * <p> This method blocks until input data is available, end of file is
1.117 - * detected, or an exception is thrown.
1.118 - *
1.119 - * <p> If <code>len</code> is zero, then no bytes are read and
1.120 - * <code>0</code> is returned; otherwise, there is an attempt to read at
1.121 - * least one byte. If no byte is available because the stream is at end of
1.122 - * file, the value <code>-1</code> is returned; otherwise, at least one
1.123 - * byte is read and stored into <code>b</code>.
1.124 - *
1.125 - * <p> The first byte read is stored into element <code>b[off]</code>, the
1.126 - * next one into <code>b[off+1]</code>, and so on. The number of bytes read
1.127 - * is, at most, equal to <code>len</code>. Let <i>k</i> be the number of
1.128 - * bytes actually read; these bytes will be stored in elements
1.129 - * <code>b[off]</code> through <code>b[off+</code><i>k</i><code>-1]</code>,
1.130 - * leaving elements <code>b[off+</code><i>k</i><code>]</code> through
1.131 - * <code>b[off+len-1]</code> unaffected.
1.132 - *
1.133 - * <p> In every case, elements <code>b[0]</code> through
1.134 - * <code>b[off]</code> and elements <code>b[off+len]</code> through
1.135 - * <code>b[b.length-1]</code> are unaffected.
1.136 - *
1.137 - * @param b the buffer into which the data is read.
1.138 - * @param off the start offset in the destination array <code>b</code>
1.139 - * @param len the maximum number of bytes read.
1.140 - * @return the total number of bytes read into the buffer, or
1.141 - * <code>-1</code> if there is no more data because the end
1.142 - * of the stream has been reached.
1.143 - * @exception NullPointerException If <code>b</code> is <code>null</code>.
1.144 - * @exception IndexOutOfBoundsException If <code>off</code> is negative,
1.145 - * <code>len</code> is negative, or <code>len</code> is greater than
1.146 - * <code>b.length - off</code>
1.147 - * @exception IOException if the first byte cannot be read for any reason
1.148 - * other than end of file, the stream has been closed and the underlying
1.149 - * input stream does not support reading after close, or another I/O
1.150 - * error occurs.
1.151 - * @see java.io.FilterInputStream#in
1.152 - * @see java.io.InputStream#read(byte[], int, int)
1.153 - */
1.154 - public final int read(byte b[], int off, int len) throws IOException {
1.155 - return in.read(b, off, len);
1.156 - }
1.157 -
1.158 - /**
1.159 - * See the general contract of the <code>readFully</code>
1.160 - * method of <code>DataInput</code>.
1.161 - * <p>
1.162 - * Bytes
1.163 - * for this operation are read from the contained
1.164 - * input stream.
1.165 - *
1.166 - * @param b the buffer into which the data is read.
1.167 - * @exception EOFException if this input stream reaches the end before
1.168 - * reading all the bytes.
1.169 - * @exception IOException the stream has been closed and the contained
1.170 - * input stream does not support reading after close, or
1.171 - * another I/O error occurs.
1.172 - * @see java.io.FilterInputStream#in
1.173 - */
1.174 - public final void readFully(byte b[]) throws IOException {
1.175 - readFully(b, 0, b.length);
1.176 - }
1.177 -
1.178 - /**
1.179 - * See the general contract of the <code>readFully</code>
1.180 - * method of <code>DataInput</code>.
1.181 - * <p>
1.182 - * Bytes
1.183 - * for this operation are read from the contained
1.184 - * input stream.
1.185 - *
1.186 - * @param b the buffer into which the data is read.
1.187 - * @param off the start offset of the data.
1.188 - * @param len the number of bytes to read.
1.189 - * @exception EOFException if this input stream reaches the end before
1.190 - * reading all the bytes.
1.191 - * @exception IOException the stream has been closed and the contained
1.192 - * input stream does not support reading after close, or
1.193 - * another I/O error occurs.
1.194 - * @see java.io.FilterInputStream#in
1.195 - */
1.196 - public final void readFully(byte b[], int off, int len) throws IOException {
1.197 - if (len < 0)
1.198 - throw new IndexOutOfBoundsException();
1.199 - int n = 0;
1.200 - while (n < len) {
1.201 - int count = in.read(b, off + n, len - n);
1.202 - if (count < 0)
1.203 - throw new EOFException();
1.204 - n += count;
1.205 - }
1.206 - }
1.207 -
1.208 - /**
1.209 - * See the general contract of the <code>skipBytes</code>
1.210 - * method of <code>DataInput</code>.
1.211 - * <p>
1.212 - * Bytes for this operation are read from the contained
1.213 - * input stream.
1.214 - *
1.215 - * @param n the number of bytes to be skipped.
1.216 - * @return the actual number of bytes skipped.
1.217 - * @exception IOException if the contained input stream does not support
1.218 - * seek, or the stream has been closed and
1.219 - * the contained input stream does not support
1.220 - * reading after close, or another I/O error occurs.
1.221 - */
1.222 - public final int skipBytes(int n) throws IOException {
1.223 - int total = 0;
1.224 - int cur = 0;
1.225 -
1.226 - while ((total<n) && ((cur = (int) in.skip(n-total)) > 0)) {
1.227 - total += cur;
1.228 - }
1.229 -
1.230 - return total;
1.231 - }
1.232 -
1.233 - /**
1.234 - * See the general contract of the <code>readBoolean</code>
1.235 - * method of <code>DataInput</code>.
1.236 - * <p>
1.237 - * Bytes for this operation are read from the contained
1.238 - * input stream.
1.239 - *
1.240 - * @return the <code>boolean</code> value read.
1.241 - * @exception EOFException if this input stream has reached the end.
1.242 - * @exception IOException the stream has been closed and the contained
1.243 - * input stream does not support reading after close, or
1.244 - * another I/O error occurs.
1.245 - * @see java.io.FilterInputStream#in
1.246 - */
1.247 - public final boolean readBoolean() throws IOException {
1.248 - int ch = in.read();
1.249 - if (ch < 0)
1.250 - throw new EOFException();
1.251 - return (ch != 0);
1.252 - }
1.253 -
1.254 - /**
1.255 - * See the general contract of the <code>readByte</code>
1.256 - * method of <code>DataInput</code>.
1.257 - * <p>
1.258 - * Bytes
1.259 - * for this operation are read from the contained
1.260 - * input stream.
1.261 - *
1.262 - * @return the next byte of this input stream as a signed 8-bit
1.263 - * <code>byte</code>.
1.264 - * @exception EOFException if this input stream has reached the end.
1.265 - * @exception IOException the stream has been closed and the contained
1.266 - * input stream does not support reading after close, or
1.267 - * another I/O error occurs.
1.268 - * @see java.io.FilterInputStream#in
1.269 - */
1.270 - public final byte readByte() throws IOException {
1.271 - int ch = in.read();
1.272 - if (ch < 0)
1.273 - throw new EOFException();
1.274 - return (byte)(ch);
1.275 - }
1.276 -
1.277 - /**
1.278 - * See the general contract of the <code>readUnsignedByte</code>
1.279 - * method of <code>DataInput</code>.
1.280 - * <p>
1.281 - * Bytes
1.282 - * for this operation are read from the contained
1.283 - * input stream.
1.284 - *
1.285 - * @return the next byte of this input stream, interpreted as an
1.286 - * unsigned 8-bit number.
1.287 - * @exception EOFException if this input stream has reached the end.
1.288 - * @exception IOException the stream has been closed and the contained
1.289 - * input stream does not support reading after close, or
1.290 - * another I/O error occurs.
1.291 - * @see java.io.FilterInputStream#in
1.292 - */
1.293 - public final int readUnsignedByte() throws IOException {
1.294 - int ch = in.read();
1.295 - if (ch < 0)
1.296 - throw new EOFException();
1.297 - return ch;
1.298 - }
1.299 -
1.300 - /**
1.301 - * See the general contract of the <code>readShort</code>
1.302 - * method of <code>DataInput</code>.
1.303 - * <p>
1.304 - * Bytes
1.305 - * for this operation are read from the contained
1.306 - * input stream.
1.307 - *
1.308 - * @return the next two bytes of this input stream, interpreted as a
1.309 - * signed 16-bit number.
1.310 - * @exception EOFException if this input stream reaches the end before
1.311 - * reading two bytes.
1.312 - * @exception IOException the stream has been closed and the contained
1.313 - * input stream does not support reading after close, or
1.314 - * another I/O error occurs.
1.315 - * @see java.io.FilterInputStream#in
1.316 - */
1.317 - public final short readShort() throws IOException {
1.318 - int ch1 = in.read();
1.319 - int ch2 = in.read();
1.320 - if ((ch1 | ch2) < 0)
1.321 - throw new EOFException();
1.322 - return (short)((ch1 << 8) + (ch2 << 0));
1.323 - }
1.324 -
1.325 - /**
1.326 - * See the general contract of the <code>readUnsignedShort</code>
1.327 - * method of <code>DataInput</code>.
1.328 - * <p>
1.329 - * Bytes
1.330 - * for this operation are read from the contained
1.331 - * input stream.
1.332 - *
1.333 - * @return the next two bytes of this input stream, interpreted as an
1.334 - * unsigned 16-bit integer.
1.335 - * @exception EOFException if this input stream reaches the end before
1.336 - * reading two bytes.
1.337 - * @exception IOException the stream has been closed and the contained
1.338 - * input stream does not support reading after close, or
1.339 - * another I/O error occurs.
1.340 - * @see java.io.FilterInputStream#in
1.341 - */
1.342 - public final int readUnsignedShort() throws IOException {
1.343 - int ch1 = in.read();
1.344 - int ch2 = in.read();
1.345 - if ((ch1 | ch2) < 0)
1.346 - throw new EOFException();
1.347 - return (ch1 << 8) + (ch2 << 0);
1.348 - }
1.349 -
1.350 - /**
1.351 - * See the general contract of the <code>readChar</code>
1.352 - * method of <code>DataInput</code>.
1.353 - * <p>
1.354 - * Bytes
1.355 - * for this operation are read from the contained
1.356 - * input stream.
1.357 - *
1.358 - * @return the next two bytes of this input stream, interpreted as a
1.359 - * <code>char</code>.
1.360 - * @exception EOFException if this input stream reaches the end before
1.361 - * reading two bytes.
1.362 - * @exception IOException the stream has been closed and the contained
1.363 - * input stream does not support reading after close, or
1.364 - * another I/O error occurs.
1.365 - * @see java.io.FilterInputStream#in
1.366 - */
1.367 - public final char readChar() throws IOException {
1.368 - int ch1 = in.read();
1.369 - int ch2 = in.read();
1.370 - if ((ch1 | ch2) < 0)
1.371 - throw new EOFException();
1.372 - return (char)((ch1 << 8) + (ch2 << 0));
1.373 - }
1.374 -
1.375 - /**
1.376 - * See the general contract of the <code>readInt</code>
1.377 - * method of <code>DataInput</code>.
1.378 - * <p>
1.379 - * Bytes
1.380 - * for this operation are read from the contained
1.381 - * input stream.
1.382 - *
1.383 - * @return the next four bytes of this input stream, interpreted as an
1.384 - * <code>int</code>.
1.385 - * @exception EOFException if this input stream reaches the end before
1.386 - * reading four bytes.
1.387 - * @exception IOException the stream has been closed and the contained
1.388 - * input stream does not support reading after close, or
1.389 - * another I/O error occurs.
1.390 - * @see java.io.FilterInputStream#in
1.391 - */
1.392 - public final int readInt() throws IOException {
1.393 - int ch1 = in.read();
1.394 - int ch2 = in.read();
1.395 - int ch3 = in.read();
1.396 - int ch4 = in.read();
1.397 - if ((ch1 | ch2 | ch3 | ch4) < 0)
1.398 - throw new EOFException();
1.399 - return ((ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0));
1.400 - }
1.401 -
1.402 - private byte readBuffer[] = new byte[8];
1.403 -
1.404 - /**
1.405 - * See the general contract of the <code>readLong</code>
1.406 - * method of <code>DataInput</code>.
1.407 - * <p>
1.408 - * Bytes
1.409 - * for this operation are read from the contained
1.410 - * input stream.
1.411 - *
1.412 - * @return the next eight bytes of this input stream, interpreted as a
1.413 - * <code>long</code>.
1.414 - * @exception EOFException if this input stream reaches the end before
1.415 - * reading eight bytes.
1.416 - * @exception IOException the stream has been closed and the contained
1.417 - * input stream does not support reading after close, or
1.418 - * another I/O error occurs.
1.419 - * @see java.io.FilterInputStream#in
1.420 - */
1.421 - public final long readLong() throws IOException {
1.422 - readFully(readBuffer, 0, 8);
1.423 - return (((long)readBuffer[0] << 56) +
1.424 - ((long)(readBuffer[1] & 255) << 48) +
1.425 - ((long)(readBuffer[2] & 255) << 40) +
1.426 - ((long)(readBuffer[3] & 255) << 32) +
1.427 - ((long)(readBuffer[4] & 255) << 24) +
1.428 - ((readBuffer[5] & 255) << 16) +
1.429 - ((readBuffer[6] & 255) << 8) +
1.430 - ((readBuffer[7] & 255) << 0));
1.431 - }
1.432 -
1.433 - /**
1.434 - * See the general contract of the <code>readFloat</code>
1.435 - * method of <code>DataInput</code>.
1.436 - * <p>
1.437 - * Bytes
1.438 - * for this operation are read from the contained
1.439 - * input stream.
1.440 - *
1.441 - * @return the next four bytes of this input stream, interpreted as a
1.442 - * <code>float</code>.
1.443 - * @exception EOFException if this input stream reaches the end before
1.444 - * reading four bytes.
1.445 - * @exception IOException the stream has been closed and the contained
1.446 - * input stream does not support reading after close, or
1.447 - * another I/O error occurs.
1.448 - * @see java.io.DataInputStream#readInt()
1.449 - * @see java.lang.Float#intBitsToFloat(int)
1.450 - */
1.451 - public final float readFloat() throws IOException {
1.452 - return Float.intBitsToFloat(readInt());
1.453 - }
1.454 -
1.455 - /**
1.456 - * See the general contract of the <code>readDouble</code>
1.457 - * method of <code>DataInput</code>.
1.458 - * <p>
1.459 - * Bytes
1.460 - * for this operation are read from the contained
1.461 - * input stream.
1.462 - *
1.463 - * @return the next eight bytes of this input stream, interpreted as a
1.464 - * <code>double</code>.
1.465 - * @exception EOFException if this input stream reaches the end before
1.466 - * reading eight bytes.
1.467 - * @exception IOException the stream has been closed and the contained
1.468 - * input stream does not support reading after close, or
1.469 - * another I/O error occurs.
1.470 - * @see java.io.DataInputStream#readLong()
1.471 - * @see java.lang.Double#longBitsToDouble(long)
1.472 - */
1.473 - public final double readDouble() throws IOException {
1.474 - int hi = readInt();
1.475 - int low = readInt();
1.476 - return toDouble(hi, low);
1.477 - }
1.478 -
1.479 - @JavaScriptBody(args={ "hi", "low" },
1.480 - body=
1.481 - "if (low == 0) {\n"
1.482 - + " if (hi === 0x7ff00000) return Number.POSITIVE_INFINITY;\n"
1.483 - + " if (hi === 0xfff00000) return Number.NEGATIVE_INFINITY;\n"
1.484 - + "}\n"
1.485 - + "if (hi >= 0x7ff00000 && hi <= 0x7fffffff) return Number.NaN;\n"
1.486 - + "if (hi >= 0xfff00000 && hi <= 0xffffffff) return Number.NaN;\n"
1.487 - + "var s = (hi & 0x80000000) === 0 ? 1 : -1;\n"
1.488 - + "var e = (hi >> 20) & 0x7ff;\n"
1.489 - + "var to32 = low >> 0;\n"
1.490 - + "if (e === 0) {\n"
1.491 - + " if (to32 & 0x80000000) {\n"
1.492 - + " hi = hi << 1 + 1; low = low << 1;\n"
1.493 - + " } else {\n"
1.494 - + " hi = hi << 1; low = low << 1;\n"
1.495 - + " }\n"
1.496 - + "} else {\n"
1.497 - + " hi = (hi & 0xfffff) | 0x100000;\n"
1.498 - + "}\n"
1.499 - + "to32 = low >> 0;\n"
1.500 - + "var m = Math.pow(2.0, 32) * hi + to32;\n"
1.501 - + "var r = s * m * Math.pow(2.0, e - 1075);\n"
1.502 - + "//throw 'exp: ' + e + ' sign: ' + s + ' hi:' + hi + ' low: ' + low + ' m: ' + m + ' r: ' + r;\n"
1.503 - + "return r;\n"
1.504 - )
1.505 - private static double toDouble(int hi, int low) {
1.506 - long both = hi;
1.507 - both = (both << 32) & low;
1.508 - return Double.doubleToLongBits(both);
1.509 - }
1.510 -
1.511 - private char lineBuffer[];
1.512 -
1.513 - /**
1.514 - * See the general contract of the <code>readLine</code>
1.515 - * method of <code>DataInput</code>.
1.516 - * <p>
1.517 - * Bytes
1.518 - * for this operation are read from the contained
1.519 - * input stream.
1.520 - *
1.521 - * @deprecated This method does not properly convert bytes to characters.
1.522 - * As of JDK 1.1, the preferred way to read lines of text is via the
1.523 - * <code>BufferedReader.readLine()</code> method. Programs that use the
1.524 - * <code>DataInputStream</code> class to read lines can be converted to use
1.525 - * the <code>BufferedReader</code> class by replacing code of the form:
1.526 - * <blockquote><pre>
1.527 - * DataInputStream d = new DataInputStream(in);
1.528 - * </pre></blockquote>
1.529 - * with:
1.530 - * <blockquote><pre>
1.531 - * BufferedReader d
1.532 - * = new BufferedReader(new InputStreamReader(in));
1.533 - * </pre></blockquote>
1.534 - *
1.535 - * @return the next line of text from this input stream.
1.536 - * @exception IOException if an I/O error occurs.
1.537 - * @see java.io.BufferedReader#readLine()
1.538 - * @see java.io.FilterInputStream#in
1.539 - */
1.540 - @Deprecated
1.541 - public final String readLine() throws IOException {
1.542 - char buf[] = lineBuffer;
1.543 -
1.544 - if (buf == null) {
1.545 - buf = lineBuffer = new char[128];
1.546 - }
1.547 -
1.548 - int room = buf.length;
1.549 - int offset = 0;
1.550 - int c;
1.551 -
1.552 -loop: while (true) {
1.553 - switch (c = in.read()) {
1.554 - case -1:
1.555 - case '\n':
1.556 - break loop;
1.557 -
1.558 - case '\r':
1.559 - int c2 = in.read();
1.560 - if ((c2 != '\n') && (c2 != -1)) {
1.561 - if (!(in instanceof PushbackInputStream)) {
1.562 - this.in = new PushbackInputStream(in);
1.563 - }
1.564 - ((PushbackInputStream)in).unread(c2);
1.565 - }
1.566 - break loop;
1.567 -
1.568 - default:
1.569 - if (--room < 0) {
1.570 - buf = new char[offset + 128];
1.571 - room = buf.length - offset - 1;
1.572 - System.arraycopy(lineBuffer, 0, buf, 0, offset);
1.573 - lineBuffer = buf;
1.574 - }
1.575 - buf[offset++] = (char) c;
1.576 - break;
1.577 - }
1.578 - }
1.579 - if ((c == -1) && (offset == 0)) {
1.580 - return null;
1.581 - }
1.582 - return String.copyValueOf(buf, 0, offset);
1.583 - }
1.584 -
1.585 - /**
1.586 - * See the general contract of the <code>readUTF</code>
1.587 - * method of <code>DataInput</code>.
1.588 - * <p>
1.589 - * Bytes
1.590 - * for this operation are read from the contained
1.591 - * input stream.
1.592 - *
1.593 - * @return a Unicode string.
1.594 - * @exception EOFException if this input stream reaches the end before
1.595 - * reading all the bytes.
1.596 - * @exception IOException the stream has been closed and the contained
1.597 - * input stream does not support reading after close, or
1.598 - * another I/O error occurs.
1.599 - * @exception UTFDataFormatException if the bytes do not represent a valid
1.600 - * modified UTF-8 encoding of a string.
1.601 - * @see java.io.DataInputStream#readUTF(java.io.DataInput)
1.602 - */
1.603 - public final String readUTF() throws IOException {
1.604 - return readUTF(this);
1.605 - }
1.606 -
1.607 - /**
1.608 - * Reads from the
1.609 - * stream <code>in</code> a representation
1.610 - * of a Unicode character string encoded in
1.611 - * <a href="DataInput.html#modified-utf-8">modified UTF-8</a> format;
1.612 - * this string of characters is then returned as a <code>String</code>.
1.613 - * The details of the modified UTF-8 representation
1.614 - * are exactly the same as for the <code>readUTF</code>
1.615 - * method of <code>DataInput</code>.
1.616 - *
1.617 - * @param in a data input stream.
1.618 - * @return a Unicode string.
1.619 - * @exception EOFException if the input stream reaches the end
1.620 - * before all the bytes.
1.621 - * @exception IOException the stream has been closed and the contained
1.622 - * input stream does not support reading after close, or
1.623 - * another I/O error occurs.
1.624 - * @exception UTFDataFormatException if the bytes do not represent a
1.625 - * valid modified UTF-8 encoding of a Unicode string.
1.626 - * @see java.io.DataInputStream#readUnsignedShort()
1.627 - */
1.628 - public final static String readUTF(DataInput in) throws IOException {
1.629 - int utflen = in.readUnsignedShort();
1.630 - byte[] bytearr = null;
1.631 - char[] chararr = null;
1.632 - if (in instanceof DataInputStream) {
1.633 - DataInputStream dis = (DataInputStream)in;
1.634 - if (dis.bytearr.length < utflen){
1.635 - dis.bytearr = new byte[utflen*2];
1.636 - dis.chararr = new char[utflen*2];
1.637 - }
1.638 - chararr = dis.chararr;
1.639 - bytearr = dis.bytearr;
1.640 - } else {
1.641 - bytearr = new byte[utflen];
1.642 - chararr = new char[utflen];
1.643 - }
1.644 -
1.645 - int c, char2, char3;
1.646 - int count = 0;
1.647 - int chararr_count=0;
1.648 -
1.649 - in.readFully(bytearr, 0, utflen);
1.650 -
1.651 - while (count < utflen) {
1.652 - c = (int) bytearr[count] & 0xff;
1.653 - if (c > 127) break;
1.654 - count++;
1.655 - chararr[chararr_count++]=(char)c;
1.656 - }
1.657 -
1.658 - while (count < utflen) {
1.659 - c = (int) bytearr[count] & 0xff;
1.660 - switch (c >> 4) {
1.661 - case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
1.662 - /* 0xxxxxxx*/
1.663 - count++;
1.664 - chararr[chararr_count++]=(char)c;
1.665 - break;
1.666 - case 12: case 13:
1.667 - /* 110x xxxx 10xx xxxx*/
1.668 - count += 2;
1.669 - if (count > utflen)
1.670 - throw new UTFDataFormatException(
1.671 - "malformed input: partial character at end");
1.672 - char2 = (int) bytearr[count-1];
1.673 - if ((char2 & 0xC0) != 0x80)
1.674 - throw new UTFDataFormatException(
1.675 - "malformed input around byte " + count);
1.676 - chararr[chararr_count++]=(char)(((c & 0x1F) << 6) |
1.677 - (char2 & 0x3F));
1.678 - break;
1.679 - case 14:
1.680 - /* 1110 xxxx 10xx xxxx 10xx xxxx */
1.681 - count += 3;
1.682 - if (count > utflen)
1.683 - throw new UTFDataFormatException(
1.684 - "malformed input: partial character at end");
1.685 - char2 = (int) bytearr[count-2];
1.686 - char3 = (int) bytearr[count-1];
1.687 - if (((char2 & 0xC0) != 0x80) || ((char3 & 0xC0) != 0x80))
1.688 - throw new UTFDataFormatException(
1.689 - "malformed input around byte " + (count-1));
1.690 - chararr[chararr_count++]=(char)(((c & 0x0F) << 12) |
1.691 - ((char2 & 0x3F) << 6) |
1.692 - ((char3 & 0x3F) << 0));
1.693 - break;
1.694 - default:
1.695 - /* 10xx xxxx, 1111 xxxx */
1.696 - throw new UTFDataFormatException(
1.697 - "malformed input around byte " + count);
1.698 - }
1.699 - }
1.700 - // The number of chars produced may be less than utflen
1.701 - return new String(chararr, 0, chararr_count);
1.702 - }
1.703 -}