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