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