📄 datainputstream.java
字号:
/* * Copyright 1994-2002 Sun Microsystems, Inc. All Rights Reserved. * * This software is the proprietary information of Sun Microsystems, Inc. * Use is subject to license terms. * */package java.io;/** * A data input stream lets an application read primitive Java data * types from an underlying input stream in a machine-independent * way. An application uses a data output stream to write data that * can later be read by a data input stream. * * @author unascribed * @version 1.31, 12/04/99 (CLDC 1.0, Spring 2000) * @see java.io.DataOutputStream * @since JDK1.0, CLDC 1.0 */publicclass DataInputStream extends InputStream implements DataInput { /** * The input stream. */ protected InputStream in; /** * Creates a <code>DataInputStream</code> * and saves its argument, the input stream * <code>in</code>, for later use. * * @param in the input stream. */ public DataInputStream(InputStream in) { this.in = in; } /** * Reads the next byte of data from this input stream. The value * byte is returned as an <code>int</code> in the range * <code>0</code> to <code>255</code>. If no byte is available * because the end of the stream has been reached, the value * <code>-1</code> is returned. This method blocks until input data * is available, the end of the stream is detected, or an exception * is thrown. * <p> * This method * simply performs <code>in.read()</code> and returns the result. * * @return the next byte of data, or <code>-1</code> if the end of the * stream is reached. * @exception IOException if an I/O error occurs. */ public int read() throws IOException { return in.read(); } /** * See the general contract of the <code>read</code> * method of <code>DataInput</code>. * <p> * Bytes for this operation are read from the contained * input stream. * * @param b the buffer into which the data is read. * @return the total number of bytes read into the buffer, or * <code>-1</code> if there is no more data because the end * of the stream has been reached. * @exception IOException if an I/O error occurs. * @see java.io.InputStream#read(byte[], int, int) */ public final int read(byte b[]) throws IOException { return in.read(b, 0, b.length); } /** * Reads up to <code>len</code> bytes of data from this input stream * into an array of bytes. This method blocks until some input is * available. * <p> * This method simply performs <code>in.read(b, off, len)</code> * and returns the result. * * @param b the buffer into which the data is read. * @param off the start offset of the data. * @param len the maximum number of bytes read. * @return the total number of bytes read into the buffer, or * <code>-1</code> if there is no more data because the end of * the stream has been reached. * @exception IOException if an I/O error occurs. */ public final int read(byte b[], int off, int len) throws IOException { return in.read(b, off, len); } /** * See the general contract of the <code>readFully</code> * method of <code>DataInput</code>. * <p> * Bytes for this operation are read from the contained * input stream. * * @param b the buffer into which the data is read. * @exception EOFException if this input stream reaches the end before * reading all the bytes. * @exception IOException if an I/O error occurs. */ public final void readFully(byte b[]) throws IOException { readFully(b, 0, b.length); } /** * See the general contract of the <code>readFully</code> * method of <code>DataInput</code>. * <p> * Bytes for this operation are read from the contained * input stream. * * @param b the buffer into which the data is read. * @param off the start offset of the data. * @param len the number of bytes to read. * @exception EOFException if this input stream reaches the end before * reading all the bytes. * @exception IOException if an I/O error occurs. */ public final void readFully(byte b[], int off, int len) throws IOException { if (len < 0) { throw new IndexOutOfBoundsException(); } int n = 0; while (n < len) { int count = read(b, off + n, len - n); if (count < 0) { throw new EOFException(); } n += count; } } /** * See the general contract of the <code>skipBytes</code> * method of <code>DataInput</code>. * <p> * Bytes for this operation are read from the contained * input stream. * * @param n the number of bytes to be skipped. * @return the actual number of bytes skipped. * @exception IOException if an I/O error occurs. */ public final int skipBytes(int n) throws IOException { int total = 0; int cur = 0; while ((total<n) && ((cur = (int) skip(n-total)) > 0)) { total += cur; } return total; } /** * See the general contract of the <code>readBoolean</code> * method of <code>DataInput</code>. * <p> * Bytes for this operation are read from the contained * input stream. * * @return the <code>boolean</code> value read. * @exception EOFException if this input stream has reached the end. * @exception IOException if an I/O error occurs. */ public final boolean readBoolean() throws IOException { int ch = read(); if (ch < 0) { throw new EOFException(); } return (ch != 0); } /** * See the general contract of the <code>readByte</code> * method of <code>DataInput</code>. * <p> * Bytes for this operation are read from the contained * input stream. * * @return the next byte of this input stream as a signed 8-bit * <code>byte</code>. * @exception EOFException if this input stream has reached the end. * @exception IOException if an I/O error occurs. */ public final byte readByte() throws IOException { int ch = read(); if (ch < 0) { throw new EOFException(); } return (byte)(ch); } /** * See the general contract of the <code>readUnsignedByte</code> * method of <code>DataInput</code>. * <p> * Bytes for this operation are read from the contained * input stream. * * @return the next byte of this input stream, interpreted as an * unsigned 8-bit number. * @exception EOFException if this input stream has reached the end. * @exception IOException if an I/O error occurs. */ public final int readUnsignedByte() throws IOException { int ch = read(); if (ch < 0) { throw new EOFException(); } return ch; } /** * See the general contract of the <code>readShort</code> * method of <code>DataInput</code>. * <p> * Bytes for this operation are read from the contained * input stream. * * @return the next two bytes of this input stream, interpreted as a * signed 16-bit number. * @exception EOFException if this input stream reaches the end before * reading two bytes. * @exception IOException if an I/O error occurs. */ public final short readShort() throws IOException { return (short)readUnsignedShort(); } /** * See the general contract of the <code>readUnsignedShort</code> * method of <code>DataInput</code>. * <p> * Bytes for this operation are read from the contained * input stream. * * @return the next two bytes of this input stream, interpreted as an * unsigned 16-bit integer. * @exception EOFException if this input stream reaches the end before * reading two bytes. * @exception IOException if an I/O error occurs. */ public final int readUnsignedShort() throws IOException { int ch1 = read();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -