Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/java/io/DataInputStream.java
38829 views
/*1* Copyright (c) 1994, 2006, 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* A data input stream lets an application read primitive Java data29* types from an underlying input stream in a machine-independent30* way. An application uses a data output stream to write data that31* can later be read by a data input stream.32* <p>33* DataInputStream is not necessarily safe for multithreaded access.34* Thread safety is optional and is the responsibility of users of35* methods in this class.36*37* @author Arthur van Hoff38* @see java.io.DataOutputStream39* @since JDK1.040*/41public42class DataInputStream extends FilterInputStream implements DataInput {4344/**45* Creates a DataInputStream that uses the specified46* underlying InputStream.47*48* @param in the specified input stream49*/50public DataInputStream(InputStream in) {51super(in);52}5354/**55* working arrays initialized on demand by readUTF56*/57private byte bytearr[] = new byte[80];58private char chararr[] = new char[80];5960/**61* Reads some number of bytes from the contained input stream and62* stores them into the buffer array <code>b</code>. The number of63* bytes actually read is returned as an integer. This method blocks64* until input data is available, end of file is detected, or an65* exception is thrown.66*67* <p>If <code>b</code> is null, a <code>NullPointerException</code> is68* thrown. If the length of <code>b</code> is zero, then no bytes are69* read and <code>0</code> is returned; otherwise, there is an attempt70* to read at least one byte. If no byte is available because the71* stream is at end of file, the value <code>-1</code> is returned;72* otherwise, at least one byte is read and stored into <code>b</code>.73*74* <p>The first byte read is stored into element <code>b[0]</code>, the75* next one into <code>b[1]</code>, and so on. The number of bytes read76* is, at most, equal to the length of <code>b</code>. Let <code>k</code>77* be the number of bytes actually read; these bytes will be stored in78* elements <code>b[0]</code> through <code>b[k-1]</code>, leaving79* elements <code>b[k]</code> through <code>b[b.length-1]</code>80* unaffected.81*82* <p>The <code>read(b)</code> method has the same effect as:83* <blockquote><pre>84* read(b, 0, b.length)85* </pre></blockquote>86*87* @param b the buffer into which the data is read.88* @return the total number of bytes read into the buffer, or89* <code>-1</code> if there is no more data because the end90* of the stream has been reached.91* @exception IOException if the first byte cannot be read for any reason92* other than end of file, the stream has been closed and the underlying93* input stream does not support reading after close, or another I/O94* error occurs.95* @see java.io.FilterInputStream#in96* @see java.io.InputStream#read(byte[], int, int)97*/98public final int read(byte b[]) throws IOException {99return in.read(b, 0, b.length);100}101102/**103* Reads up to <code>len</code> bytes of data from the contained104* input stream into an array of bytes. An attempt is made to read105* as many as <code>len</code> bytes, but a smaller number may be read,106* possibly zero. The number of bytes actually read is returned as an107* integer.108*109* <p> This method blocks until input data is available, end of file is110* detected, or an exception is thrown.111*112* <p> If <code>len</code> is zero, then no bytes are read and113* <code>0</code> is returned; otherwise, there is an attempt to read at114* least one byte. If no byte is available because the stream is at end of115* file, the value <code>-1</code> is returned; otherwise, at least one116* byte is read and stored into <code>b</code>.117*118* <p> The first byte read is stored into element <code>b[off]</code>, the119* next one into <code>b[off+1]</code>, and so on. The number of bytes read120* is, at most, equal to <code>len</code>. Let <i>k</i> be the number of121* bytes actually read; these bytes will be stored in elements122* <code>b[off]</code> through <code>b[off+</code><i>k</i><code>-1]</code>,123* leaving elements <code>b[off+</code><i>k</i><code>]</code> through124* <code>b[off+len-1]</code> unaffected.125*126* <p> In every case, elements <code>b[0]</code> through127* <code>b[off]</code> and elements <code>b[off+len]</code> through128* <code>b[b.length-1]</code> are unaffected.129*130* @param b the buffer into which the data is read.131* @param off the start offset in the destination array <code>b</code>132* @param len the maximum number of bytes read.133* @return the total number of bytes read into the buffer, or134* <code>-1</code> if there is no more data because the end135* of the stream has been reached.136* @exception NullPointerException If <code>b</code> is <code>null</code>.137* @exception IndexOutOfBoundsException If <code>off</code> is negative,138* <code>len</code> is negative, or <code>len</code> is greater than139* <code>b.length - off</code>140* @exception IOException if the first byte cannot be read for any reason141* other than end of file, the stream has been closed and the underlying142* input stream does not support reading after close, or another I/O143* error occurs.144* @see java.io.FilterInputStream#in145* @see java.io.InputStream#read(byte[], int, int)146*/147public final int read(byte b[], int off, int len) throws IOException {148return in.read(b, off, len);149}150151/**152* See the general contract of the <code>readFully</code>153* method of <code>DataInput</code>.154* <p>155* Bytes156* for this operation are read from the contained157* input stream.158*159* @param b the buffer into which the data is read.160* @exception EOFException if this input stream reaches the end before161* reading all the bytes.162* @exception IOException the stream has been closed and the contained163* input stream does not support reading after close, or164* another I/O error occurs.165* @see java.io.FilterInputStream#in166*/167public final void readFully(byte b[]) throws IOException {168readFully(b, 0, b.length);169}170171/**172* See the general contract of the <code>readFully</code>173* method of <code>DataInput</code>.174* <p>175* Bytes176* for this operation are read from the contained177* input stream.178*179* @param b the buffer into which the data is read.180* @param off the start offset of the data.181* @param len the number of bytes to read.182* @exception EOFException if this input stream reaches the end before183* reading all the bytes.184* @exception IOException the stream has been closed and the contained185* input stream does not support reading after close, or186* another I/O error occurs.187* @see java.io.FilterInputStream#in188*/189public final void readFully(byte b[], int off, int len) throws IOException {190if (len < 0)191throw new IndexOutOfBoundsException();192int n = 0;193while (n < len) {194int count = in.read(b, off + n, len - n);195if (count < 0)196throw new EOFException();197n += count;198}199}200201/**202* See the general contract of the <code>skipBytes</code>203* method of <code>DataInput</code>.204* <p>205* Bytes for this operation are read from the contained206* input stream.207*208* @param n the number of bytes to be skipped.209* @return the actual number of bytes skipped.210* @exception IOException if the contained input stream does not support211* seek, or the stream has been closed and212* the contained input stream does not support213* reading after close, or another I/O error occurs.214*/215public final int skipBytes(int n) throws IOException {216int total = 0;217int cur = 0;218219while ((total<n) && ((cur = (int) in.skip(n-total)) > 0)) {220total += cur;221}222223return total;224}225226/**227* See the general contract of the <code>readBoolean</code>228* method of <code>DataInput</code>.229* <p>230* Bytes for this operation are read from the contained231* input stream.232*233* @return the <code>boolean</code> value read.234* @exception EOFException if this input stream has reached the end.235* @exception IOException the stream has been closed and the contained236* input stream does not support reading after close, or237* another I/O error occurs.238* @see java.io.FilterInputStream#in239*/240public final boolean readBoolean() throws IOException {241int ch = in.read();242if (ch < 0)243throw new EOFException();244return (ch != 0);245}246247/**248* See the general contract of the <code>readByte</code>249* method of <code>DataInput</code>.250* <p>251* Bytes252* for this operation are read from the contained253* input stream.254*255* @return the next byte of this input stream as a signed 8-bit256* <code>byte</code>.257* @exception EOFException if this input stream has reached the end.258* @exception IOException the stream has been closed and the contained259* input stream does not support reading after close, or260* another I/O error occurs.261* @see java.io.FilterInputStream#in262*/263public final byte readByte() throws IOException {264int ch = in.read();265if (ch < 0)266throw new EOFException();267return (byte)(ch);268}269270/**271* See the general contract of the <code>readUnsignedByte</code>272* method of <code>DataInput</code>.273* <p>274* Bytes275* for this operation are read from the contained276* input stream.277*278* @return the next byte of this input stream, interpreted as an279* unsigned 8-bit number.280* @exception EOFException if this input stream has reached the end.281* @exception IOException the stream has been closed and the contained282* input stream does not support reading after close, or283* another I/O error occurs.284* @see java.io.FilterInputStream#in285*/286public final int readUnsignedByte() throws IOException {287int ch = in.read();288if (ch < 0)289throw new EOFException();290return ch;291}292293/**294* See the general contract of the <code>readShort</code>295* method of <code>DataInput</code>.296* <p>297* Bytes298* for this operation are read from the contained299* input stream.300*301* @return the next two bytes of this input stream, interpreted as a302* signed 16-bit number.303* @exception EOFException if this input stream reaches the end before304* reading two bytes.305* @exception IOException the stream has been closed and the contained306* input stream does not support reading after close, or307* another I/O error occurs.308* @see java.io.FilterInputStream#in309*/310public final short readShort() throws IOException {311int ch1 = in.read();312int ch2 = in.read();313if ((ch1 | ch2) < 0)314throw new EOFException();315return (short)((ch1 << 8) + (ch2 << 0));316}317318/**319* See the general contract of the <code>readUnsignedShort</code>320* method of <code>DataInput</code>.321* <p>322* Bytes323* for this operation are read from the contained324* input stream.325*326* @return the next two bytes of this input stream, interpreted as an327* unsigned 16-bit integer.328* @exception EOFException if this input stream reaches the end before329* reading two bytes.330* @exception IOException the stream has been closed and the contained331* input stream does not support reading after close, or332* another I/O error occurs.333* @see java.io.FilterInputStream#in334*/335public final int readUnsignedShort() throws IOException {336int ch1 = in.read();337int ch2 = in.read();338if ((ch1 | ch2) < 0)339throw new EOFException();340return (ch1 << 8) + (ch2 << 0);341}342343/**344* See the general contract of the <code>readChar</code>345* method of <code>DataInput</code>.346* <p>347* Bytes348* for this operation are read from the contained349* input stream.350*351* @return the next two bytes of this input stream, interpreted as a352* <code>char</code>.353* @exception EOFException if this input stream reaches the end before354* reading two bytes.355* @exception IOException the stream has been closed and the contained356* input stream does not support reading after close, or357* another I/O error occurs.358* @see java.io.FilterInputStream#in359*/360public final char readChar() throws IOException {361int ch1 = in.read();362int ch2 = in.read();363if ((ch1 | ch2) < 0)364throw new EOFException();365return (char)((ch1 << 8) + (ch2 << 0));366}367368/**369* See the general contract of the <code>readInt</code>370* method of <code>DataInput</code>.371* <p>372* Bytes373* for this operation are read from the contained374* input stream.375*376* @return the next four bytes of this input stream, interpreted as an377* <code>int</code>.378* @exception EOFException if this input stream reaches the end before379* reading four bytes.380* @exception IOException the stream has been closed and the contained381* input stream does not support reading after close, or382* another I/O error occurs.383* @see java.io.FilterInputStream#in384*/385public final int readInt() throws IOException {386int ch1 = in.read();387int ch2 = in.read();388int ch3 = in.read();389int ch4 = in.read();390if ((ch1 | ch2 | ch3 | ch4) < 0)391throw new EOFException();392return ((ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0));393}394395private byte readBuffer[] = new byte[8];396397/**398* See the general contract of the <code>readLong</code>399* method of <code>DataInput</code>.400* <p>401* Bytes402* for this operation are read from the contained403* input stream.404*405* @return the next eight bytes of this input stream, interpreted as a406* <code>long</code>.407* @exception EOFException if this input stream reaches the end before408* reading eight bytes.409* @exception IOException the stream has been closed and the contained410* input stream does not support reading after close, or411* another I/O error occurs.412* @see java.io.FilterInputStream#in413*/414public final long readLong() throws IOException {415readFully(readBuffer, 0, 8);416return (((long)readBuffer[0] << 56) +417((long)(readBuffer[1] & 255) << 48) +418((long)(readBuffer[2] & 255) << 40) +419((long)(readBuffer[3] & 255) << 32) +420((long)(readBuffer[4] & 255) << 24) +421((readBuffer[5] & 255) << 16) +422((readBuffer[6] & 255) << 8) +423((readBuffer[7] & 255) << 0));424}425426/**427* See the general contract of the <code>readFloat</code>428* method of <code>DataInput</code>.429* <p>430* Bytes431* for this operation are read from the contained432* input stream.433*434* @return the next four bytes of this input stream, interpreted as a435* <code>float</code>.436* @exception EOFException if this input stream reaches the end before437* reading four bytes.438* @exception IOException the stream has been closed and the contained439* input stream does not support reading after close, or440* another I/O error occurs.441* @see java.io.DataInputStream#readInt()442* @see java.lang.Float#intBitsToFloat(int)443*/444public final float readFloat() throws IOException {445return Float.intBitsToFloat(readInt());446}447448/**449* See the general contract of the <code>readDouble</code>450* method of <code>DataInput</code>.451* <p>452* Bytes453* for this operation are read from the contained454* input stream.455*456* @return the next eight bytes of this input stream, interpreted as a457* <code>double</code>.458* @exception EOFException if this input stream reaches the end before459* reading eight bytes.460* @exception IOException the stream has been closed and the contained461* input stream does not support reading after close, or462* another I/O error occurs.463* @see java.io.DataInputStream#readLong()464* @see java.lang.Double#longBitsToDouble(long)465*/466public final double readDouble() throws IOException {467return Double.longBitsToDouble(readLong());468}469470private char lineBuffer[];471472/**473* See the general contract of the <code>readLine</code>474* method of <code>DataInput</code>.475* <p>476* Bytes477* for this operation are read from the contained478* input stream.479*480* @deprecated This method does not properly convert bytes to characters.481* As of JDK 1.1, the preferred way to read lines of text is via the482* <code>BufferedReader.readLine()</code> method. Programs that use the483* <code>DataInputStream</code> class to read lines can be converted to use484* the <code>BufferedReader</code> class by replacing code of the form:485* <blockquote><pre>486* DataInputStream d = new DataInputStream(in);487* </pre></blockquote>488* with:489* <blockquote><pre>490* BufferedReader d491* = new BufferedReader(new InputStreamReader(in));492* </pre></blockquote>493*494* @return the next line of text from this input stream.495* @exception IOException if an I/O error occurs.496* @see java.io.BufferedReader#readLine()497* @see java.io.FilterInputStream#in498*/499@Deprecated500public final String readLine() throws IOException {501char buf[] = lineBuffer;502503if (buf == null) {504buf = lineBuffer = new char[128];505}506507int room = buf.length;508int offset = 0;509int c;510511loop: while (true) {512switch (c = in.read()) {513case -1:514case '\n':515break loop;516517case '\r':518int c2 = in.read();519if ((c2 != '\n') && (c2 != -1)) {520if (!(in instanceof PushbackInputStream)) {521this.in = new PushbackInputStream(in);522}523((PushbackInputStream)in).unread(c2);524}525break loop;526527default:528if (--room < 0) {529buf = new char[offset + 128];530room = buf.length - offset - 1;531System.arraycopy(lineBuffer, 0, buf, 0, offset);532lineBuffer = buf;533}534buf[offset++] = (char) c;535break;536}537}538if ((c == -1) && (offset == 0)) {539return null;540}541return String.copyValueOf(buf, 0, offset);542}543544/**545* See the general contract of the <code>readUTF</code>546* method of <code>DataInput</code>.547* <p>548* Bytes549* for this operation are read from the contained550* input stream.551*552* @return a Unicode string.553* @exception EOFException if this input stream reaches the end before554* reading all the bytes.555* @exception IOException the stream has been closed and the contained556* input stream does not support reading after close, or557* another I/O error occurs.558* @exception UTFDataFormatException if the bytes do not represent a valid559* modified UTF-8 encoding of a string.560* @see java.io.DataInputStream#readUTF(java.io.DataInput)561*/562public final String readUTF() throws IOException {563return readUTF(this);564}565566/**567* Reads from the568* stream <code>in</code> a representation569* of a Unicode character string encoded in570* <a href="DataInput.html#modified-utf-8">modified UTF-8</a> format;571* this string of characters is then returned as a <code>String</code>.572* The details of the modified UTF-8 representation573* are exactly the same as for the <code>readUTF</code>574* method of <code>DataInput</code>.575*576* @param in a data input stream.577* @return a Unicode string.578* @exception EOFException if the input stream reaches the end579* before all the bytes.580* @exception IOException the stream has been closed and the contained581* input stream does not support reading after close, or582* another I/O error occurs.583* @exception UTFDataFormatException if the bytes do not represent a584* valid modified UTF-8 encoding of a Unicode string.585* @see java.io.DataInputStream#readUnsignedShort()586*/587public final static String readUTF(DataInput in) throws IOException {588int utflen = in.readUnsignedShort();589byte[] bytearr = null;590char[] chararr = null;591if (in instanceof DataInputStream) {592DataInputStream dis = (DataInputStream)in;593if (dis.bytearr.length < utflen){594dis.bytearr = new byte[utflen*2];595dis.chararr = new char[utflen*2];596}597chararr = dis.chararr;598bytearr = dis.bytearr;599} else {600bytearr = new byte[utflen];601chararr = new char[utflen];602}603604int c, char2, char3;605int count = 0;606int chararr_count=0;607608in.readFully(bytearr, 0, utflen);609610while (count < utflen) {611c = (int) bytearr[count] & 0xff;612if (c > 127) break;613count++;614chararr[chararr_count++]=(char)c;615}616617while (count < utflen) {618c = (int) bytearr[count] & 0xff;619switch (c >> 4) {620case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:621/* 0xxxxxxx*/622count++;623chararr[chararr_count++]=(char)c;624break;625case 12: case 13:626/* 110x xxxx 10xx xxxx*/627count += 2;628if (count > utflen)629throw new UTFDataFormatException(630"malformed input: partial character at end");631char2 = (int) bytearr[count-1];632if ((char2 & 0xC0) != 0x80)633throw new UTFDataFormatException(634"malformed input around byte " + count);635chararr[chararr_count++]=(char)(((c & 0x1F) << 6) |636(char2 & 0x3F));637break;638case 14:639/* 1110 xxxx 10xx xxxx 10xx xxxx */640count += 3;641if (count > utflen)642throw new UTFDataFormatException(643"malformed input: partial character at end");644char2 = (int) bytearr[count-2];645char3 = (int) bytearr[count-1];646if (((char2 & 0xC0) != 0x80) || ((char3 & 0xC0) != 0x80))647throw new UTFDataFormatException(648"malformed input around byte " + (count-1));649chararr[chararr_count++]=(char)(((c & 0x0F) << 12) |650((char2 & 0x3F) << 6) |651((char3 & 0x3F) << 0));652break;653default:654/* 10xx xxxx, 1111 xxxx */655throw new UTFDataFormatException(656"malformed input around byte " + count);657}658}659// The number of chars produced may be less than utflen660return new String(chararr, 0, chararr_count);661}662}663664665