Merging in DataInputStream & co.
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:54:30 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:54:30 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/EOFException.java Sat Nov 10 17:54:30 2012 +0100
3.3 @@ -0,0 +1,65 @@
3.4 +/*
3.5 + * Copyright (c) 1995, 2008, 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 + * Signals that an end of file or end of stream has been reached
3.33 + * unexpectedly during input.
3.34 + * <p>
3.35 + * This exception is mainly used by data input streams to signal end of
3.36 + * stream. Note that many other input operations return a special value on
3.37 + * end of stream rather than throwing an exception.
3.38 + * <p>
3.39 + *
3.40 + * @author Frank Yellin
3.41 + * @see java.io.DataInputStream
3.42 + * @see java.io.IOException
3.43 + * @since JDK1.0
3.44 + */
3.45 +public
3.46 +class EOFException extends IOException {
3.47 + private static final long serialVersionUID = 6433858223774886977L;
3.48 +
3.49 + /**
3.50 + * Constructs an <code>EOFException</code> with <code>null</code>
3.51 + * as its error detail message.
3.52 + */
3.53 + public EOFException() {
3.54 + super();
3.55 + }
3.56 +
3.57 + /**
3.58 + * Constructs an <code>EOFException</code> with the specified detail
3.59 + * message. The string <code>s</code> may later be retrieved by the
3.60 + * <code>{@link java.lang.Throwable#getMessage}</code> method of class
3.61 + * <code>java.lang.Throwable</code>.
3.62 + *
3.63 + * @param s the detail message.
3.64 + */
3.65 + public EOFException(String s) {
3.66 + super(s);
3.67 + }
3.68 +}
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
4.2 +++ b/emul/src/main/java/java/io/FilterInputStream.java Sat Nov 10 17:54:30 2012 +0100
4.3 @@ -0,0 +1,245 @@
4.4 +/*
4.5 + * Copyright (c) 1994, 2010, Oracle and/or its affiliates. All rights reserved.
4.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4.7 + *
4.8 + * This code is free software; you can redistribute it and/or modify it
4.9 + * under the terms of the GNU General Public License version 2 only, as
4.10 + * published by the Free Software Foundation. Oracle designates this
4.11 + * particular file as subject to the "Classpath" exception as provided
4.12 + * by Oracle in the LICENSE file that accompanied this code.
4.13 + *
4.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
4.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
4.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
4.17 + * version 2 for more details (a copy is included in the LICENSE file that
4.18 + * accompanied this code).
4.19 + *
4.20 + * You should have received a copy of the GNU General Public License version
4.21 + * 2 along with this work; if not, write to the Free Software Foundation,
4.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
4.23 + *
4.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
4.25 + * or visit www.oracle.com if you need additional information or have any
4.26 + * questions.
4.27 + */
4.28 +
4.29 +package java.io;
4.30 +
4.31 +/**
4.32 + * A <code>FilterInputStream</code> contains
4.33 + * some other input stream, which it uses as
4.34 + * its basic source of data, possibly transforming
4.35 + * the data along the way or providing additional
4.36 + * functionality. The class <code>FilterInputStream</code>
4.37 + * itself simply overrides all methods of
4.38 + * <code>InputStream</code> with versions that
4.39 + * pass all requests to the contained input
4.40 + * stream. Subclasses of <code>FilterInputStream</code>
4.41 + * may further override some of these methods
4.42 + * and may also provide additional methods
4.43 + * and fields.
4.44 + *
4.45 + * @author Jonathan Payne
4.46 + * @since JDK1.0
4.47 + */
4.48 +public
4.49 +class FilterInputStream extends InputStream {
4.50 + /**
4.51 + * The input stream to be filtered.
4.52 + */
4.53 + protected volatile InputStream in;
4.54 +
4.55 + /**
4.56 + * Creates a <code>FilterInputStream</code>
4.57 + * by assigning the argument <code>in</code>
4.58 + * to the field <code>this.in</code> so as
4.59 + * to remember it for later use.
4.60 + *
4.61 + * @param in the underlying input stream, or <code>null</code> if
4.62 + * this instance is to be created without an underlying stream.
4.63 + */
4.64 + protected FilterInputStream(InputStream in) {
4.65 + this.in = in;
4.66 + }
4.67 +
4.68 + /**
4.69 + * Reads the next byte of data from this input stream. The value
4.70 + * byte is returned as an <code>int</code> in the range
4.71 + * <code>0</code> to <code>255</code>. If no byte is available
4.72 + * because the end of the stream has been reached, the value
4.73 + * <code>-1</code> is returned. This method blocks until input data
4.74 + * is available, the end of the stream is detected, or an exception
4.75 + * is thrown.
4.76 + * <p>
4.77 + * This method
4.78 + * simply performs <code>in.read()</code> and returns the result.
4.79 + *
4.80 + * @return the next byte of data, or <code>-1</code> if the end of the
4.81 + * stream is reached.
4.82 + * @exception IOException if an I/O error occurs.
4.83 + * @see java.io.FilterInputStream#in
4.84 + */
4.85 + public int read() throws IOException {
4.86 + return in.read();
4.87 + }
4.88 +
4.89 + /**
4.90 + * Reads up to <code>byte.length</code> bytes of data from this
4.91 + * input stream into an array of bytes. This method blocks until some
4.92 + * input is available.
4.93 + * <p>
4.94 + * This method simply performs the call
4.95 + * <code>read(b, 0, b.length)</code> and returns
4.96 + * the result. It is important that it does
4.97 + * <i>not</i> do <code>in.read(b)</code> instead;
4.98 + * certain subclasses of <code>FilterInputStream</code>
4.99 + * depend on the implementation strategy actually
4.100 + * used.
4.101 + *
4.102 + * @param b the buffer into which the data is read.
4.103 + * @return the total number of bytes read into the buffer, or
4.104 + * <code>-1</code> if there is no more data because the end of
4.105 + * the stream has been reached.
4.106 + * @exception IOException if an I/O error occurs.
4.107 + * @see java.io.FilterInputStream#read(byte[], int, int)
4.108 + */
4.109 + public int read(byte b[]) throws IOException {
4.110 + return read(b, 0, b.length);
4.111 + }
4.112 +
4.113 + /**
4.114 + * Reads up to <code>len</code> bytes of data from this input stream
4.115 + * into an array of bytes. If <code>len</code> is not zero, the method
4.116 + * blocks until some input is available; otherwise, no
4.117 + * bytes are read and <code>0</code> is returned.
4.118 + * <p>
4.119 + * This method simply performs <code>in.read(b, off, len)</code>
4.120 + * and returns the result.
4.121 + *
4.122 + * @param b the buffer into which the data is read.
4.123 + * @param off the start offset in the destination array <code>b</code>
4.124 + * @param len the maximum number of bytes read.
4.125 + * @return the total number of bytes read into the buffer, or
4.126 + * <code>-1</code> if there is no more data because the end of
4.127 + * the stream has been reached.
4.128 + * @exception NullPointerException If <code>b</code> is <code>null</code>.
4.129 + * @exception IndexOutOfBoundsException If <code>off</code> is negative,
4.130 + * <code>len</code> is negative, or <code>len</code> is greater than
4.131 + * <code>b.length - off</code>
4.132 + * @exception IOException if an I/O error occurs.
4.133 + * @see java.io.FilterInputStream#in
4.134 + */
4.135 + public int read(byte b[], int off, int len) throws IOException {
4.136 + return in.read(b, off, len);
4.137 + }
4.138 +
4.139 + /**
4.140 + * Skips over and discards <code>n</code> bytes of data from the
4.141 + * input stream. The <code>skip</code> method may, for a variety of
4.142 + * reasons, end up skipping over some smaller number of bytes,
4.143 + * possibly <code>0</code>. The actual number of bytes skipped is
4.144 + * returned.
4.145 + * <p>
4.146 + * This method simply performs <code>in.skip(n)</code>.
4.147 + *
4.148 + * @param n the number of bytes to be skipped.
4.149 + * @return the actual number of bytes skipped.
4.150 + * @exception IOException if the stream does not support seek,
4.151 + * or if some other I/O error occurs.
4.152 + */
4.153 + public long skip(long n) throws IOException {
4.154 + return in.skip(n);
4.155 + }
4.156 +
4.157 + /**
4.158 + * Returns an estimate of the number of bytes that can be read (or
4.159 + * skipped over) from this input stream without blocking by the next
4.160 + * caller of a method for this input stream. The next caller might be
4.161 + * the same thread or another thread. A single read or skip of this
4.162 + * many bytes will not block, but may read or skip fewer bytes.
4.163 + * <p>
4.164 + * This method returns the result of {@link #in in}.available().
4.165 + *
4.166 + * @return an estimate of the number of bytes that can be read (or skipped
4.167 + * over) from this input stream without blocking.
4.168 + * @exception IOException if an I/O error occurs.
4.169 + */
4.170 + public int available() throws IOException {
4.171 + return in.available();
4.172 + }
4.173 +
4.174 + /**
4.175 + * Closes this input stream and releases any system resources
4.176 + * associated with the stream.
4.177 + * This
4.178 + * method simply performs <code>in.close()</code>.
4.179 + *
4.180 + * @exception IOException if an I/O error occurs.
4.181 + * @see java.io.FilterInputStream#in
4.182 + */
4.183 + public void close() throws IOException {
4.184 + in.close();
4.185 + }
4.186 +
4.187 + /**
4.188 + * Marks the current position in this input stream. A subsequent
4.189 + * call to the <code>reset</code> method repositions this stream at
4.190 + * the last marked position so that subsequent reads re-read the same bytes.
4.191 + * <p>
4.192 + * The <code>readlimit</code> argument tells this input stream to
4.193 + * allow that many bytes to be read before the mark position gets
4.194 + * invalidated.
4.195 + * <p>
4.196 + * This method simply performs <code>in.mark(readlimit)</code>.
4.197 + *
4.198 + * @param readlimit the maximum limit of bytes that can be read before
4.199 + * the mark position becomes invalid.
4.200 + * @see java.io.FilterInputStream#in
4.201 + * @see java.io.FilterInputStream#reset()
4.202 + */
4.203 + public synchronized void mark(int readlimit) {
4.204 + in.mark(readlimit);
4.205 + }
4.206 +
4.207 + /**
4.208 + * Repositions this stream to the position at the time the
4.209 + * <code>mark</code> method was last called on this input stream.
4.210 + * <p>
4.211 + * This method
4.212 + * simply performs <code>in.reset()</code>.
4.213 + * <p>
4.214 + * Stream marks are intended to be used in
4.215 + * situations where you need to read ahead a little to see what's in
4.216 + * the stream. Often this is most easily done by invoking some
4.217 + * general parser. If the stream is of the type handled by the
4.218 + * parse, it just chugs along happily. If the stream is not of
4.219 + * that type, the parser should toss an exception when it fails.
4.220 + * If this happens within readlimit bytes, it allows the outer
4.221 + * code to reset the stream and try another parser.
4.222 + *
4.223 + * @exception IOException if the stream has not been marked or if the
4.224 + * mark has been invalidated.
4.225 + * @see java.io.FilterInputStream#in
4.226 + * @see java.io.FilterInputStream#mark(int)
4.227 + */
4.228 + public synchronized void reset() throws IOException {
4.229 + in.reset();
4.230 + }
4.231 +
4.232 + /**
4.233 + * Tests if this input stream supports the <code>mark</code>
4.234 + * and <code>reset</code> methods.
4.235 + * This method
4.236 + * simply performs <code>in.markSupported()</code>.
4.237 + *
4.238 + * @return <code>true</code> if this stream type supports the
4.239 + * <code>mark</code> and <code>reset</code> method;
4.240 + * <code>false</code> otherwise.
4.241 + * @see java.io.FilterInputStream#in
4.242 + * @see java.io.InputStream#mark(int)
4.243 + * @see java.io.InputStream#reset()
4.244 + */
4.245 + public boolean markSupported() {
4.246 + return in.markSupported();
4.247 + }
4.248 +}
5.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
5.2 +++ b/emul/src/main/java/java/io/PushbackInputStream.java Sat Nov 10 17:54:30 2012 +0100
5.3 @@ -0,0 +1,383 @@
5.4 +/*
5.5 + * Copyright (c) 1994, 2011, Oracle and/or its affiliates. All rights reserved.
5.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5.7 + *
5.8 + * This code is free software; you can redistribute it and/or modify it
5.9 + * under the terms of the GNU General Public License version 2 only, as
5.10 + * published by the Free Software Foundation. Oracle designates this
5.11 + * particular file as subject to the "Classpath" exception as provided
5.12 + * by Oracle in the LICENSE file that accompanied this code.
5.13 + *
5.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
5.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
5.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
5.17 + * version 2 for more details (a copy is included in the LICENSE file that
5.18 + * accompanied this code).
5.19 + *
5.20 + * You should have received a copy of the GNU General Public License version
5.21 + * 2 along with this work; if not, write to the Free Software Foundation,
5.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
5.23 + *
5.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
5.25 + * or visit www.oracle.com if you need additional information or have any
5.26 + * questions.
5.27 + */
5.28 +
5.29 +package java.io;
5.30 +
5.31 +/**
5.32 + * A <code>PushbackInputStream</code> adds
5.33 + * functionality to another input stream, namely
5.34 + * the ability to "push back" or "unread"
5.35 + * one byte. This is useful in situations where
5.36 + * it is convenient for a fragment of code
5.37 + * to read an indefinite number of data bytes
5.38 + * that are delimited by a particular byte
5.39 + * value; after reading the terminating byte,
5.40 + * the code fragment can "unread" it, so that
5.41 + * the next read operation on the input stream
5.42 + * will reread the byte that was pushed back.
5.43 + * For example, bytes representing the characters
5.44 + * constituting an identifier might be terminated
5.45 + * by a byte representing an operator character;
5.46 + * a method whose job is to read just an identifier
5.47 + * can read until it sees the operator and
5.48 + * then push the operator back to be re-read.
5.49 + *
5.50 + * @author David Connelly
5.51 + * @author Jonathan Payne
5.52 + * @since JDK1.0
5.53 + */
5.54 +public
5.55 +class PushbackInputStream extends FilterInputStream {
5.56 + /**
5.57 + * The pushback buffer.
5.58 + * @since JDK1.1
5.59 + */
5.60 + protected byte[] buf;
5.61 +
5.62 + /**
5.63 + * The position within the pushback buffer from which the next byte will
5.64 + * be read. When the buffer is empty, <code>pos</code> is equal to
5.65 + * <code>buf.length</code>; when the buffer is full, <code>pos</code> is
5.66 + * equal to zero.
5.67 + *
5.68 + * @since JDK1.1
5.69 + */
5.70 + protected int pos;
5.71 +
5.72 + /**
5.73 + * Check to make sure that this stream has not been closed
5.74 + */
5.75 + private void ensureOpen() throws IOException {
5.76 + if (in == null)
5.77 + throw new IOException("Stream closed");
5.78 + }
5.79 +
5.80 + /**
5.81 + * Creates a <code>PushbackInputStream</code>
5.82 + * with a pushback buffer of the specified <code>size</code>,
5.83 + * and saves its argument, the input stream
5.84 + * <code>in</code>, for later use. Initially,
5.85 + * there is no pushed-back byte (the field
5.86 + * <code>pushBack</code> is initialized to
5.87 + * <code>-1</code>).
5.88 + *
5.89 + * @param in the input stream from which bytes will be read.
5.90 + * @param size the size of the pushback buffer.
5.91 + * @exception IllegalArgumentException if size is <= 0
5.92 + * @since JDK1.1
5.93 + */
5.94 + public PushbackInputStream(InputStream in, int size) {
5.95 + super(in);
5.96 + if (size <= 0) {
5.97 + throw new IllegalArgumentException("size <= 0");
5.98 + }
5.99 + this.buf = new byte[size];
5.100 + this.pos = size;
5.101 + }
5.102 +
5.103 + /**
5.104 + * Creates a <code>PushbackInputStream</code>
5.105 + * and saves its argument, the input stream
5.106 + * <code>in</code>, for later use. Initially,
5.107 + * there is no pushed-back byte (the field
5.108 + * <code>pushBack</code> is initialized to
5.109 + * <code>-1</code>).
5.110 + *
5.111 + * @param in the input stream from which bytes will be read.
5.112 + */
5.113 + public PushbackInputStream(InputStream in) {
5.114 + this(in, 1);
5.115 + }
5.116 +
5.117 + /**
5.118 + * Reads the next byte of data from this input stream. The value
5.119 + * byte is returned as an <code>int</code> in the range
5.120 + * <code>0</code> to <code>255</code>. If no byte is available
5.121 + * because the end of the stream has been reached, the value
5.122 + * <code>-1</code> is returned. This method blocks until input data
5.123 + * is available, the end of the stream is detected, or an exception
5.124 + * is thrown.
5.125 + *
5.126 + * <p> This method returns the most recently pushed-back byte, if there is
5.127 + * one, and otherwise calls the <code>read</code> method of its underlying
5.128 + * input stream and returns whatever value that method returns.
5.129 + *
5.130 + * @return the next byte of data, or <code>-1</code> if the end of the
5.131 + * stream has been reached.
5.132 + * @exception IOException if this input stream has been closed by
5.133 + * invoking its {@link #close()} method,
5.134 + * or an I/O error occurs.
5.135 + * @see java.io.InputStream#read()
5.136 + */
5.137 + public int read() throws IOException {
5.138 + ensureOpen();
5.139 + if (pos < buf.length) {
5.140 + return buf[pos++] & 0xff;
5.141 + }
5.142 + return super.read();
5.143 + }
5.144 +
5.145 + /**
5.146 + * Reads up to <code>len</code> bytes of data from this input stream into
5.147 + * an array of bytes. This method first reads any pushed-back bytes; after
5.148 + * that, if fewer than <code>len</code> bytes have been read then it
5.149 + * reads from the underlying input stream. If <code>len</code> is not zero, the method
5.150 + * blocks until at least 1 byte of input is available; otherwise, no
5.151 + * bytes are read and <code>0</code> is returned.
5.152 + *
5.153 + * @param b the buffer into which the data is read.
5.154 + * @param off the start offset in the destination array <code>b</code>
5.155 + * @param len the maximum number of bytes read.
5.156 + * @return the total number of bytes read into the buffer, or
5.157 + * <code>-1</code> if there is no more data because the end of
5.158 + * the stream has been reached.
5.159 + * @exception NullPointerException If <code>b</code> is <code>null</code>.
5.160 + * @exception IndexOutOfBoundsException If <code>off</code> is negative,
5.161 + * <code>len</code> is negative, or <code>len</code> is greater than
5.162 + * <code>b.length - off</code>
5.163 + * @exception IOException if this input stream has been closed by
5.164 + * invoking its {@link #close()} method,
5.165 + * or an I/O error occurs.
5.166 + * @see java.io.InputStream#read(byte[], int, int)
5.167 + */
5.168 + public int read(byte[] b, int off, int len) throws IOException {
5.169 + ensureOpen();
5.170 + if (b == null) {
5.171 + throw new NullPointerException();
5.172 + } else if (off < 0 || len < 0 || len > b.length - off) {
5.173 + throw new IndexOutOfBoundsException();
5.174 + } else if (len == 0) {
5.175 + return 0;
5.176 + }
5.177 +
5.178 + int avail = buf.length - pos;
5.179 + if (avail > 0) {
5.180 + if (len < avail) {
5.181 + avail = len;
5.182 + }
5.183 + System.arraycopy(buf, pos, b, off, avail);
5.184 + pos += avail;
5.185 + off += avail;
5.186 + len -= avail;
5.187 + }
5.188 + if (len > 0) {
5.189 + len = super.read(b, off, len);
5.190 + if (len == -1) {
5.191 + return avail == 0 ? -1 : avail;
5.192 + }
5.193 + return avail + len;
5.194 + }
5.195 + return avail;
5.196 + }
5.197 +
5.198 + /**
5.199 + * Pushes back a byte by copying it to the front of the pushback buffer.
5.200 + * After this method returns, the next byte to be read will have the value
5.201 + * <code>(byte)b</code>.
5.202 + *
5.203 + * @param b the <code>int</code> value whose low-order
5.204 + * byte is to be pushed back.
5.205 + * @exception IOException If there is not enough room in the pushback
5.206 + * buffer for the byte, or this input stream has been closed by
5.207 + * invoking its {@link #close()} method.
5.208 + */
5.209 + public void unread(int b) throws IOException {
5.210 + ensureOpen();
5.211 + if (pos == 0) {
5.212 + throw new IOException("Push back buffer is full");
5.213 + }
5.214 + buf[--pos] = (byte)b;
5.215 + }
5.216 +
5.217 + /**
5.218 + * Pushes back a portion of an array of bytes by copying it to the front
5.219 + * of the pushback buffer. After this method returns, the next byte to be
5.220 + * read will have the value <code>b[off]</code>, the byte after that will
5.221 + * have the value <code>b[off+1]</code>, and so forth.
5.222 + *
5.223 + * @param b the byte array to push back.
5.224 + * @param off the start offset of the data.
5.225 + * @param len the number of bytes to push back.
5.226 + * @exception IOException If there is not enough room in the pushback
5.227 + * buffer for the specified number of bytes,
5.228 + * or this input stream has been closed by
5.229 + * invoking its {@link #close()} method.
5.230 + * @since JDK1.1
5.231 + */
5.232 + public void unread(byte[] b, int off, int len) throws IOException {
5.233 + ensureOpen();
5.234 + if (len > pos) {
5.235 + throw new IOException("Push back buffer is full");
5.236 + }
5.237 + pos -= len;
5.238 + System.arraycopy(b, off, buf, pos, len);
5.239 + }
5.240 +
5.241 + /**
5.242 + * Pushes back an array of bytes by copying it to the front of the
5.243 + * pushback buffer. After this method returns, the next byte to be read
5.244 + * will have the value <code>b[0]</code>, the byte after that will have the
5.245 + * value <code>b[1]</code>, and so forth.
5.246 + *
5.247 + * @param b the byte array to push back
5.248 + * @exception IOException If there is not enough room in the pushback
5.249 + * buffer for the specified number of bytes,
5.250 + * or this input stream has been closed by
5.251 + * invoking its {@link #close()} method.
5.252 + * @since JDK1.1
5.253 + */
5.254 + public void unread(byte[] b) throws IOException {
5.255 + unread(b, 0, b.length);
5.256 + }
5.257 +
5.258 + /**
5.259 + * Returns an estimate of the number of bytes that can be read (or
5.260 + * skipped over) from this input stream without blocking by the next
5.261 + * invocation of a method for this input stream. The next invocation might be
5.262 + * the same thread or another thread. A single read or skip of this
5.263 + * many bytes will not block, but may read or skip fewer bytes.
5.264 + *
5.265 + * <p> The method returns the sum of the number of bytes that have been
5.266 + * pushed back and the value returned by {@link
5.267 + * java.io.FilterInputStream#available available}.
5.268 + *
5.269 + * @return the number of bytes that can be read (or skipped over) from
5.270 + * the input stream without blocking.
5.271 + * @exception IOException if this input stream has been closed by
5.272 + * invoking its {@link #close()} method,
5.273 + * or an I/O error occurs.
5.274 + * @see java.io.FilterInputStream#in
5.275 + * @see java.io.InputStream#available()
5.276 + */
5.277 + public int available() throws IOException {
5.278 + ensureOpen();
5.279 + int n = buf.length - pos;
5.280 + int avail = super.available();
5.281 + return n > (Integer.MAX_VALUE - avail)
5.282 + ? Integer.MAX_VALUE
5.283 + : n + avail;
5.284 + }
5.285 +
5.286 + /**
5.287 + * Skips over and discards <code>n</code> bytes of data from this
5.288 + * input stream. The <code>skip</code> method may, for a variety of
5.289 + * reasons, end up skipping over some smaller number of bytes,
5.290 + * possibly zero. If <code>n</code> is negative, no bytes are skipped.
5.291 + *
5.292 + * <p> The <code>skip</code> method of <code>PushbackInputStream</code>
5.293 + * first skips over the bytes in the pushback buffer, if any. It then
5.294 + * calls the <code>skip</code> method of the underlying input stream if
5.295 + * more bytes need to be skipped. The actual number of bytes skipped
5.296 + * is returned.
5.297 + *
5.298 + * @param n {@inheritDoc}
5.299 + * @return {@inheritDoc}
5.300 + * @exception IOException if the stream does not support seek,
5.301 + * or the stream has been closed by
5.302 + * invoking its {@link #close()} method,
5.303 + * or an I/O error occurs.
5.304 + * @see java.io.FilterInputStream#in
5.305 + * @see java.io.InputStream#skip(long n)
5.306 + * @since 1.2
5.307 + */
5.308 + public long skip(long n) throws IOException {
5.309 + ensureOpen();
5.310 + if (n <= 0) {
5.311 + return 0;
5.312 + }
5.313 +
5.314 + long pskip = buf.length - pos;
5.315 + if (pskip > 0) {
5.316 + if (n < pskip) {
5.317 + pskip = n;
5.318 + }
5.319 + pos += pskip;
5.320 + n -= pskip;
5.321 + }
5.322 + if (n > 0) {
5.323 + pskip += super.skip(n);
5.324 + }
5.325 + return pskip;
5.326 + }
5.327 +
5.328 + /**
5.329 + * Tests if this input stream supports the <code>mark</code> and
5.330 + * <code>reset</code> methods, which it does not.
5.331 + *
5.332 + * @return <code>false</code>, since this class does not support the
5.333 + * <code>mark</code> and <code>reset</code> methods.
5.334 + * @see java.io.InputStream#mark(int)
5.335 + * @see java.io.InputStream#reset()
5.336 + */
5.337 + public boolean markSupported() {
5.338 + return false;
5.339 + }
5.340 +
5.341 + /**
5.342 + * Marks the current position in this input stream.
5.343 + *
5.344 + * <p> The <code>mark</code> method of <code>PushbackInputStream</code>
5.345 + * does nothing.
5.346 + *
5.347 + * @param readlimit the maximum limit of bytes that can be read before
5.348 + * the mark position becomes invalid.
5.349 + * @see java.io.InputStream#reset()
5.350 + */
5.351 + public synchronized void mark(int readlimit) {
5.352 + }
5.353 +
5.354 + /**
5.355 + * Repositions this stream to the position at the time the
5.356 + * <code>mark</code> method was last called on this input stream.
5.357 + *
5.358 + * <p> The method <code>reset</code> for class
5.359 + * <code>PushbackInputStream</code> does nothing except throw an
5.360 + * <code>IOException</code>.
5.361 + *
5.362 + * @exception IOException if this method is invoked.
5.363 + * @see java.io.InputStream#mark(int)
5.364 + * @see java.io.IOException
5.365 + */
5.366 + public synchronized void reset() throws IOException {
5.367 + throw new IOException("mark/reset not supported");
5.368 + }
5.369 +
5.370 + /**
5.371 + * Closes this input stream and releases any system resources
5.372 + * associated with the stream.
5.373 + * Once the stream has been closed, further read(), unread(),
5.374 + * available(), reset(), or skip() invocations will throw an IOException.
5.375 + * Closing a previously closed stream has no effect.
5.376 + *
5.377 + * @exception IOException if an I/O error occurs.
5.378 + */
5.379 + public synchronized void close() throws IOException {
5.380 + if (in == null)
5.381 + return;
5.382 + in.close();
5.383 + in = null;
5.384 + buf = null;
5.385 + }
5.386 +}
6.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
6.2 +++ b/emul/src/main/java/java/io/UTFDataFormatException.java Sat Nov 10 17:54:30 2012 +0100
6.3 @@ -0,0 +1,69 @@
6.4 +/*
6.5 + * Copyright (c) 1995, 2008, Oracle and/or its affiliates. All rights reserved.
6.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6.7 + *
6.8 + * This code is free software; you can redistribute it and/or modify it
6.9 + * under the terms of the GNU General Public License version 2 only, as
6.10 + * published by the Free Software Foundation. Oracle designates this
6.11 + * particular file as subject to the "Classpath" exception as provided
6.12 + * by Oracle in the LICENSE file that accompanied this code.
6.13 + *
6.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
6.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
6.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
6.17 + * version 2 for more details (a copy is included in the LICENSE file that
6.18 + * accompanied this code).
6.19 + *
6.20 + * You should have received a copy of the GNU General Public License version
6.21 + * 2 along with this work; if not, write to the Free Software Foundation,
6.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
6.23 + *
6.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
6.25 + * or visit www.oracle.com if you need additional information or have any
6.26 + * questions.
6.27 + */
6.28 +
6.29 +package java.io;
6.30 +
6.31 +/**
6.32 + * Signals that a malformed string in
6.33 + * <a href="DataInput.html#modified-utf-8">modified UTF-8</a>
6.34 + * format has been read in a data
6.35 + * input stream or by any class that implements the data input
6.36 + * interface.
6.37 + * See the
6.38 + * <a href="DataInput.html#modified-utf-8"><code>DataInput</code></a>
6.39 + * class description for the format in
6.40 + * which modified UTF-8 strings are read and written.
6.41 + *
6.42 + * @author Frank Yellin
6.43 + * @see java.io.DataInput
6.44 + * @see java.io.DataInputStream#readUTF(java.io.DataInput)
6.45 + * @see java.io.IOException
6.46 + * @since JDK1.0
6.47 + */
6.48 +public
6.49 +class UTFDataFormatException extends IOException {
6.50 + private static final long serialVersionUID = 420743449228280612L;
6.51 +
6.52 + /**
6.53 + * Constructs a <code>UTFDataFormatException</code> with
6.54 + * <code>null</code> as its error detail message.
6.55 + */
6.56 + public UTFDataFormatException() {
6.57 + super();
6.58 + }
6.59 +
6.60 + /**
6.61 + * Constructs a <code>UTFDataFormatException</code> with the
6.62 + * specified detail message. The string <code>s</code> can be
6.63 + * retrieved later by the
6.64 + * <code>{@link java.lang.Throwable#getMessage}</code>
6.65 + * method of class <code>java.lang.Throwable</code>.
6.66 + *
6.67 + * @param s the detail message.
6.68 + */
6.69 + public UTFDataFormatException(String s) {
6.70 + super(s);
6.71 + }
6.72 +}