Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/java/io/DataInput.java
38829 views
/*1* Copyright (c) 1995, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package java.io;2627/**28* The {@code DataInput} interface provides29* for reading bytes from a binary stream and30* reconstructing from them data in any of31* the Java primitive types. There is also32* a33* facility for reconstructing a {@code String}34* from data in35* <a href="#modified-utf-8">modified UTF-8</a>36* format.37* <p>38* It is generally true of all the reading39* routines in this interface that if end of40* file is reached before the desired number41* of bytes has been read, an {@code EOFException}42* (which is a kind of {@code IOException})43* is thrown. If any byte cannot be read for44* any reason other than end of file, an {@code IOException}45* other than {@code EOFException} is46* thrown. In particular, an {@code IOException}47* may be thrown if the input stream has been48* closed.49*50* <h3><a name="modified-utf-8">Modified UTF-8</a></h3>51* <p>52* Implementations of the DataInput and DataOutput interfaces represent53* Unicode strings in a format that is a slight modification of UTF-8.54* (For information regarding the standard UTF-8 format, see section55* <i>3.9 Unicode Encoding Forms</i> of <i>The Unicode Standard, Version56* 4.0</i>).57* Note that in the following table, the most significant bit appears in the58* far left-hand column.59*60* <blockquote>61* <table border="1" cellspacing="0" cellpadding="8"62* summary="Bit values and bytes">63* <tr>64* <th colspan="9"><span style="font-weight:normal">65* All characters in the range {@code '\u005Cu0001'} to66* {@code '\u005Cu007F'} are represented by a single byte:</span></th>67* </tr>68* <tr>69* <td></td>70* <th colspan="8" id="bit_a">Bit Values</th>71* </tr>72* <tr>73* <th id="byte1_a">Byte 1</th>74* <td><center>0</center>75* <td colspan="7"><center>bits 6-0</center>76* </tr>77* <tr>78* <th colspan="9"><span style="font-weight:normal">79* The null character {@code '\u005Cu0000'} and characters80* in the range {@code '\u005Cu0080'} to {@code '\u005Cu07FF'} are81* represented by a pair of bytes:</span></th>82* </tr>83* <tr>84* <td></td>85* <th colspan="8" id="bit_b">Bit Values</th>86* </tr>87* <tr>88* <th id="byte1_b">Byte 1</th>89* <td><center>1</center>90* <td><center>1</center>91* <td><center>0</center>92* <td colspan="5"><center>bits 10-6</center>93* </tr>94* <tr>95* <th id="byte2_a">Byte 2</th>96* <td><center>1</center>97* <td><center>0</center>98* <td colspan="6"><center>bits 5-0</center>99* </tr>100* <tr>101* <th colspan="9"><span style="font-weight:normal">102* {@code char} values in the range {@code '\u005Cu0800'}103* to {@code '\u005CuFFFF'} are represented by three bytes:</span></th>104* </tr>105* <tr>106* <td></td>107* <th colspan="8"id="bit_c">Bit Values</th>108* </tr>109* <tr>110* <th id="byte1_c">Byte 1</th>111* <td><center>1</center>112* <td><center>1</center>113* <td><center>1</center>114* <td><center>0</center>115* <td colspan="4"><center>bits 15-12</center>116* </tr>117* <tr>118* <th id="byte2_b">Byte 2</th>119* <td><center>1</center>120* <td><center>0</center>121* <td colspan="6"><center>bits 11-6</center>122* </tr>123* <tr>124* <th id="byte3">Byte 3</th>125* <td><center>1</center>126* <td><center>0</center>127* <td colspan="6"><center>bits 5-0</center>128* </tr>129* </table>130* </blockquote>131* <p>132* The differences between this format and the133* standard UTF-8 format are the following:134* <ul>135* <li>The null byte {@code '\u005Cu0000'} is encoded in 2-byte format136* rather than 1-byte, so that the encoded strings never have137* embedded nulls.138* <li>Only the 1-byte, 2-byte, and 3-byte formats are used.139* <li><a href="../lang/Character.html#unicode">Supplementary characters</a>140* are represented in the form of surrogate pairs.141* </ul>142* @author Frank Yellin143* @see java.io.DataInputStream144* @see java.io.DataOutput145* @since JDK1.0146*/147public148interface DataInput {149/**150* Reads some bytes from an input151* stream and stores them into the buffer152* array {@code b}. The number of bytes153* read is equal154* to the length of {@code b}.155* <p>156* This method blocks until one of the157* following conditions occurs:158* <ul>159* <li>{@code b.length}160* bytes of input data are available, in which161* case a normal return is made.162*163* <li>End of164* file is detected, in which case an {@code EOFException}165* is thrown.166*167* <li>An I/O error occurs, in168* which case an {@code IOException} other169* than {@code EOFException} is thrown.170* </ul>171* <p>172* If {@code b} is {@code null},173* a {@code NullPointerException} is thrown.174* If {@code b.length} is zero, then175* no bytes are read. Otherwise, the first176* byte read is stored into element {@code b[0]},177* the next one into {@code b[1]}, and178* so on.179* If an exception is thrown from180* this method, then it may be that some but181* not all bytes of {@code b} have been182* updated with data from the input stream.183*184* @param b the buffer into which the data is read.185* @exception EOFException if this stream reaches the end before reading186* all the bytes.187* @exception IOException if an I/O error occurs.188*/189void readFully(byte b[]) throws IOException;190191/**192*193* Reads {@code len}194* bytes from195* an input stream.196* <p>197* This method198* blocks until one of the following conditions199* occurs:200* <ul>201* <li>{@code len} bytes202* of input data are available, in which case203* a normal return is made.204*205* <li>End of file206* is detected, in which case an {@code EOFException}207* is thrown.208*209* <li>An I/O error occurs, in210* which case an {@code IOException} other211* than {@code EOFException} is thrown.212* </ul>213* <p>214* If {@code b} is {@code null},215* a {@code NullPointerException} is thrown.216* If {@code off} is negative, or {@code len}217* is negative, or {@code off+len} is218* greater than the length of the array {@code b},219* then an {@code IndexOutOfBoundsException}220* is thrown.221* If {@code len} is zero,222* then no bytes are read. Otherwise, the first223* byte read is stored into element {@code b[off]},224* the next one into {@code b[off+1]},225* and so on. The number of bytes read is,226* at most, equal to {@code len}.227*228* @param b the buffer into which the data is read.229* @param off an int specifying the offset into the data.230* @param len an int specifying the number of bytes to read.231* @exception EOFException if this stream reaches the end before reading232* all the bytes.233* @exception IOException if an I/O error occurs.234*/235void readFully(byte b[], int off, int len) throws IOException;236237/**238* Makes an attempt to skip over239* {@code n} bytes240* of data from the input241* stream, discarding the skipped bytes. However,242* it may skip243* over some smaller number of244* bytes, possibly zero. This may result from245* any of a246* number of conditions; reaching247* end of file before {@code n} bytes248* have been skipped is249* only one possibility.250* This method never throws an {@code EOFException}.251* The actual252* number of bytes skipped is returned.253*254* @param n the number of bytes to be skipped.255* @return the number of bytes actually skipped.256* @exception IOException if an I/O error occurs.257*/258int skipBytes(int n) throws IOException;259260/**261* Reads one input byte and returns262* {@code true} if that byte is nonzero,263* {@code false} if that byte is zero.264* This method is suitable for reading265* the byte written by the {@code writeBoolean}266* method of interface {@code DataOutput}.267*268* @return the {@code boolean} value read.269* @exception EOFException if this stream reaches the end before reading270* all the bytes.271* @exception IOException if an I/O error occurs.272*/273boolean readBoolean() throws IOException;274275/**276* Reads and returns one input byte.277* The byte is treated as a signed value in278* the range {@code -128} through {@code 127},279* inclusive.280* This method is suitable for281* reading the byte written by the {@code writeByte}282* method of interface {@code DataOutput}.283*284* @return the 8-bit value read.285* @exception EOFException if this stream reaches the end before reading286* all the bytes.287* @exception IOException if an I/O error occurs.288*/289byte readByte() throws IOException;290291/**292* Reads one input byte, zero-extends293* it to type {@code int}, and returns294* the result, which is therefore in the range295* {@code 0}296* through {@code 255}.297* This method is suitable for reading298* the byte written by the {@code writeByte}299* method of interface {@code DataOutput}300* if the argument to {@code writeByte}301* was intended to be a value in the range302* {@code 0} through {@code 255}.303*304* @return the unsigned 8-bit value read.305* @exception EOFException if this stream reaches the end before reading306* all the bytes.307* @exception IOException if an I/O error occurs.308*/309int readUnsignedByte() throws IOException;310311/**312* Reads two input bytes and returns313* a {@code short} value. Let {@code a}314* be the first byte read and {@code b}315* be the second byte. The value316* returned317* is:318* <pre>{@code (short)((a << 8) | (b & 0xff))319* }</pre>320* This method321* is suitable for reading the bytes written322* by the {@code writeShort} method of323* interface {@code DataOutput}.324*325* @return the 16-bit value read.326* @exception EOFException if this stream reaches the end before reading327* all the bytes.328* @exception IOException if an I/O error occurs.329*/330short readShort() throws IOException;331332/**333* Reads two input bytes and returns334* an {@code int} value in the range {@code 0}335* through {@code 65535}. Let {@code a}336* be the first byte read and337* {@code b}338* be the second byte. The value returned is:339* <pre>{@code (((a & 0xff) << 8) | (b & 0xff))340* }</pre>341* This method is suitable for reading the bytes342* written by the {@code writeShort} method343* of interface {@code DataOutput} if344* the argument to {@code writeShort}345* was intended to be a value in the range346* {@code 0} through {@code 65535}.347*348* @return the unsigned 16-bit value read.349* @exception EOFException if this stream reaches the end before reading350* all the bytes.351* @exception IOException if an I/O error occurs.352*/353int readUnsignedShort() throws IOException;354355/**356* Reads two input bytes and returns a {@code char} value.357* Let {@code a}358* be the first byte read and {@code b}359* be the second byte. The value360* returned is:361* <pre>{@code (char)((a << 8) | (b & 0xff))362* }</pre>363* This method364* is suitable for reading bytes written by365* the {@code writeChar} method of interface366* {@code DataOutput}.367*368* @return the {@code char} value read.369* @exception EOFException if this stream reaches the end before reading370* all the bytes.371* @exception IOException if an I/O error occurs.372*/373char readChar() throws IOException;374375/**376* Reads four input bytes and returns an377* {@code int} value. Let {@code a-d}378* be the first through fourth bytes read. The value returned is:379* <pre>{@code380* (((a & 0xff) << 24) | ((b & 0xff) << 16) |381* ((c & 0xff) << 8) | (d & 0xff))382* }</pre>383* This method is suitable384* for reading bytes written by the {@code writeInt}385* method of interface {@code DataOutput}.386*387* @return the {@code int} value read.388* @exception EOFException if this stream reaches the end before reading389* all the bytes.390* @exception IOException if an I/O error occurs.391*/392int readInt() throws IOException;393394/**395* Reads eight input bytes and returns396* a {@code long} value. Let {@code a-h}397* be the first through eighth bytes read.398* The value returned is:399* <pre>{@code400* (((long)(a & 0xff) << 56) |401* ((long)(b & 0xff) << 48) |402* ((long)(c & 0xff) << 40) |403* ((long)(d & 0xff) << 32) |404* ((long)(e & 0xff) << 24) |405* ((long)(f & 0xff) << 16) |406* ((long)(g & 0xff) << 8) |407* ((long)(h & 0xff)))408* }</pre>409* <p>410* This method is suitable411* for reading bytes written by the {@code writeLong}412* method of interface {@code DataOutput}.413*414* @return the {@code long} value read.415* @exception EOFException if this stream reaches the end before reading416* all the bytes.417* @exception IOException if an I/O error occurs.418*/419long readLong() throws IOException;420421/**422* Reads four input bytes and returns423* a {@code float} value. It does this424* by first constructing an {@code int}425* value in exactly the manner426* of the {@code readInt}427* method, then converting this {@code int}428* value to a {@code float} in429* exactly the manner of the method {@code Float.intBitsToFloat}.430* This method is suitable for reading431* bytes written by the {@code writeFloat}432* method of interface {@code DataOutput}.433*434* @return the {@code float} value read.435* @exception EOFException if this stream reaches the end before reading436* all the bytes.437* @exception IOException if an I/O error occurs.438*/439float readFloat() throws IOException;440441/**442* Reads eight input bytes and returns443* a {@code double} value. It does this444* by first constructing a {@code long}445* value in exactly the manner446* of the {@code readLong}447* method, then converting this {@code long}448* value to a {@code double} in exactly449* the manner of the method {@code Double.longBitsToDouble}.450* This method is suitable for reading451* bytes written by the {@code writeDouble}452* method of interface {@code DataOutput}.453*454* @return the {@code double} value read.455* @exception EOFException if this stream reaches the end before reading456* all the bytes.457* @exception IOException if an I/O error occurs.458*/459double readDouble() throws IOException;460461/**462* Reads the next line of text from the input stream.463* It reads successive bytes, converting464* each byte separately into a character,465* until it encounters a line terminator or466* end of467* file; the characters read are then468* returned as a {@code String}. Note469* that because this470* method processes bytes,471* it does not support input of the full Unicode472* character set.473* <p>474* If end of file is encountered475* before even one byte can be read, then {@code null}476* is returned. Otherwise, each byte that is477* read is converted to type {@code char}478* by zero-extension. If the character {@code '\n'}479* is encountered, it is discarded and reading480* ceases. If the character {@code '\r'}481* is encountered, it is discarded and, if482* the following byte converts  to the483* character {@code '\n'}, then that is484* discarded also; reading then ceases. If485* end of file is encountered before either486* of the characters {@code '\n'} and487* {@code '\r'} is encountered, reading488* ceases. Once reading has ceased, a {@code String}489* is returned that contains all the characters490* read and not discarded, taken in order.491* Note that every character in this string492* will have a value less than {@code \u005Cu0100},493* that is, {@code (char)256}.494*495* @return the next line of text from the input stream,496* or {@code null} if the end of file is497* encountered before a byte can be read.498* @exception IOException if an I/O error occurs.499*/500String readLine() throws IOException;501502/**503* Reads in a string that has been encoded using a504* <a href="#modified-utf-8">modified UTF-8</a>505* format.506* The general contract of {@code readUTF}507* is that it reads a representation of a Unicode508* character string encoded in modified509* UTF-8 format; this string of characters510* is then returned as a {@code String}.511* <p>512* First, two bytes are read and used to513* construct an unsigned 16-bit integer in514* exactly the manner of the {@code readUnsignedShort}515* method . This integer value is called the516* <i>UTF length</i> and specifies the number517* of additional bytes to be read. These bytes518* are then converted to characters by considering519* them in groups. The length of each group520* is computed from the value of the first521* byte of the group. The byte following a522* group, if any, is the first byte of the523* next group.524* <p>525* If the first byte of a group526* matches the bit pattern {@code 0xxxxxxx}527* (where {@code x} means "may be {@code 0}528* or {@code 1}"), then the group consists529* of just that byte. The byte is zero-extended530* to form a character.531* <p>532* If the first byte533* of a group matches the bit pattern {@code 110xxxxx},534* then the group consists of that byte {@code a}535* and a second byte {@code b}. If there536* is no byte {@code b} (because byte537* {@code a} was the last of the bytes538* to be read), or if byte {@code b} does539* not match the bit pattern {@code 10xxxxxx},540* then a {@code UTFDataFormatException}541* is thrown. Otherwise, the group is converted542* to the character:543* <pre>{@code (char)(((a & 0x1F) << 6) | (b & 0x3F))544* }</pre>545* If the first byte of a group546* matches the bit pattern {@code 1110xxxx},547* then the group consists of that byte {@code a}548* and two more bytes {@code b} and {@code c}.549* If there is no byte {@code c} (because550* byte {@code a} was one of the last551* two of the bytes to be read), or either552* byte {@code b} or byte {@code c}553* does not match the bit pattern {@code 10xxxxxx},554* then a {@code UTFDataFormatException}555* is thrown. Otherwise, the group is converted556* to the character:557* <pre>{@code558* (char)(((a & 0x0F) << 12) | ((b & 0x3F) << 6) | (c & 0x3F))559* }</pre>560* If the first byte of a group matches the561* pattern {@code 1111xxxx} or the pattern562* {@code 10xxxxxx}, then a {@code UTFDataFormatException}563* is thrown.564* <p>565* If end of file is encountered566* at any time during this entire process,567* then an {@code EOFException} is thrown.568* <p>569* After every group has been converted to570* a character by this process, the characters571* are gathered, in the same order in which572* their corresponding groups were read from573* the input stream, to form a {@code String},574* which is returned.575* <p>576* The {@code writeUTF}577* method of interface {@code DataOutput}578* may be used to write data that is suitable579* for reading by this method.580* @return a Unicode string.581* @exception EOFException if this stream reaches the end582* before reading all the bytes.583* @exception IOException if an I/O error occurs.584* @exception UTFDataFormatException if the bytes do not represent a585* valid modified UTF-8 encoding of a string.586*/587String readUTF() throws IOException;588}589590591