Path: blob/master/src/java.base/share/classes/java/io/DataInput.java
41152 views
/*1* Copyright (c) 1995, 2019, 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* <h2><a id="modified-utf-8">Modified UTF-8</a></h2>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*58* <ul>59* <li>Characters in the range {@code '\u005Cu0001'} to60* {@code '\u005Cu007F'} are represented by a single byte.61* <li>The null character {@code '\u005Cu0000'} and characters62* in the range {@code '\u005Cu0080'} to {@code '\u005Cu07FF'} are63* represented by a pair of bytes.64* <li>Characters in the range {@code '\u005Cu0800'}65* to {@code '\u005CuFFFF'} are represented by three bytes.66* </ul>67*68* <table class="plain" style="margin-left:2em;">69* <caption>Encoding of UTF-8 values</caption>70* <thead>71* <tr>72* <th scope="col" rowspan="2">Value</th>73* <th scope="col" rowspan="2">Byte</th>74* <th scope="col" colspan="8" id="bit_a">Bit Values</th>75* </tr>76* <tr>77* <!-- Value -->78* <!-- Byte -->79* <th scope="col" style="width:3em"> 7 </th>80* <th scope="col" style="width:3em"> 6 </th>81* <th scope="col" style="width:3em"> 5 </th>82* <th scope="col" style="width:3em"> 4 </th>83* <th scope="col" style="width:3em"> 3 </th>84* <th scope="col" style="width:3em"> 2 </th>85* <th scope="col" style="width:3em"> 1 </th>86* <th scope="col" style="width:3em"> 0 </th>87* </thead>88* <tbody>89* <tr>90* <th scope="row" style="text-align:left; font-weight:normal">91* {@code \u005Cu0001} to {@code \u005Cu007F} </th>92* <th scope="row" style="font-weight:normal; text-align:center"> 1 </th>93* <td style="text-align:center">094* <td colspan="7" style="text-align:right; padding-right:6em">bits 6-095* </tr>96* <tr>97* <th scope="row" rowspan="2" style="text-align:left; font-weight:normal">98* {@code \u005Cu0000},<br>99* {@code \u005Cu0080} to {@code \u005Cu07FF} </th>100* <th scope="row" style="font-weight:normal; text-align:center"> 1 </th>101* <td style="text-align:center">1102* <td style="text-align:center">1103* <td style="text-align:center">0104* <td colspan="5" style="text-align:right; padding-right:6em">bits 10-6105* </tr>106* <tr>107* <!-- (value) -->108* <th scope="row" style="font-weight:normal; text-align:center"> 2 </th>109* <td style="text-align:center">1110* <td style="text-align:center">0111* <td colspan="6" style="text-align:right; padding-right:6em">bits 5-0112* </tr>113* <tr>114* <th scope="row" rowspan="3" style="text-align:left; font-weight:normal">115* {@code \u005Cu0800} to {@code \u005CuFFFF} </th>116* <th scope="row" style="font-weight:normal; text-align:center"> 1 </th>117* <td style="text-align:center">1118* <td style="text-align:center">1119* <td style="text-align:center">1120* <td style="text-align:center">0121* <td colspan="4" style="text-align:right; padding-right:6em">bits 15-12122* </tr>123* <tr>124* <!-- (value) -->125* <th scope="row" style="font-weight:normal; text-align:center"> 2 </th>126* <td style="text-align:center">1127* <td style="text-align:center">0128* <td colspan="6" style="text-align:right; padding-right:6em">bits 11-6129* </tr>130* <tr>131* <!-- (value) -->132* <th scope="row" style="font-weight:normal; text-align:center"> 3 </th>133* <td style="text-align:center">1134* <td style="text-align:center">0135* <td colspan="6" style="text-align:right; padding-right:6em">bits 5-0136* </tr>137* </tbody>138* </table>139*140* <p>141* The differences between this format and the142* standard UTF-8 format are the following:143* <ul>144* <li>The null byte {@code '\u005Cu0000'} is encoded in 2-byte format145* rather than 1-byte, so that the encoded strings never have146* embedded nulls.147* <li>Only the 1-byte, 2-byte, and 3-byte formats are used.148* <li><a href="../lang/Character.html#unicode">Supplementary characters</a>149* are represented in the form of surrogate pairs.150* </ul>151* @author Frank Yellin152* @see java.io.DataInputStream153* @see java.io.DataOutput154* @since 1.0155*/156public interface DataInput {157/**158* Reads some bytes from an input159* stream and stores them into the buffer160* array {@code b}. The number of bytes161* read is equal162* to the length of {@code b}.163* <p>164* This method blocks until one of the165* following conditions occurs:166* <ul>167* <li>{@code b.length}168* bytes of input data are available, in which169* case a normal return is made.170*171* <li>End of172* file is detected, in which case an {@code EOFException}173* is thrown.174*175* <li>An I/O error occurs, in176* which case an {@code IOException} other177* than {@code EOFException} is thrown.178* </ul>179* <p>180* If {@code b} is {@code null},181* a {@code NullPointerException} is thrown.182* If {@code b.length} is zero, then183* no bytes are read. Otherwise, the first184* byte read is stored into element {@code b[0]},185* the next one into {@code b[1]}, and186* so on.187* If an exception is thrown from188* this method, then it may be that some but189* not all bytes of {@code b} have been190* updated with data from the input stream.191*192* @param b the buffer into which the data is read.193* @throws NullPointerException if {@code b} is {@code null}.194* @throws EOFException if this stream reaches the end before reading195* all the bytes.196* @throws IOException if an I/O error occurs.197*/198void readFully(byte b[]) throws IOException;199200/**201*202* Reads {@code len}203* bytes from204* an input stream.205* <p>206* This method207* blocks until one of the following conditions208* occurs:209* <ul>210* <li>{@code len} bytes211* of input data are available, in which case212* a normal return is made.213*214* <li>End of file215* is detected, in which case an {@code EOFException}216* is thrown.217*218* <li>An I/O error occurs, in219* which case an {@code IOException} other220* than {@code EOFException} is thrown.221* </ul>222* <p>223* If {@code b} is {@code null},224* a {@code NullPointerException} is thrown.225* If {@code off} is negative, or {@code len}226* is negative, or {@code off+len} is227* greater than the length of the array {@code b},228* then an {@code IndexOutOfBoundsException}229* is thrown.230* If {@code len} is zero,231* then no bytes are read. Otherwise, the first232* byte read is stored into element {@code b[off]},233* the next one into {@code b[off+1]},234* and so on. The number of bytes read is,235* at most, equal to {@code len}.236*237* @param b the buffer into which the data is read.238* @param off an int specifying the offset in the data array {@code b}.239* @param len an int specifying the number of bytes to read.240* @throws NullPointerException if {@code b} is {@code null}.241* @throws IndexOutOfBoundsException if {@code off} is negative,242* {@code len} is negative, or {@code len} is greater than243* {@code b.length - off}.244* @throws EOFException if this stream reaches the end before reading245* all the bytes.246* @throws IOException if an I/O error occurs.247*/248void readFully(byte b[], int off, int len) throws IOException;249250/**251* Makes an attempt to skip over252* {@code n} bytes253* of data from the input254* stream, discarding the skipped bytes. However,255* it may skip256* over some smaller number of257* bytes, possibly zero. This may result from258* any of a259* number of conditions; reaching260* end of file before {@code n} bytes261* have been skipped is262* only one possibility.263* This method never throws an {@code EOFException}.264* The actual265* number of bytes skipped is returned.266*267* @param n the number of bytes to be skipped.268* @return the number of bytes actually skipped.269* @throws IOException if an I/O error occurs.270*/271int skipBytes(int n) throws IOException;272273/**274* Reads one input byte and returns275* {@code true} if that byte is nonzero,276* {@code false} if that byte is zero.277* This method is suitable for reading278* the byte written by the {@code writeBoolean}279* method of interface {@code DataOutput}.280*281* @return the {@code boolean} value read.282* @throws EOFException if this stream reaches the end before reading283* all the bytes.284* @throws IOException if an I/O error occurs.285*/286boolean readBoolean() throws IOException;287288/**289* Reads and returns one input byte.290* The byte is treated as a signed value in291* the range {@code -128} through {@code 127},292* inclusive.293* This method is suitable for294* reading the byte written by the {@code writeByte}295* method of interface {@code DataOutput}.296*297* @return the 8-bit value read.298* @throws EOFException if this stream reaches the end before reading299* all the bytes.300* @throws IOException if an I/O error occurs.301*/302byte readByte() throws IOException;303304/**305* Reads one input byte, zero-extends306* it to type {@code int}, and returns307* the result, which is therefore in the range308* {@code 0}309* through {@code 255}.310* This method is suitable for reading311* the byte written by the {@code writeByte}312* method of interface {@code DataOutput}313* if the argument to {@code writeByte}314* was intended to be a value in the range315* {@code 0} through {@code 255}.316*317* @return the unsigned 8-bit value read.318* @throws EOFException if this stream reaches the end before reading319* all the bytes.320* @throws IOException if an I/O error occurs.321*/322int readUnsignedByte() throws IOException;323324/**325* Reads two input bytes and returns326* a {@code short} value. Let {@code a}327* be the first byte read and {@code b}328* be the second byte. The value329* returned330* is:331* <pre>{@code (short)((a << 8) | (b & 0xff))332* }</pre>333* This method334* is suitable for reading the bytes written335* by the {@code writeShort} method of336* interface {@code DataOutput}.337*338* @return the 16-bit value read.339* @throws EOFException if this stream reaches the end before reading340* all the bytes.341* @throws IOException if an I/O error occurs.342*/343short readShort() throws IOException;344345/**346* Reads two input bytes and returns347* an {@code int} value in the range {@code 0}348* through {@code 65535}. Let {@code a}349* be the first byte read and350* {@code b}351* be the second byte. The value returned is:352* <pre>{@code (((a & 0xff) << 8) | (b & 0xff))353* }</pre>354* This method is suitable for reading the bytes355* written by the {@code writeShort} method356* of interface {@code DataOutput} if357* the argument to {@code writeShort}358* was intended to be a value in the range359* {@code 0} through {@code 65535}.360*361* @return the unsigned 16-bit value read.362* @throws EOFException if this stream reaches the end before reading363* all the bytes.364* @throws IOException if an I/O error occurs.365*/366int readUnsignedShort() throws IOException;367368/**369* Reads two input bytes and returns a {@code char} value.370* Let {@code a}371* be the first byte read and {@code b}372* be the second byte. The value373* returned is:374* <pre>{@code (char)((a << 8) | (b & 0xff))375* }</pre>376* This method377* is suitable for reading bytes written by378* the {@code writeChar} method of interface379* {@code DataOutput}.380*381* @return the {@code char} value read.382* @throws EOFException if this stream reaches the end before reading383* all the bytes.384* @throws IOException if an I/O error occurs.385*/386char readChar() throws IOException;387388/**389* Reads four input bytes and returns an390* {@code int} value. Let {@code a-d}391* be the first through fourth bytes read. The value returned is:392* <pre>{@code393* (((a & 0xff) << 24) | ((b & 0xff) << 16) |394* ((c & 0xff) << 8) | (d & 0xff))395* }</pre>396* This method is suitable397* for reading bytes written by the {@code writeInt}398* method of interface {@code DataOutput}.399*400* @return the {@code int} value read.401* @throws EOFException if this stream reaches the end before reading402* all the bytes.403* @throws IOException if an I/O error occurs.404*/405int readInt() throws IOException;406407/**408* Reads eight input bytes and returns409* a {@code long} value. Let {@code a-h}410* be the first through eighth bytes read.411* The value returned is:412* <pre>{@code413* (((long)(a & 0xff) << 56) |414* ((long)(b & 0xff) << 48) |415* ((long)(c & 0xff) << 40) |416* ((long)(d & 0xff) << 32) |417* ((long)(e & 0xff) << 24) |418* ((long)(f & 0xff) << 16) |419* ((long)(g & 0xff) << 8) |420* ((long)(h & 0xff)))421* }</pre>422* <p>423* This method is suitable424* for reading bytes written by the {@code writeLong}425* method of interface {@code DataOutput}.426*427* @return the {@code long} value read.428* @throws EOFException if this stream reaches the end before reading429* all the bytes.430* @throws IOException if an I/O error occurs.431*/432long readLong() throws IOException;433434/**435* Reads four input bytes and returns436* a {@code float} value. It does this437* by first constructing an {@code int}438* value in exactly the manner439* of the {@code readInt}440* method, then converting this {@code int}441* value to a {@code float} in442* exactly the manner of the method {@code Float.intBitsToFloat}.443* This method is suitable for reading444* bytes written by the {@code writeFloat}445* method of interface {@code DataOutput}.446*447* @return the {@code float} value read.448* @throws EOFException if this stream reaches the end before reading449* all the bytes.450* @throws IOException if an I/O error occurs.451*/452float readFloat() throws IOException;453454/**455* Reads eight input bytes and returns456* a {@code double} value. It does this457* by first constructing a {@code long}458* value in exactly the manner459* of the {@code readLong}460* method, then converting this {@code long}461* value to a {@code double} in exactly462* the manner of the method {@code Double.longBitsToDouble}.463* This method is suitable for reading464* bytes written by the {@code writeDouble}465* method of interface {@code DataOutput}.466*467* @return the {@code double} value read.468* @throws EOFException if this stream reaches the end before reading469* all the bytes.470* @throws IOException if an I/O error occurs.471*/472double readDouble() throws IOException;473474/**475* Reads the next line of text from the input stream.476* It reads successive bytes, converting477* each byte separately into a character,478* until it encounters a line terminator or479* end of480* file; the characters read are then481* returned as a {@code String}. Note482* that because this483* method processes bytes,484* it does not support input of the full Unicode485* character set.486* <p>487* If end of file is encountered488* before even one byte can be read, then {@code null}489* is returned. Otherwise, each byte that is490* read is converted to type {@code char}491* by zero-extension. If the character {@code '\n'}492* is encountered, it is discarded and reading493* ceases. If the character {@code '\r'}494* is encountered, it is discarded and, if495* the following byte converts  to the496* character {@code '\n'}, then that is497* discarded also; reading then ceases. If498* end of file is encountered before either499* of the characters {@code '\n'} and500* {@code '\r'} is encountered, reading501* ceases. Once reading has ceased, a {@code String}502* is returned that contains all the characters503* read and not discarded, taken in order.504* Note that every character in this string505* will have a value less than {@code \u005Cu0100},506* that is, {@code (char)256}.507*508* @return the next line of text from the input stream,509* or {@code null} if the end of file is510* encountered before a byte can be read.511* @throws IOException if an I/O error occurs.512*/513String readLine() throws IOException;514515/**516* Reads in a string that has been encoded using a517* <a href="#modified-utf-8">modified UTF-8</a>518* format.519* The general contract of {@code readUTF}520* is that it reads a representation of a Unicode521* character string encoded in modified522* UTF-8 format; this string of characters523* is then returned as a {@code String}.524* <p>525* First, two bytes are read and used to526* construct an unsigned 16-bit integer in527* exactly the manner of the {@code readUnsignedShort}528* method . This integer value is called the529* <i>UTF length</i> and specifies the number530* of additional bytes to be read. These bytes531* are then converted to characters by considering532* them in groups. The length of each group533* is computed from the value of the first534* byte of the group. The byte following a535* group, if any, is the first byte of the536* next group.537* <p>538* If the first byte of a group539* matches the bit pattern {@code 0xxxxxxx}540* (where {@code x} means "may be {@code 0}541* or {@code 1}"), then the group consists542* of just that byte. The byte is zero-extended543* to form a character.544* <p>545* If the first byte546* of a group matches the bit pattern {@code 110xxxxx},547* then the group consists of that byte {@code a}548* and a second byte {@code b}. If there549* is no byte {@code b} (because byte550* {@code a} was the last of the bytes551* to be read), or if byte {@code b} does552* not match the bit pattern {@code 10xxxxxx},553* then a {@code UTFDataFormatException}554* is thrown. Otherwise, the group is converted555* to the character:556* <pre>{@code (char)(((a & 0x1F) << 6) | (b & 0x3F))557* }</pre>558* If the first byte of a group559* matches the bit pattern {@code 1110xxxx},560* then the group consists of that byte {@code a}561* and two more bytes {@code b} and {@code c}.562* If there is no byte {@code c} (because563* byte {@code a} was one of the last564* two of the bytes to be read), or either565* byte {@code b} or byte {@code c}566* does not match the bit pattern {@code 10xxxxxx},567* then a {@code UTFDataFormatException}568* is thrown. Otherwise, the group is converted569* to the character:570* <pre>{@code571* (char)(((a & 0x0F) << 12) | ((b & 0x3F) << 6) | (c & 0x3F))572* }</pre>573* If the first byte of a group matches the574* pattern {@code 1111xxxx} or the pattern575* {@code 10xxxxxx}, then a {@code UTFDataFormatException}576* is thrown.577* <p>578* If end of file is encountered579* at any time during this entire process,580* then an {@code EOFException} is thrown.581* <p>582* After every group has been converted to583* a character by this process, the characters584* are gathered, in the same order in which585* their corresponding groups were read from586* the input stream, to form a {@code String},587* which is returned.588* <p>589* The {@code writeUTF}590* method of interface {@code DataOutput}591* may be used to write data that is suitable592* for reading by this method.593* @return a Unicode string.594* @throws EOFException if this stream reaches the end595* before reading all the bytes.596* @throws IOException if an I/O error occurs.597* @throws UTFDataFormatException if the bytes do not represent a598* valid modified UTF-8 encoding of a string.599*/600String readUTF() throws IOException;601}602603604