1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/emul/src/main/java/java/io/DataInput.java Sat Nov 10 17:27:07 2012 +0100
1.3 @@ -0,0 +1,635 @@
1.4 +/*
1.5 + * Copyright (c) 1995, 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 + * The <code>DataInput</code> interface provides
1.33 + * for reading bytes from a binary stream and
1.34 + * reconstructing from them data in any of
1.35 + * the Java primitive types. There is also
1.36 + * a
1.37 + * facility for reconstructing a <code>String</code>
1.38 + * from data in
1.39 + * <a href="#modified-utf-8">modified UTF-8</a>
1.40 + * format.
1.41 + * <p>
1.42 + * It is generally true of all the reading
1.43 + * routines in this interface that if end of
1.44 + * file is reached before the desired number
1.45 + * of bytes has been read, an <code>EOFException</code>
1.46 + * (which is a kind of <code>IOException</code>)
1.47 + * is thrown. If any byte cannot be read for
1.48 + * any reason other than end of file, an <code>IOException</code>
1.49 + * other than <code>EOFException</code> is
1.50 + * thrown. In particular, an <code>IOException</code>
1.51 + * may be thrown if the input stream has been
1.52 + * closed.
1.53 + *
1.54 + * <h4><a name="modified-utf-8">Modified UTF-8</a></h4>
1.55 + * <p>
1.56 + * Implementations of the DataInput and DataOutput interfaces represent
1.57 + * Unicode strings in a format that is a slight modification of UTF-8.
1.58 + * (For information regarding the standard UTF-8 format, see section
1.59 + * <i>3.9 Unicode Encoding Forms</i> of <i>The Unicode Standard, Version
1.60 + * 4.0</i>).
1.61 + * Note that in the following tables, the most significant bit appears in the
1.62 + * far left-hand column.
1.63 + * <p>
1.64 + * All characters in the range <code>'\u0001'</code> to
1.65 + * <code>'\u007F'</code> are represented by a single byte:
1.66 + *
1.67 + * <blockquote>
1.68 + * <table border="1" cellspacing="0" cellpadding="8" width="50%"
1.69 + * summary="Bit values and bytes">
1.70 + * <tr>
1.71 + * <td></td>
1.72 + * <th id="bit">Bit Values</th>
1.73 + * </tr>
1.74 + * <tr>
1.75 + * <th id="byte1">Byte 1</th>
1.76 + * <td>
1.77 + * <table border="1" cellspacing="0" width="100%">
1.78 + * <tr>
1.79 + * <td width="12%"><center>0</center>
1.80 + * <td colspan="7"><center>bits 6-0</center>
1.81 + * </tr>
1.82 + * </table>
1.83 + * </td>
1.84 + * </tr>
1.85 + * </table>
1.86 + * </blockquote>
1.87 + *
1.88 + * <p>
1.89 + * The null character <code>'\u0000'</code> and characters in the
1.90 + * range <code>'\u0080'</code> to <code>'\u07FF'</code> are
1.91 + * represented by a pair of bytes:
1.92 + *
1.93 + * <blockquote>
1.94 + * <table border="1" cellspacing="0" cellpadding="8" width="50%"
1.95 + * summary="Bit values and bytes">
1.96 + * <tr>
1.97 + * <td></td>
1.98 + * <th id="bit">Bit Values</th>
1.99 + * </tr>
1.100 + * <tr>
1.101 + * <th id="byte1">Byte 1</th>
1.102 + * <td>
1.103 + * <table border="1" cellspacing="0" width="100%">
1.104 + * <tr>
1.105 + * <td width="12%"><center>1</center>
1.106 + * <td width="13%"><center>1</center>
1.107 + * <td width="12%"><center>0</center>
1.108 + * <td colspan="5"><center>bits 10-6</center>
1.109 + * </tr>
1.110 + * </table>
1.111 + * </td>
1.112 + * </tr>
1.113 + * <tr>
1.114 + * <th id="byte2">Byte 2</th>
1.115 + * <td>
1.116 + * <table border="1" cellspacing="0" width="100%">
1.117 + * <tr>
1.118 + * <td width="12%"><center>1</center>
1.119 + * <td width="13%"><center>0</center>
1.120 + * <td colspan="6"><center>bits 5-0</center>
1.121 + * </tr>
1.122 + * </table>
1.123 + * </td>
1.124 + * </tr>
1.125 + * </table>
1.126 + * </blockquote>
1.127 + *
1.128 + * <br>
1.129 + * <code>char</code> values in the range <code>'\u0800'</code> to
1.130 + * <code>'\uFFFF'</code> are represented by three bytes:
1.131 + *
1.132 + * <blockquote>
1.133 + * <table border="1" cellspacing="0" cellpadding="8" width="50%"
1.134 + * summary="Bit values and bytes">
1.135 + * <tr>
1.136 + * <td></td>
1.137 + * <th id="bit">Bit Values</th>
1.138 + * </tr>
1.139 + * <tr>
1.140 + * <th id="byte1">Byte 1</th>
1.141 + * <td>
1.142 + * <table border="1" cellspacing="0" width="100%">
1.143 + * <tr>
1.144 + * <td width="12%"><center>1</center>
1.145 + * <td width="13%"><center>1</center>
1.146 + * <td width="12%"><center>1</center>
1.147 + * <td width="13%"><center>0</center>
1.148 + * <td colspan="4"><center>bits 15-12</center>
1.149 + * </tr>
1.150 + * </table>
1.151 + * </td>
1.152 + * </tr>
1.153 + * <tr>
1.154 + * <th id="byte2">Byte 2</th>
1.155 + * <td>
1.156 + * <table border="1" cellspacing="0" width="100%">
1.157 + * <tr>
1.158 + * <td width="12%"><center>1</center>
1.159 + * <td width="13%"><center>0</center>
1.160 + * <td colspan="6"><center>bits 11-6</center>
1.161 + * </tr>
1.162 + * </table>
1.163 + * </td>
1.164 + * </tr>
1.165 + * <tr>
1.166 + * <th id="byte3">Byte 3</th>
1.167 + * <td>
1.168 + * <table border="1" cellspacing="0" width="100%">
1.169 + * <tr>
1.170 + * <td width="12%"><center>1</center>
1.171 + * <td width="13%"><center>0</center>
1.172 + * <td colspan="6"><center>bits 5-0</center>
1.173 + * </tr>
1.174 + * </table>
1.175 + * </td>
1.176 + * </tr>
1.177 + * </table>
1.178 + * </blockquote>
1.179 + *
1.180 + * <p>
1.181 + * The differences between this format and the
1.182 + * standard UTF-8 format are the following:
1.183 + * <ul>
1.184 + * <li>The null byte <code>'\u0000'</code> is encoded in 2-byte format
1.185 + * rather than 1-byte, so that the encoded strings never have
1.186 + * embedded nulls.
1.187 + * <li>Only the 1-byte, 2-byte, and 3-byte formats are used.
1.188 + * <li><a href="../lang/Character.html#unicode">Supplementary characters</a>
1.189 + * are represented in the form of surrogate pairs.
1.190 + * </ul>
1.191 + * @author Frank Yellin
1.192 + * @see java.io.DataInputStream
1.193 + * @see java.io.DataOutput
1.194 + * @since JDK1.0
1.195 + */
1.196 +public
1.197 +interface DataInput {
1.198 + /**
1.199 + * Reads some bytes from an input
1.200 + * stream and stores them into the buffer
1.201 + * array <code>b</code>. The number of bytes
1.202 + * read is equal
1.203 + * to the length of <code>b</code>.
1.204 + * <p>
1.205 + * This method blocks until one of the
1.206 + * following conditions occurs:<p>
1.207 + * <ul>
1.208 + * <li><code>b.length</code>
1.209 + * bytes of input data are available, in which
1.210 + * case a normal return is made.
1.211 + *
1.212 + * <li>End of
1.213 + * file is detected, in which case an <code>EOFException</code>
1.214 + * is thrown.
1.215 + *
1.216 + * <li>An I/O error occurs, in
1.217 + * which case an <code>IOException</code> other
1.218 + * than <code>EOFException</code> is thrown.
1.219 + * </ul>
1.220 + * <p>
1.221 + * If <code>b</code> is <code>null</code>,
1.222 + * a <code>NullPointerException</code> is thrown.
1.223 + * If <code>b.length</code> is zero, then
1.224 + * no bytes are read. Otherwise, the first
1.225 + * byte read is stored into element <code>b[0]</code>,
1.226 + * the next one into <code>b[1]</code>, and
1.227 + * so on.
1.228 + * If an exception is thrown from
1.229 + * this method, then it may be that some but
1.230 + * not all bytes of <code>b</code> have been
1.231 + * updated with data from the input stream.
1.232 + *
1.233 + * @param b the buffer into which the data is read.
1.234 + * @exception EOFException if this stream reaches the end before reading
1.235 + * all the bytes.
1.236 + * @exception IOException if an I/O error occurs.
1.237 + */
1.238 + void readFully(byte b[]) throws IOException;
1.239 +
1.240 + /**
1.241 + *
1.242 + * Reads <code>len</code>
1.243 + * bytes from
1.244 + * an input stream.
1.245 + * <p>
1.246 + * This method
1.247 + * blocks until one of the following conditions
1.248 + * occurs:<p>
1.249 + * <ul>
1.250 + * <li><code>len</code> bytes
1.251 + * of input data are available, in which case
1.252 + * a normal return is made.
1.253 + *
1.254 + * <li>End of file
1.255 + * is detected, in which case an <code>EOFException</code>
1.256 + * is thrown.
1.257 + *
1.258 + * <li>An I/O error occurs, in
1.259 + * which case an <code>IOException</code> other
1.260 + * than <code>EOFException</code> is thrown.
1.261 + * </ul>
1.262 + * <p>
1.263 + * If <code>b</code> is <code>null</code>,
1.264 + * a <code>NullPointerException</code> is thrown.
1.265 + * If <code>off</code> is negative, or <code>len</code>
1.266 + * is negative, or <code>off+len</code> is
1.267 + * greater than the length of the array <code>b</code>,
1.268 + * then an <code>IndexOutOfBoundsException</code>
1.269 + * is thrown.
1.270 + * If <code>len</code> is zero,
1.271 + * then no bytes are read. Otherwise, the first
1.272 + * byte read is stored into element <code>b[off]</code>,
1.273 + * the next one into <code>b[off+1]</code>,
1.274 + * and so on. The number of bytes read is,
1.275 + * at most, equal to <code>len</code>.
1.276 + *
1.277 + * @param b the buffer into which the data is read.
1.278 + * @param off an int specifying the offset into the data.
1.279 + * @param len an int specifying the number of bytes to read.
1.280 + * @exception EOFException if this stream reaches the end before reading
1.281 + * all the bytes.
1.282 + * @exception IOException if an I/O error occurs.
1.283 + */
1.284 + void readFully(byte b[], int off, int len) throws IOException;
1.285 +
1.286 + /**
1.287 + * Makes an attempt to skip over
1.288 + * <code>n</code> bytes
1.289 + * of data from the input
1.290 + * stream, discarding the skipped bytes. However,
1.291 + * it may skip
1.292 + * over some smaller number of
1.293 + * bytes, possibly zero. This may result from
1.294 + * any of a
1.295 + * number of conditions; reaching
1.296 + * end of file before <code>n</code> bytes
1.297 + * have been skipped is
1.298 + * only one possibility.
1.299 + * This method never throws an <code>EOFException</code>.
1.300 + * The actual
1.301 + * number of bytes skipped is returned.
1.302 + *
1.303 + * @param n the number of bytes to be skipped.
1.304 + * @return the number of bytes actually skipped.
1.305 + * @exception IOException if an I/O error occurs.
1.306 + */
1.307 + int skipBytes(int n) throws IOException;
1.308 +
1.309 + /**
1.310 + * Reads one input byte and returns
1.311 + * <code>true</code> if that byte is nonzero,
1.312 + * <code>false</code> if that byte is zero.
1.313 + * This method is suitable for reading
1.314 + * the byte written by the <code>writeBoolean</code>
1.315 + * method of interface <code>DataOutput</code>.
1.316 + *
1.317 + * @return the <code>boolean</code> value read.
1.318 + * @exception EOFException if this stream reaches the end before reading
1.319 + * all the bytes.
1.320 + * @exception IOException if an I/O error occurs.
1.321 + */
1.322 + boolean readBoolean() throws IOException;
1.323 +
1.324 + /**
1.325 + * Reads and returns one input byte.
1.326 + * The byte is treated as a signed value in
1.327 + * the range <code>-128</code> through <code>127</code>,
1.328 + * inclusive.
1.329 + * This method is suitable for
1.330 + * reading the byte written by the <code>writeByte</code>
1.331 + * method of interface <code>DataOutput</code>.
1.332 + *
1.333 + * @return the 8-bit value read.
1.334 + * @exception EOFException if this stream reaches the end before reading
1.335 + * all the bytes.
1.336 + * @exception IOException if an I/O error occurs.
1.337 + */
1.338 + byte readByte() throws IOException;
1.339 +
1.340 + /**
1.341 + * Reads one input byte, zero-extends
1.342 + * it to type <code>int</code>, and returns
1.343 + * the result, which is therefore in the range
1.344 + * <code>0</code>
1.345 + * through <code>255</code>.
1.346 + * This method is suitable for reading
1.347 + * the byte written by the <code>writeByte</code>
1.348 + * method of interface <code>DataOutput</code>
1.349 + * if the argument to <code>writeByte</code>
1.350 + * was intended to be a value in the range
1.351 + * <code>0</code> through <code>255</code>.
1.352 + *
1.353 + * @return the unsigned 8-bit value read.
1.354 + * @exception EOFException if this stream reaches the end before reading
1.355 + * all the bytes.
1.356 + * @exception IOException if an I/O error occurs.
1.357 + */
1.358 + int readUnsignedByte() throws IOException;
1.359 +
1.360 + /**
1.361 + * Reads two input bytes and returns
1.362 + * a <code>short</code> value. Let <code>a</code>
1.363 + * be the first byte read and <code>b</code>
1.364 + * be the second byte. The value
1.365 + * returned
1.366 + * is:
1.367 + * <p><pre><code>(short)((a << 8) | (b & 0xff))
1.368 + * </code></pre>
1.369 + * This method
1.370 + * is suitable for reading the bytes written
1.371 + * by the <code>writeShort</code> method of
1.372 + * interface <code>DataOutput</code>.
1.373 + *
1.374 + * @return the 16-bit value read.
1.375 + * @exception EOFException if this stream reaches the end before reading
1.376 + * all the bytes.
1.377 + * @exception IOException if an I/O error occurs.
1.378 + */
1.379 + short readShort() throws IOException;
1.380 +
1.381 + /**
1.382 + * Reads two input bytes and returns
1.383 + * an <code>int</code> value in the range <code>0</code>
1.384 + * through <code>65535</code>. Let <code>a</code>
1.385 + * be the first byte read and
1.386 + * <code>b</code>
1.387 + * be the second byte. The value returned is:
1.388 + * <p><pre><code>(((a & 0xff) << 8) | (b & 0xff))
1.389 + * </code></pre>
1.390 + * This method is suitable for reading the bytes
1.391 + * written by the <code>writeShort</code> method
1.392 + * of interface <code>DataOutput</code> if
1.393 + * the argument to <code>writeShort</code>
1.394 + * was intended to be a value in the range
1.395 + * <code>0</code> through <code>65535</code>.
1.396 + *
1.397 + * @return the unsigned 16-bit value read.
1.398 + * @exception EOFException if this stream reaches the end before reading
1.399 + * all the bytes.
1.400 + * @exception IOException if an I/O error occurs.
1.401 + */
1.402 + int readUnsignedShort() throws IOException;
1.403 +
1.404 + /**
1.405 + * Reads two input bytes and returns a <code>char</code> value.
1.406 + * Let <code>a</code>
1.407 + * be the first byte read and <code>b</code>
1.408 + * be the second byte. The value
1.409 + * returned is:
1.410 + * <p><pre><code>(char)((a << 8) | (b & 0xff))
1.411 + * </code></pre>
1.412 + * This method
1.413 + * is suitable for reading bytes written by
1.414 + * the <code>writeChar</code> method of interface
1.415 + * <code>DataOutput</code>.
1.416 + *
1.417 + * @return the <code>char</code> value read.
1.418 + * @exception EOFException if this stream reaches the end before reading
1.419 + * all the bytes.
1.420 + * @exception IOException if an I/O error occurs.
1.421 + */
1.422 + char readChar() throws IOException;
1.423 +
1.424 + /**
1.425 + * Reads four input bytes and returns an
1.426 + * <code>int</code> value. Let <code>a-d</code>
1.427 + * be the first through fourth bytes read. The value returned is:
1.428 + * <p><pre>
1.429 + * <code>
1.430 + * (((a & 0xff) << 24) | ((b & 0xff) << 16) |
1.431 + *  ((c & 0xff) << 8) | (d & 0xff))
1.432 + * </code></pre>
1.433 + * This method is suitable
1.434 + * for reading bytes written by the <code>writeInt</code>
1.435 + * method of interface <code>DataOutput</code>.
1.436 + *
1.437 + * @return the <code>int</code> value read.
1.438 + * @exception EOFException if this stream reaches the end before reading
1.439 + * all the bytes.
1.440 + * @exception IOException if an I/O error occurs.
1.441 + */
1.442 + int readInt() throws IOException;
1.443 +
1.444 + /**
1.445 + * Reads eight input bytes and returns
1.446 + * a <code>long</code> value. Let <code>a-h</code>
1.447 + * be the first through eighth bytes read.
1.448 + * The value returned is:
1.449 + * <p><pre> <code>
1.450 + * (((long)(a & 0xff) << 56) |
1.451 + * ((long)(b & 0xff) << 48) |
1.452 + * ((long)(c & 0xff) << 40) |
1.453 + * ((long)(d & 0xff) << 32) |
1.454 + * ((long)(e & 0xff) << 24) |
1.455 + * ((long)(f & 0xff) << 16) |
1.456 + * ((long)(g & 0xff) << 8) |
1.457 + * ((long)(h & 0xff)))
1.458 + * </code></pre>
1.459 + * <p>
1.460 + * This method is suitable
1.461 + * for reading bytes written by the <code>writeLong</code>
1.462 + * method of interface <code>DataOutput</code>.
1.463 + *
1.464 + * @return the <code>long</code> value read.
1.465 + * @exception EOFException if this stream reaches the end before reading
1.466 + * all the bytes.
1.467 + * @exception IOException if an I/O error occurs.
1.468 + */
1.469 + long readLong() throws IOException;
1.470 +
1.471 + /**
1.472 + * Reads four input bytes and returns
1.473 + * a <code>float</code> value. It does this
1.474 + * by first constructing an <code>int</code>
1.475 + * value in exactly the manner
1.476 + * of the <code>readInt</code>
1.477 + * method, then converting this <code>int</code>
1.478 + * value to a <code>float</code> in
1.479 + * exactly the manner of the method <code>Float.intBitsToFloat</code>.
1.480 + * This method is suitable for reading
1.481 + * bytes written by the <code>writeFloat</code>
1.482 + * method of interface <code>DataOutput</code>.
1.483 + *
1.484 + * @return the <code>float</code> value read.
1.485 + * @exception EOFException if this stream reaches the end before reading
1.486 + * all the bytes.
1.487 + * @exception IOException if an I/O error occurs.
1.488 + */
1.489 + float readFloat() throws IOException;
1.490 +
1.491 + /**
1.492 + * Reads eight input bytes and returns
1.493 + * a <code>double</code> value. It does this
1.494 + * by first constructing a <code>long</code>
1.495 + * value in exactly the manner
1.496 + * of the <code>readlong</code>
1.497 + * method, then converting this <code>long</code>
1.498 + * value to a <code>double</code> in exactly
1.499 + * the manner of the method <code>Double.longBitsToDouble</code>.
1.500 + * This method is suitable for reading
1.501 + * bytes written by the <code>writeDouble</code>
1.502 + * method of interface <code>DataOutput</code>.
1.503 + *
1.504 + * @return the <code>double</code> value read.
1.505 + * @exception EOFException if this stream reaches the end before reading
1.506 + * all the bytes.
1.507 + * @exception IOException if an I/O error occurs.
1.508 + */
1.509 + double readDouble() throws IOException;
1.510 +
1.511 + /**
1.512 + * Reads the next line of text from the input stream.
1.513 + * It reads successive bytes, converting
1.514 + * each byte separately into a character,
1.515 + * until it encounters a line terminator or
1.516 + * end of
1.517 + * file; the characters read are then
1.518 + * returned as a <code>String</code>. Note
1.519 + * that because this
1.520 + * method processes bytes,
1.521 + * it does not support input of the full Unicode
1.522 + * character set.
1.523 + * <p>
1.524 + * If end of file is encountered
1.525 + * before even one byte can be read, then <code>null</code>
1.526 + * is returned. Otherwise, each byte that is
1.527 + * read is converted to type <code>char</code>
1.528 + * by zero-extension. If the character <code>'\n'</code>
1.529 + * is encountered, it is discarded and reading
1.530 + * ceases. If the character <code>'\r'</code>
1.531 + * is encountered, it is discarded and, if
1.532 + * the following byte converts  to the
1.533 + * character <code>'\n'</code>, then that is
1.534 + * discarded also; reading then ceases. If
1.535 + * end of file is encountered before either
1.536 + * of the characters <code>'\n'</code> and
1.537 + * <code>'\r'</code> is encountered, reading
1.538 + * ceases. Once reading has ceased, a <code>String</code>
1.539 + * is returned that contains all the characters
1.540 + * read and not discarded, taken in order.
1.541 + * Note that every character in this string
1.542 + * will have a value less than <code>\u0100</code>,
1.543 + * that is, <code>(char)256</code>.
1.544 + *
1.545 + * @return the next line of text from the input stream,
1.546 + * or <CODE>null</CODE> if the end of file is
1.547 + * encountered before a byte can be read.
1.548 + * @exception IOException if an I/O error occurs.
1.549 + */
1.550 + String readLine() throws IOException;
1.551 +
1.552 + /**
1.553 + * Reads in a string that has been encoded using a
1.554 + * <a href="#modified-utf-8">modified UTF-8</a>
1.555 + * format.
1.556 + * The general contract of <code>readUTF</code>
1.557 + * is that it reads a representation of a Unicode
1.558 + * character string encoded in modified
1.559 + * UTF-8 format; this string of characters
1.560 + * is then returned as a <code>String</code>.
1.561 + * <p>
1.562 + * First, two bytes are read and used to
1.563 + * construct an unsigned 16-bit integer in
1.564 + * exactly the manner of the <code>readUnsignedShort</code>
1.565 + * method . This integer value is called the
1.566 + * <i>UTF length</i> and specifies the number
1.567 + * of additional bytes to be read. These bytes
1.568 + * are then converted to characters by considering
1.569 + * them in groups. The length of each group
1.570 + * is computed from the value of the first
1.571 + * byte of the group. The byte following a
1.572 + * group, if any, is the first byte of the
1.573 + * next group.
1.574 + * <p>
1.575 + * If the first byte of a group
1.576 + * matches the bit pattern <code>0xxxxxxx</code>
1.577 + * (where <code>x</code> means "may be <code>0</code>
1.578 + * or <code>1</code>"), then the group consists
1.579 + * of just that byte. The byte is zero-extended
1.580 + * to form a character.
1.581 + * <p>
1.582 + * If the first byte
1.583 + * of a group matches the bit pattern <code>110xxxxx</code>,
1.584 + * then the group consists of that byte <code>a</code>
1.585 + * and a second byte <code>b</code>. If there
1.586 + * is no byte <code>b</code> (because byte
1.587 + * <code>a</code> was the last of the bytes
1.588 + * to be read), or if byte <code>b</code> does
1.589 + * not match the bit pattern <code>10xxxxxx</code>,
1.590 + * then a <code>UTFDataFormatException</code>
1.591 + * is thrown. Otherwise, the group is converted
1.592 + * to the character:<p>
1.593 + * <pre><code>(char)(((a& 0x1F) << 6) | (b & 0x3F))
1.594 + * </code></pre>
1.595 + * If the first byte of a group
1.596 + * matches the bit pattern <code>1110xxxx</code>,
1.597 + * then the group consists of that byte <code>a</code>
1.598 + * and two more bytes <code>b</code> and <code>c</code>.
1.599 + * If there is no byte <code>c</code> (because
1.600 + * byte <code>a</code> was one of the last
1.601 + * two of the bytes to be read), or either
1.602 + * byte <code>b</code> or byte <code>c</code>
1.603 + * does not match the bit pattern <code>10xxxxxx</code>,
1.604 + * then a <code>UTFDataFormatException</code>
1.605 + * is thrown. Otherwise, the group is converted
1.606 + * to the character:<p>
1.607 + * <pre><code>
1.608 + * (char)(((a & 0x0F) << 12) | ((b & 0x3F) << 6) | (c & 0x3F))
1.609 + * </code></pre>
1.610 + * If the first byte of a group matches the
1.611 + * pattern <code>1111xxxx</code> or the pattern
1.612 + * <code>10xxxxxx</code>, then a <code>UTFDataFormatException</code>
1.613 + * is thrown.
1.614 + * <p>
1.615 + * If end of file is encountered
1.616 + * at any time during this entire process,
1.617 + * then an <code>EOFException</code> is thrown.
1.618 + * <p>
1.619 + * After every group has been converted to
1.620 + * a character by this process, the characters
1.621 + * are gathered, in the same order in which
1.622 + * their corresponding groups were read from
1.623 + * the input stream, to form a <code>String</code>,
1.624 + * which is returned.
1.625 + * <p>
1.626 + * The <code>writeUTF</code>
1.627 + * method of interface <code>DataOutput</code>
1.628 + * may be used to write data that is suitable
1.629 + * for reading by this method.
1.630 + * @return a Unicode string.
1.631 + * @exception EOFException if this stream reaches the end
1.632 + * before reading all the bytes.
1.633 + * @exception IOException if an I/O error occurs.
1.634 + * @exception UTFDataFormatException if the bytes do not represent a
1.635 + * valid modified UTF-8 encoding of a string.
1.636 + */
1.637 + String readUTF() throws IOException;
1.638 +}
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
2.2 +++ b/emul/src/main/java/java/io/DataInputStream.java Sat Nov 10 17:27:07 2012 +0100
2.3 @@ -0,0 +1,663 @@
2.4 +/*
2.5 + * Copyright (c) 1994, 2006, Oracle and/or its affiliates. All rights reserved.
2.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
2.7 + *
2.8 + * This code is free software; you can redistribute it and/or modify it
2.9 + * under the terms of the GNU General Public License version 2 only, as
2.10 + * published by the Free Software Foundation. Oracle designates this
2.11 + * particular file as subject to the "Classpath" exception as provided
2.12 + * by Oracle in the LICENSE file that accompanied this code.
2.13 + *
2.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
2.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
2.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
2.17 + * version 2 for more details (a copy is included in the LICENSE file that
2.18 + * accompanied this code).
2.19 + *
2.20 + * You should have received a copy of the GNU General Public License version
2.21 + * 2 along with this work; if not, write to the Free Software Foundation,
2.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2.23 + *
2.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2.25 + * or visit www.oracle.com if you need additional information or have any
2.26 + * questions.
2.27 + */
2.28 +
2.29 +package java.io;
2.30 +
2.31 +/**
2.32 + * A data input stream lets an application read primitive Java data
2.33 + * types from an underlying input stream in a machine-independent
2.34 + * way. An application uses a data output stream to write data that
2.35 + * can later be read by a data input stream.
2.36 + * <p>
2.37 + * DataInputStream is not necessarily safe for multithreaded access.
2.38 + * Thread safety is optional and is the responsibility of users of
2.39 + * methods in this class.
2.40 + *
2.41 + * @author Arthur van Hoff
2.42 + * @see java.io.DataOutputStream
2.43 + * @since JDK1.0
2.44 + */
2.45 +public
2.46 +class DataInputStream extends FilterInputStream implements DataInput {
2.47 +
2.48 + /**
2.49 + * Creates a DataInputStream that uses the specified
2.50 + * underlying InputStream.
2.51 + *
2.52 + * @param in the specified input stream
2.53 + */
2.54 + public DataInputStream(InputStream in) {
2.55 + super(in);
2.56 + }
2.57 +
2.58 + /**
2.59 + * working arrays initialized on demand by readUTF
2.60 + */
2.61 + private byte bytearr[] = new byte[80];
2.62 + private char chararr[] = new char[80];
2.63 +
2.64 + /**
2.65 + * Reads some number of bytes from the contained input stream and
2.66 + * stores them into the buffer array <code>b</code>. The number of
2.67 + * bytes actually read is returned as an integer. This method blocks
2.68 + * until input data is available, end of file is detected, or an
2.69 + * exception is thrown.
2.70 + *
2.71 + * <p>If <code>b</code> is null, a <code>NullPointerException</code> is
2.72 + * thrown. If the length of <code>b</code> is zero, then no bytes are
2.73 + * read and <code>0</code> is returned; otherwise, there is an attempt
2.74 + * to read at least one byte. If no byte is available because the
2.75 + * stream is at end of file, the value <code>-1</code> is returned;
2.76 + * otherwise, at least one byte is read and stored into <code>b</code>.
2.77 + *
2.78 + * <p>The first byte read is stored into element <code>b[0]</code>, the
2.79 + * next one into <code>b[1]</code>, and so on. The number of bytes read
2.80 + * is, at most, equal to the length of <code>b</code>. Let <code>k</code>
2.81 + * be the number of bytes actually read; these bytes will be stored in
2.82 + * elements <code>b[0]</code> through <code>b[k-1]</code>, leaving
2.83 + * elements <code>b[k]</code> through <code>b[b.length-1]</code>
2.84 + * unaffected.
2.85 + *
2.86 + * <p>The <code>read(b)</code> method has the same effect as:
2.87 + * <blockquote><pre>
2.88 + * read(b, 0, b.length)
2.89 + * </pre></blockquote>
2.90 + *
2.91 + * @param b the buffer into which the data is read.
2.92 + * @return the total number of bytes read into the buffer, or
2.93 + * <code>-1</code> if there is no more data because the end
2.94 + * of the stream has been reached.
2.95 + * @exception IOException if the first byte cannot be read for any reason
2.96 + * other than end of file, the stream has been closed and the underlying
2.97 + * input stream does not support reading after close, or another I/O
2.98 + * error occurs.
2.99 + * @see java.io.FilterInputStream#in
2.100 + * @see java.io.InputStream#read(byte[], int, int)
2.101 + */
2.102 + public final int read(byte b[]) throws IOException {
2.103 + return in.read(b, 0, b.length);
2.104 + }
2.105 +
2.106 + /**
2.107 + * Reads up to <code>len</code> bytes of data from the contained
2.108 + * input stream into an array of bytes. An attempt is made to read
2.109 + * as many as <code>len</code> bytes, but a smaller number may be read,
2.110 + * possibly zero. The number of bytes actually read is returned as an
2.111 + * integer.
2.112 + *
2.113 + * <p> This method blocks until input data is available, end of file is
2.114 + * detected, or an exception is thrown.
2.115 + *
2.116 + * <p> If <code>len</code> is zero, then no bytes are read and
2.117 + * <code>0</code> is returned; otherwise, there is an attempt to read at
2.118 + * least one byte. If no byte is available because the stream is at end of
2.119 + * file, the value <code>-1</code> is returned; otherwise, at least one
2.120 + * byte is read and stored into <code>b</code>.
2.121 + *
2.122 + * <p> The first byte read is stored into element <code>b[off]</code>, the
2.123 + * next one into <code>b[off+1]</code>, and so on. The number of bytes read
2.124 + * is, at most, equal to <code>len</code>. Let <i>k</i> be the number of
2.125 + * bytes actually read; these bytes will be stored in elements
2.126 + * <code>b[off]</code> through <code>b[off+</code><i>k</i><code>-1]</code>,
2.127 + * leaving elements <code>b[off+</code><i>k</i><code>]</code> through
2.128 + * <code>b[off+len-1]</code> unaffected.
2.129 + *
2.130 + * <p> In every case, elements <code>b[0]</code> through
2.131 + * <code>b[off]</code> and elements <code>b[off+len]</code> through
2.132 + * <code>b[b.length-1]</code> are unaffected.
2.133 + *
2.134 + * @param b the buffer into which the data is read.
2.135 + * @param off the start offset in the destination array <code>b</code>
2.136 + * @param len the maximum number of bytes read.
2.137 + * @return the total number of bytes read into the buffer, or
2.138 + * <code>-1</code> if there is no more data because the end
2.139 + * of the stream has been reached.
2.140 + * @exception NullPointerException If <code>b</code> is <code>null</code>.
2.141 + * @exception IndexOutOfBoundsException If <code>off</code> is negative,
2.142 + * <code>len</code> is negative, or <code>len</code> is greater than
2.143 + * <code>b.length - off</code>
2.144 + * @exception IOException if the first byte cannot be read for any reason
2.145 + * other than end of file, the stream has been closed and the underlying
2.146 + * input stream does not support reading after close, or another I/O
2.147 + * error occurs.
2.148 + * @see java.io.FilterInputStream#in
2.149 + * @see java.io.InputStream#read(byte[], int, int)
2.150 + */
2.151 + public final int read(byte b[], int off, int len) throws IOException {
2.152 + return in.read(b, off, len);
2.153 + }
2.154 +
2.155 + /**
2.156 + * See the general contract of the <code>readFully</code>
2.157 + * method of <code>DataInput</code>.
2.158 + * <p>
2.159 + * Bytes
2.160 + * for this operation are read from the contained
2.161 + * input stream.
2.162 + *
2.163 + * @param b the buffer into which the data is read.
2.164 + * @exception EOFException if this input stream reaches the end before
2.165 + * reading all the bytes.
2.166 + * @exception IOException the stream has been closed and the contained
2.167 + * input stream does not support reading after close, or
2.168 + * another I/O error occurs.
2.169 + * @see java.io.FilterInputStream#in
2.170 + */
2.171 + public final void readFully(byte b[]) throws IOException {
2.172 + readFully(b, 0, b.length);
2.173 + }
2.174 +
2.175 + /**
2.176 + * See the general contract of the <code>readFully</code>
2.177 + * method of <code>DataInput</code>.
2.178 + * <p>
2.179 + * Bytes
2.180 + * for this operation are read from the contained
2.181 + * input stream.
2.182 + *
2.183 + * @param b the buffer into which the data is read.
2.184 + * @param off the start offset of the data.
2.185 + * @param len the number of bytes to read.
2.186 + * @exception EOFException if this input stream reaches the end before
2.187 + * reading all the bytes.
2.188 + * @exception IOException the stream has been closed and the contained
2.189 + * input stream does not support reading after close, or
2.190 + * another I/O error occurs.
2.191 + * @see java.io.FilterInputStream#in
2.192 + */
2.193 + public final void readFully(byte b[], int off, int len) throws IOException {
2.194 + if (len < 0)
2.195 + throw new IndexOutOfBoundsException();
2.196 + int n = 0;
2.197 + while (n < len) {
2.198 + int count = in.read(b, off + n, len - n);
2.199 + if (count < 0)
2.200 + throw new EOFException();
2.201 + n += count;
2.202 + }
2.203 + }
2.204 +
2.205 + /**
2.206 + * See the general contract of the <code>skipBytes</code>
2.207 + * method of <code>DataInput</code>.
2.208 + * <p>
2.209 + * Bytes for this operation are read from the contained
2.210 + * input stream.
2.211 + *
2.212 + * @param n the number of bytes to be skipped.
2.213 + * @return the actual number of bytes skipped.
2.214 + * @exception IOException if the contained input stream does not support
2.215 + * seek, or the stream has been closed and
2.216 + * the contained input stream does not support
2.217 + * reading after close, or another I/O error occurs.
2.218 + */
2.219 + public final int skipBytes(int n) throws IOException {
2.220 + int total = 0;
2.221 + int cur = 0;
2.222 +
2.223 + while ((total<n) && ((cur = (int) in.skip(n-total)) > 0)) {
2.224 + total += cur;
2.225 + }
2.226 +
2.227 + return total;
2.228 + }
2.229 +
2.230 + /**
2.231 + * See the general contract of the <code>readBoolean</code>
2.232 + * method of <code>DataInput</code>.
2.233 + * <p>
2.234 + * Bytes for this operation are read from the contained
2.235 + * input stream.
2.236 + *
2.237 + * @return the <code>boolean</code> value read.
2.238 + * @exception EOFException if this input stream has reached the end.
2.239 + * @exception IOException the stream has been closed and the contained
2.240 + * input stream does not support reading after close, or
2.241 + * another I/O error occurs.
2.242 + * @see java.io.FilterInputStream#in
2.243 + */
2.244 + public final boolean readBoolean() throws IOException {
2.245 + int ch = in.read();
2.246 + if (ch < 0)
2.247 + throw new EOFException();
2.248 + return (ch != 0);
2.249 + }
2.250 +
2.251 + /**
2.252 + * See the general contract of the <code>readByte</code>
2.253 + * method of <code>DataInput</code>.
2.254 + * <p>
2.255 + * Bytes
2.256 + * for this operation are read from the contained
2.257 + * input stream.
2.258 + *
2.259 + * @return the next byte of this input stream as a signed 8-bit
2.260 + * <code>byte</code>.
2.261 + * @exception EOFException if this input stream has reached the end.
2.262 + * @exception IOException the stream has been closed and the contained
2.263 + * input stream does not support reading after close, or
2.264 + * another I/O error occurs.
2.265 + * @see java.io.FilterInputStream#in
2.266 + */
2.267 + public final byte readByte() throws IOException {
2.268 + int ch = in.read();
2.269 + if (ch < 0)
2.270 + throw new EOFException();
2.271 + return (byte)(ch);
2.272 + }
2.273 +
2.274 + /**
2.275 + * See the general contract of the <code>readUnsignedByte</code>
2.276 + * method of <code>DataInput</code>.
2.277 + * <p>
2.278 + * Bytes
2.279 + * for this operation are read from the contained
2.280 + * input stream.
2.281 + *
2.282 + * @return the next byte of this input stream, interpreted as an
2.283 + * unsigned 8-bit number.
2.284 + * @exception EOFException if this input stream has reached the end.
2.285 + * @exception IOException the stream has been closed and the contained
2.286 + * input stream does not support reading after close, or
2.287 + * another I/O error occurs.
2.288 + * @see java.io.FilterInputStream#in
2.289 + */
2.290 + public final int readUnsignedByte() throws IOException {
2.291 + int ch = in.read();
2.292 + if (ch < 0)
2.293 + throw new EOFException();
2.294 + return ch;
2.295 + }
2.296 +
2.297 + /**
2.298 + * See the general contract of the <code>readShort</code>
2.299 + * method of <code>DataInput</code>.
2.300 + * <p>
2.301 + * Bytes
2.302 + * for this operation are read from the contained
2.303 + * input stream.
2.304 + *
2.305 + * @return the next two bytes of this input stream, interpreted as a
2.306 + * signed 16-bit number.
2.307 + * @exception EOFException if this input stream reaches the end before
2.308 + * reading two bytes.
2.309 + * @exception IOException the stream has been closed and the contained
2.310 + * input stream does not support reading after close, or
2.311 + * another I/O error occurs.
2.312 + * @see java.io.FilterInputStream#in
2.313 + */
2.314 + public final short readShort() throws IOException {
2.315 + int ch1 = in.read();
2.316 + int ch2 = in.read();
2.317 + if ((ch1 | ch2) < 0)
2.318 + throw new EOFException();
2.319 + return (short)((ch1 << 8) + (ch2 << 0));
2.320 + }
2.321 +
2.322 + /**
2.323 + * See the general contract of the <code>readUnsignedShort</code>
2.324 + * method of <code>DataInput</code>.
2.325 + * <p>
2.326 + * Bytes
2.327 + * for this operation are read from the contained
2.328 + * input stream.
2.329 + *
2.330 + * @return the next two bytes of this input stream, interpreted as an
2.331 + * unsigned 16-bit integer.
2.332 + * @exception EOFException if this input stream reaches the end before
2.333 + * reading two bytes.
2.334 + * @exception IOException the stream has been closed and the contained
2.335 + * input stream does not support reading after close, or
2.336 + * another I/O error occurs.
2.337 + * @see java.io.FilterInputStream#in
2.338 + */
2.339 + public final int readUnsignedShort() throws IOException {
2.340 + int ch1 = in.read();
2.341 + int ch2 = in.read();
2.342 + if ((ch1 | ch2) < 0)
2.343 + throw new EOFException();
2.344 + return (ch1 << 8) + (ch2 << 0);
2.345 + }
2.346 +
2.347 + /**
2.348 + * See the general contract of the <code>readChar</code>
2.349 + * method of <code>DataInput</code>.
2.350 + * <p>
2.351 + * Bytes
2.352 + * for this operation are read from the contained
2.353 + * input stream.
2.354 + *
2.355 + * @return the next two bytes of this input stream, interpreted as a
2.356 + * <code>char</code>.
2.357 + * @exception EOFException if this input stream reaches the end before
2.358 + * reading two bytes.
2.359 + * @exception IOException the stream has been closed and the contained
2.360 + * input stream does not support reading after close, or
2.361 + * another I/O error occurs.
2.362 + * @see java.io.FilterInputStream#in
2.363 + */
2.364 + public final char readChar() throws IOException {
2.365 + int ch1 = in.read();
2.366 + int ch2 = in.read();
2.367 + if ((ch1 | ch2) < 0)
2.368 + throw new EOFException();
2.369 + return (char)((ch1 << 8) + (ch2 << 0));
2.370 + }
2.371 +
2.372 + /**
2.373 + * See the general contract of the <code>readInt</code>
2.374 + * method of <code>DataInput</code>.
2.375 + * <p>
2.376 + * Bytes
2.377 + * for this operation are read from the contained
2.378 + * input stream.
2.379 + *
2.380 + * @return the next four bytes of this input stream, interpreted as an
2.381 + * <code>int</code>.
2.382 + * @exception EOFException if this input stream reaches the end before
2.383 + * reading four bytes.
2.384 + * @exception IOException the stream has been closed and the contained
2.385 + * input stream does not support reading after close, or
2.386 + * another I/O error occurs.
2.387 + * @see java.io.FilterInputStream#in
2.388 + */
2.389 + public final int readInt() throws IOException {
2.390 + int ch1 = in.read();
2.391 + int ch2 = in.read();
2.392 + int ch3 = in.read();
2.393 + int ch4 = in.read();
2.394 + if ((ch1 | ch2 | ch3 | ch4) < 0)
2.395 + throw new EOFException();
2.396 + return ((ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0));
2.397 + }
2.398 +
2.399 + private byte readBuffer[] = new byte[8];
2.400 +
2.401 + /**
2.402 + * See the general contract of the <code>readLong</code>
2.403 + * method of <code>DataInput</code>.
2.404 + * <p>
2.405 + * Bytes
2.406 + * for this operation are read from the contained
2.407 + * input stream.
2.408 + *
2.409 + * @return the next eight bytes of this input stream, interpreted as a
2.410 + * <code>long</code>.
2.411 + * @exception EOFException if this input stream reaches the end before
2.412 + * reading eight bytes.
2.413 + * @exception IOException the stream has been closed and the contained
2.414 + * input stream does not support reading after close, or
2.415 + * another I/O error occurs.
2.416 + * @see java.io.FilterInputStream#in
2.417 + */
2.418 + public final long readLong() throws IOException {
2.419 + readFully(readBuffer, 0, 8);
2.420 + return (((long)readBuffer[0] << 56) +
2.421 + ((long)(readBuffer[1] & 255) << 48) +
2.422 + ((long)(readBuffer[2] & 255) << 40) +
2.423 + ((long)(readBuffer[3] & 255) << 32) +
2.424 + ((long)(readBuffer[4] & 255) << 24) +
2.425 + ((readBuffer[5] & 255) << 16) +
2.426 + ((readBuffer[6] & 255) << 8) +
2.427 + ((readBuffer[7] & 255) << 0));
2.428 + }
2.429 +
2.430 + /**
2.431 + * See the general contract of the <code>readFloat</code>
2.432 + * method of <code>DataInput</code>.
2.433 + * <p>
2.434 + * Bytes
2.435 + * for this operation are read from the contained
2.436 + * input stream.
2.437 + *
2.438 + * @return the next four bytes of this input stream, interpreted as a
2.439 + * <code>float</code>.
2.440 + * @exception EOFException if this input stream reaches the end before
2.441 + * reading four bytes.
2.442 + * @exception IOException the stream has been closed and the contained
2.443 + * input stream does not support reading after close, or
2.444 + * another I/O error occurs.
2.445 + * @see java.io.DataInputStream#readInt()
2.446 + * @see java.lang.Float#intBitsToFloat(int)
2.447 + */
2.448 + public final float readFloat() throws IOException {
2.449 + return Float.intBitsToFloat(readInt());
2.450 + }
2.451 +
2.452 + /**
2.453 + * See the general contract of the <code>readDouble</code>
2.454 + * method of <code>DataInput</code>.
2.455 + * <p>
2.456 + * Bytes
2.457 + * for this operation are read from the contained
2.458 + * input stream.
2.459 + *
2.460 + * @return the next eight bytes of this input stream, interpreted as a
2.461 + * <code>double</code>.
2.462 + * @exception EOFException if this input stream reaches the end before
2.463 + * reading eight bytes.
2.464 + * @exception IOException the stream has been closed and the contained
2.465 + * input stream does not support reading after close, or
2.466 + * another I/O error occurs.
2.467 + * @see java.io.DataInputStream#readLong()
2.468 + * @see java.lang.Double#longBitsToDouble(long)
2.469 + */
2.470 + public final double readDouble() throws IOException {
2.471 + return Double.longBitsToDouble(readLong());
2.472 + }
2.473 +
2.474 + private char lineBuffer[];
2.475 +
2.476 + /**
2.477 + * See the general contract of the <code>readLine</code>
2.478 + * method of <code>DataInput</code>.
2.479 + * <p>
2.480 + * Bytes
2.481 + * for this operation are read from the contained
2.482 + * input stream.
2.483 + *
2.484 + * @deprecated This method does not properly convert bytes to characters.
2.485 + * As of JDK 1.1, the preferred way to read lines of text is via the
2.486 + * <code>BufferedReader.readLine()</code> method. Programs that use the
2.487 + * <code>DataInputStream</code> class to read lines can be converted to use
2.488 + * the <code>BufferedReader</code> class by replacing code of the form:
2.489 + * <blockquote><pre>
2.490 + * DataInputStream d = new DataInputStream(in);
2.491 + * </pre></blockquote>
2.492 + * with:
2.493 + * <blockquote><pre>
2.494 + * BufferedReader d
2.495 + * = new BufferedReader(new InputStreamReader(in));
2.496 + * </pre></blockquote>
2.497 + *
2.498 + * @return the next line of text from this input stream.
2.499 + * @exception IOException if an I/O error occurs.
2.500 + * @see java.io.BufferedReader#readLine()
2.501 + * @see java.io.FilterInputStream#in
2.502 + */
2.503 + @Deprecated
2.504 + public final String readLine() throws IOException {
2.505 + char buf[] = lineBuffer;
2.506 +
2.507 + if (buf == null) {
2.508 + buf = lineBuffer = new char[128];
2.509 + }
2.510 +
2.511 + int room = buf.length;
2.512 + int offset = 0;
2.513 + int c;
2.514 +
2.515 +loop: while (true) {
2.516 + switch (c = in.read()) {
2.517 + case -1:
2.518 + case '\n':
2.519 + break loop;
2.520 +
2.521 + case '\r':
2.522 + int c2 = in.read();
2.523 + if ((c2 != '\n') && (c2 != -1)) {
2.524 + if (!(in instanceof PushbackInputStream)) {
2.525 + this.in = new PushbackInputStream(in);
2.526 + }
2.527 + ((PushbackInputStream)in).unread(c2);
2.528 + }
2.529 + break loop;
2.530 +
2.531 + default:
2.532 + if (--room < 0) {
2.533 + buf = new char[offset + 128];
2.534 + room = buf.length - offset - 1;
2.535 + System.arraycopy(lineBuffer, 0, buf, 0, offset);
2.536 + lineBuffer = buf;
2.537 + }
2.538 + buf[offset++] = (char) c;
2.539 + break;
2.540 + }
2.541 + }
2.542 + if ((c == -1) && (offset == 0)) {
2.543 + return null;
2.544 + }
2.545 + return String.copyValueOf(buf, 0, offset);
2.546 + }
2.547 +
2.548 + /**
2.549 + * See the general contract of the <code>readUTF</code>
2.550 + * method of <code>DataInput</code>.
2.551 + * <p>
2.552 + * Bytes
2.553 + * for this operation are read from the contained
2.554 + * input stream.
2.555 + *
2.556 + * @return a Unicode string.
2.557 + * @exception EOFException if this input stream reaches the end before
2.558 + * reading all the bytes.
2.559 + * @exception IOException the stream has been closed and the contained
2.560 + * input stream does not support reading after close, or
2.561 + * another I/O error occurs.
2.562 + * @exception UTFDataFormatException if the bytes do not represent a valid
2.563 + * modified UTF-8 encoding of a string.
2.564 + * @see java.io.DataInputStream#readUTF(java.io.DataInput)
2.565 + */
2.566 + public final String readUTF() throws IOException {
2.567 + return readUTF(this);
2.568 + }
2.569 +
2.570 + /**
2.571 + * Reads from the
2.572 + * stream <code>in</code> a representation
2.573 + * of a Unicode character string encoded in
2.574 + * <a href="DataInput.html#modified-utf-8">modified UTF-8</a> format;
2.575 + * this string of characters is then returned as a <code>String</code>.
2.576 + * The details of the modified UTF-8 representation
2.577 + * are exactly the same as for the <code>readUTF</code>
2.578 + * method of <code>DataInput</code>.
2.579 + *
2.580 + * @param in a data input stream.
2.581 + * @return a Unicode string.
2.582 + * @exception EOFException if the input stream reaches the end
2.583 + * before all the bytes.
2.584 + * @exception IOException the stream has been closed and the contained
2.585 + * input stream does not support reading after close, or
2.586 + * another I/O error occurs.
2.587 + * @exception UTFDataFormatException if the bytes do not represent a
2.588 + * valid modified UTF-8 encoding of a Unicode string.
2.589 + * @see java.io.DataInputStream#readUnsignedShort()
2.590 + */
2.591 + public final static String readUTF(DataInput in) throws IOException {
2.592 + int utflen = in.readUnsignedShort();
2.593 + byte[] bytearr = null;
2.594 + char[] chararr = null;
2.595 + if (in instanceof DataInputStream) {
2.596 + DataInputStream dis = (DataInputStream)in;
2.597 + if (dis.bytearr.length < utflen){
2.598 + dis.bytearr = new byte[utflen*2];
2.599 + dis.chararr = new char[utflen*2];
2.600 + }
2.601 + chararr = dis.chararr;
2.602 + bytearr = dis.bytearr;
2.603 + } else {
2.604 + bytearr = new byte[utflen];
2.605 + chararr = new char[utflen];
2.606 + }
2.607 +
2.608 + int c, char2, char3;
2.609 + int count = 0;
2.610 + int chararr_count=0;
2.611 +
2.612 + in.readFully(bytearr, 0, utflen);
2.613 +
2.614 + while (count < utflen) {
2.615 + c = (int) bytearr[count] & 0xff;
2.616 + if (c > 127) break;
2.617 + count++;
2.618 + chararr[chararr_count++]=(char)c;
2.619 + }
2.620 +
2.621 + while (count < utflen) {
2.622 + c = (int) bytearr[count] & 0xff;
2.623 + switch (c >> 4) {
2.624 + case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
2.625 + /* 0xxxxxxx*/
2.626 + count++;
2.627 + chararr[chararr_count++]=(char)c;
2.628 + break;
2.629 + case 12: case 13:
2.630 + /* 110x xxxx 10xx xxxx*/
2.631 + count += 2;
2.632 + if (count > utflen)
2.633 + throw new UTFDataFormatException(
2.634 + "malformed input: partial character at end");
2.635 + char2 = (int) bytearr[count-1];
2.636 + if ((char2 & 0xC0) != 0x80)
2.637 + throw new UTFDataFormatException(
2.638 + "malformed input around byte " + count);
2.639 + chararr[chararr_count++]=(char)(((c & 0x1F) << 6) |
2.640 + (char2 & 0x3F));
2.641 + break;
2.642 + case 14:
2.643 + /* 1110 xxxx 10xx xxxx 10xx xxxx */
2.644 + count += 3;
2.645 + if (count > utflen)
2.646 + throw new UTFDataFormatException(
2.647 + "malformed input: partial character at end");
2.648 + char2 = (int) bytearr[count-2];
2.649 + char3 = (int) bytearr[count-1];
2.650 + if (((char2 & 0xC0) != 0x80) || ((char3 & 0xC0) != 0x80))
2.651 + throw new UTFDataFormatException(
2.652 + "malformed input around byte " + (count-1));
2.653 + chararr[chararr_count++]=(char)(((c & 0x0F) << 12) |
2.654 + ((char2 & 0x3F) << 6) |
2.655 + ((char3 & 0x3F) << 0));
2.656 + break;
2.657 + default:
2.658 + /* 10xx xxxx, 1111 xxxx */
2.659 + throw new UTFDataFormatException(
2.660 + "malformed input around byte " + count);
2.661 + }
2.662 + }
2.663 + // The number of chars produced may be less than utflen
2.664 + return new String(chararr, 0, chararr_count);
2.665 + }
2.666 +}
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
3.2 +++ b/emul/src/main/java/java/io/FilterInputStream.java Sat Nov 10 17:27:07 2012 +0100
3.3 @@ -0,0 +1,245 @@
3.4 +/*
3.5 + * Copyright (c) 1994, 2010, Oracle and/or its affiliates. All rights reserved.
3.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3.7 + *
3.8 + * This code is free software; you can redistribute it and/or modify it
3.9 + * under the terms of the GNU General Public License version 2 only, as
3.10 + * published by the Free Software Foundation. Oracle designates this
3.11 + * particular file as subject to the "Classpath" exception as provided
3.12 + * by Oracle in the LICENSE file that accompanied this code.
3.13 + *
3.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
3.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
3.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
3.17 + * version 2 for more details (a copy is included in the LICENSE file that
3.18 + * accompanied this code).
3.19 + *
3.20 + * You should have received a copy of the GNU General Public License version
3.21 + * 2 along with this work; if not, write to the Free Software Foundation,
3.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
3.23 + *
3.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
3.25 + * or visit www.oracle.com if you need additional information or have any
3.26 + * questions.
3.27 + */
3.28 +
3.29 +package java.io;
3.30 +
3.31 +/**
3.32 + * A <code>FilterInputStream</code> contains
3.33 + * some other input stream, which it uses as
3.34 + * its basic source of data, possibly transforming
3.35 + * the data along the way or providing additional
3.36 + * functionality. The class <code>FilterInputStream</code>
3.37 + * itself simply overrides all methods of
3.38 + * <code>InputStream</code> with versions that
3.39 + * pass all requests to the contained input
3.40 + * stream. Subclasses of <code>FilterInputStream</code>
3.41 + * may further override some of these methods
3.42 + * and may also provide additional methods
3.43 + * and fields.
3.44 + *
3.45 + * @author Jonathan Payne
3.46 + * @since JDK1.0
3.47 + */
3.48 +public
3.49 +class FilterInputStream extends InputStream {
3.50 + /**
3.51 + * The input stream to be filtered.
3.52 + */
3.53 + protected volatile InputStream in;
3.54 +
3.55 + /**
3.56 + * Creates a <code>FilterInputStream</code>
3.57 + * by assigning the argument <code>in</code>
3.58 + * to the field <code>this.in</code> so as
3.59 + * to remember it for later use.
3.60 + *
3.61 + * @param in the underlying input stream, or <code>null</code> if
3.62 + * this instance is to be created without an underlying stream.
3.63 + */
3.64 + protected FilterInputStream(InputStream in) {
3.65 + this.in = in;
3.66 + }
3.67 +
3.68 + /**
3.69 + * Reads the next byte of data from this input stream. The value
3.70 + * byte is returned as an <code>int</code> in the range
3.71 + * <code>0</code> to <code>255</code>. If no byte is available
3.72 + * because the end of the stream has been reached, the value
3.73 + * <code>-1</code> is returned. This method blocks until input data
3.74 + * is available, the end of the stream is detected, or an exception
3.75 + * is thrown.
3.76 + * <p>
3.77 + * This method
3.78 + * simply performs <code>in.read()</code> and returns the result.
3.79 + *
3.80 + * @return the next byte of data, or <code>-1</code> if the end of the
3.81 + * stream is reached.
3.82 + * @exception IOException if an I/O error occurs.
3.83 + * @see java.io.FilterInputStream#in
3.84 + */
3.85 + public int read() throws IOException {
3.86 + return in.read();
3.87 + }
3.88 +
3.89 + /**
3.90 + * Reads up to <code>byte.length</code> bytes of data from this
3.91 + * input stream into an array of bytes. This method blocks until some
3.92 + * input is available.
3.93 + * <p>
3.94 + * This method simply performs the call
3.95 + * <code>read(b, 0, b.length)</code> and returns
3.96 + * the result. It is important that it does
3.97 + * <i>not</i> do <code>in.read(b)</code> instead;
3.98 + * certain subclasses of <code>FilterInputStream</code>
3.99 + * depend on the implementation strategy actually
3.100 + * used.
3.101 + *
3.102 + * @param b the buffer into which the data is read.
3.103 + * @return the total number of bytes read into the buffer, or
3.104 + * <code>-1</code> if there is no more data because the end of
3.105 + * the stream has been reached.
3.106 + * @exception IOException if an I/O error occurs.
3.107 + * @see java.io.FilterInputStream#read(byte[], int, int)
3.108 + */
3.109 + public int read(byte b[]) throws IOException {
3.110 + return read(b, 0, b.length);
3.111 + }
3.112 +
3.113 + /**
3.114 + * Reads up to <code>len</code> bytes of data from this input stream
3.115 + * into an array of bytes. If <code>len</code> is not zero, the method
3.116 + * blocks until some input is available; otherwise, no
3.117 + * bytes are read and <code>0</code> is returned.
3.118 + * <p>
3.119 + * This method simply performs <code>in.read(b, off, len)</code>
3.120 + * and returns the result.
3.121 + *
3.122 + * @param b the buffer into which the data is read.
3.123 + * @param off the start offset in the destination array <code>b</code>
3.124 + * @param len the maximum number of bytes read.
3.125 + * @return the total number of bytes read into the buffer, or
3.126 + * <code>-1</code> if there is no more data because the end of
3.127 + * the stream has been reached.
3.128 + * @exception NullPointerException If <code>b</code> is <code>null</code>.
3.129 + * @exception IndexOutOfBoundsException If <code>off</code> is negative,
3.130 + * <code>len</code> is negative, or <code>len</code> is greater than
3.131 + * <code>b.length - off</code>
3.132 + * @exception IOException if an I/O error occurs.
3.133 + * @see java.io.FilterInputStream#in
3.134 + */
3.135 + public int read(byte b[], int off, int len) throws IOException {
3.136 + return in.read(b, off, len);
3.137 + }
3.138 +
3.139 + /**
3.140 + * Skips over and discards <code>n</code> bytes of data from the
3.141 + * input stream. The <code>skip</code> method may, for a variety of
3.142 + * reasons, end up skipping over some smaller number of bytes,
3.143 + * possibly <code>0</code>. The actual number of bytes skipped is
3.144 + * returned.
3.145 + * <p>
3.146 + * This method simply performs <code>in.skip(n)</code>.
3.147 + *
3.148 + * @param n the number of bytes to be skipped.
3.149 + * @return the actual number of bytes skipped.
3.150 + * @exception IOException if the stream does not support seek,
3.151 + * or if some other I/O error occurs.
3.152 + */
3.153 + public long skip(long n) throws IOException {
3.154 + return in.skip(n);
3.155 + }
3.156 +
3.157 + /**
3.158 + * Returns an estimate of the number of bytes that can be read (or
3.159 + * skipped over) from this input stream without blocking by the next
3.160 + * caller of a method for this input stream. The next caller might be
3.161 + * the same thread or another thread. A single read or skip of this
3.162 + * many bytes will not block, but may read or skip fewer bytes.
3.163 + * <p>
3.164 + * This method returns the result of {@link #in in}.available().
3.165 + *
3.166 + * @return an estimate of the number of bytes that can be read (or skipped
3.167 + * over) from this input stream without blocking.
3.168 + * @exception IOException if an I/O error occurs.
3.169 + */
3.170 + public int available() throws IOException {
3.171 + return in.available();
3.172 + }
3.173 +
3.174 + /**
3.175 + * Closes this input stream and releases any system resources
3.176 + * associated with the stream.
3.177 + * This
3.178 + * method simply performs <code>in.close()</code>.
3.179 + *
3.180 + * @exception IOException if an I/O error occurs.
3.181 + * @see java.io.FilterInputStream#in
3.182 + */
3.183 + public void close() throws IOException {
3.184 + in.close();
3.185 + }
3.186 +
3.187 + /**
3.188 + * Marks the current position in this input stream. A subsequent
3.189 + * call to the <code>reset</code> method repositions this stream at
3.190 + * the last marked position so that subsequent reads re-read the same bytes.
3.191 + * <p>
3.192 + * The <code>readlimit</code> argument tells this input stream to
3.193 + * allow that many bytes to be read before the mark position gets
3.194 + * invalidated.
3.195 + * <p>
3.196 + * This method simply performs <code>in.mark(readlimit)</code>.
3.197 + *
3.198 + * @param readlimit the maximum limit of bytes that can be read before
3.199 + * the mark position becomes invalid.
3.200 + * @see java.io.FilterInputStream#in
3.201 + * @see java.io.FilterInputStream#reset()
3.202 + */
3.203 + public synchronized void mark(int readlimit) {
3.204 + in.mark(readlimit);
3.205 + }
3.206 +
3.207 + /**
3.208 + * Repositions this stream to the position at the time the
3.209 + * <code>mark</code> method was last called on this input stream.
3.210 + * <p>
3.211 + * This method
3.212 + * simply performs <code>in.reset()</code>.
3.213 + * <p>
3.214 + * Stream marks are intended to be used in
3.215 + * situations where you need to read ahead a little to see what's in
3.216 + * the stream. Often this is most easily done by invoking some
3.217 + * general parser. If the stream is of the type handled by the
3.218 + * parse, it just chugs along happily. If the stream is not of
3.219 + * that type, the parser should toss an exception when it fails.
3.220 + * If this happens within readlimit bytes, it allows the outer
3.221 + * code to reset the stream and try another parser.
3.222 + *
3.223 + * @exception IOException if the stream has not been marked or if the
3.224 + * mark has been invalidated.
3.225 + * @see java.io.FilterInputStream#in
3.226 + * @see java.io.FilterInputStream#mark(int)
3.227 + */
3.228 + public synchronized void reset() throws IOException {
3.229 + in.reset();
3.230 + }
3.231 +
3.232 + /**
3.233 + * Tests if this input stream supports the <code>mark</code>
3.234 + * and <code>reset</code> methods.
3.235 + * This method
3.236 + * simply performs <code>in.markSupported()</code>.
3.237 + *
3.238 + * @return <code>true</code> if this stream type supports the
3.239 + * <code>mark</code> and <code>reset</code> method;
3.240 + * <code>false</code> otherwise.
3.241 + * @see java.io.FilterInputStream#in
3.242 + * @see java.io.InputStream#mark(int)
3.243 + * @see java.io.InputStream#reset()
3.244 + */
3.245 + public boolean markSupported() {
3.246 + return in.markSupported();
3.247 + }
3.248 +}