public class

DataInputStream

extends FilterInputStream
implements DataInput
java.lang.Object
   ↳ java.io.InputStream
     ↳ java.io.FilterInputStream
       ↳ java.io.DataInputStream

Class Overview

Wraps an existing InputStream and reads typed data from it. Typically, this stream has been written by a DataOutputStream. Types that can be read include byte, 16-bit short, 32-bit int, 32-bit float, 64-bit long, 64-bit double, byte strings, and strings encoded in modified UTF-8.

See Also

Summary

[Expand]
Inherited Fields
From class java.io.FilterInputStream
Public Constructors
DataInputStream(InputStream in)
Constructs a new DataInputStream on the InputStream in.
Public Methods
final int read(byte[] buffer, int offset, int length)
Reads at most length bytes from this stream and stores them in the byte array buffer starting at offset.
final int read(byte[] buffer)
Reads bytes from this stream into the byte array buffer.
final boolean readBoolean()
Reads a boolean from this stream.
final byte readByte()
Reads an 8-bit byte value from this stream.
final char readChar()
Reads a 16-bit character value from this stream.
final double readDouble()
Reads a 64-bit double value from this stream.
final float readFloat()
Reads a 32-bit float value from this stream.
final void readFully(byte[] buffer)
Reads bytes from this stream into the byte array buffer.
final void readFully(byte[] buffer, int offset, int length)
Reads bytes from this stream and stores them in the byte array buffer starting at the position offset.
final int readInt()
Reads a 32-bit integer value from this stream.
final String readLine()
This method is deprecated. Use BufferedReader
final long readLong()
Reads a 64-bit long value from this stream.
final short readShort()
Reads a 16-bit short value from this stream.
final static String readUTF(DataInput in)
Reads a string encoded in modified UTF-8 from the DataInput stream in.
final String readUTF()
Reads an string encoded in modified UTF-8 from this stream.
final int readUnsignedByte()
Reads an unsigned 8-bit byte value from this stream and returns it as an int.
final int readUnsignedShort()
Reads a 16-bit unsigned short value from this stream and returns it as an int.
final int skipBytes(int count)
Skips count number of bytes in this stream.
[Expand]
Inherited Methods
From class java.io.FilterInputStream
From class java.io.InputStream
From class java.lang.Object
From interface java.io.Closeable
From interface java.io.DataInput

Public Constructors

public DataInputStream (InputStream in)

Since: API Level 1

Constructs a new DataInputStream on the InputStream in. All reads are then filtered through this stream. Note that data read by this stream is not in a human readable format and was most likely created by a DataOutputStream.

Warning: passing a null source creates an invalid DataInputStream. All operations on such a stream will fail.

Parameters
in the source InputStream the filter reads from.

Public Methods

public final int read (byte[] buffer, int offset, int length)

Since: API Level 1

Reads at most length bytes from this stream and stores them in the byte array buffer starting at offset. Returns the number of bytes that have been read or -1 if no bytes have been read and the end of the stream has been reached.

Parameters
buffer the byte array in which to store the bytes read.
offset the initial position in buffer to store the bytes read from this stream.
length the maximum number of bytes to store in buffer.
Returns
  • the number of bytes that have been read or -1 if the end of the stream has been reached.
Throws
IOException if a problem occurs while reading from this stream.

public final int read (byte[] buffer)

Since: API Level 1

Reads bytes from this stream into the byte array buffer. Returns the number of bytes that have been read.

Parameters
buffer the buffer to read bytes into.
Returns
  • the number of bytes that have been read or -1 if the end of the stream has been reached.
Throws
IOException if a problem occurs while reading from this stream.

public final boolean readBoolean ()

Since: API Level 1

Reads a boolean from this stream.

Returns
  • the next boolean value from the source stream.
Throws
EOFException if the end of the filtered stream is reached before one byte has been read.
IOException if a problem occurs while reading from this stream.

public final byte readByte ()

Since: API Level 1

Reads an 8-bit byte value from this stream.

Returns
  • the next byte value from the source stream.
Throws
EOFException if the end of the filtered stream is reached before one byte has been read.
IOException if a problem occurs while reading from this stream.
See Also

public final char readChar ()

Since: API Level 1

Reads a 16-bit character value from this stream.

Returns
  • the next char value from the source stream.
Throws
EOFException if the end of the filtered stream is reached before two bytes have been read.
IOException if a problem occurs while reading from this stream.
See Also

public final double readDouble ()

Since: API Level 1

Reads a 64-bit double value from this stream.

Returns
  • the next double value from the source stream.
Throws
EOFException if the end of the filtered stream is reached before eight bytes have been read.
IOException if a problem occurs while reading from this stream.

public final float readFloat ()

Since: API Level 1

Reads a 32-bit float value from this stream.

Returns
  • the next float value from the source stream.
Throws
EOFException if the end of the filtered stream is reached before four bytes have been read.
IOException if a problem occurs while reading from this stream.

public final void readFully (byte[] buffer)

Since: API Level 1

Reads bytes from this stream into the byte array buffer. This method will block until buffer.length number of bytes have been read.

Parameters
buffer to read bytes into.
Throws
EOFException if the end of the source stream is reached before enough bytes have been read.
IOException if a problem occurs while reading from this stream.

public final void readFully (byte[] buffer, int offset, int length)

Since: API Level 1

Reads bytes from this stream and stores them in the byte array buffer starting at the position offset. This method blocks until length bytes have been read. If length is zero, then this method returns without reading any bytes.

Parameters
buffer the byte array into which the data is read.
offset the offset in buffer from where to store the bytes read.
length the maximum number of bytes to read.
Throws
EOFException if the end of the source stream is reached before enough bytes have been read.
IndexOutOfBoundsException if offset < 0 or length < 0, or if offset + length is greater than the size of buffer.
IOException if a problem occurs while reading from this stream.
NullPointerException if buffer or the source stream are null.

public final int readInt ()

Since: API Level 1

Reads a 32-bit integer value from this stream.

Returns
  • the next int value from the source stream.
Throws
EOFException if the end of the filtered stream is reached before four bytes have been read.
IOException if a problem occurs while reading from this stream.
See Also

public final String readLine ()

Since: API Level 1

This method is deprecated.
Use BufferedReader

Returns a string that contains the next line of text available from the source stream. A line is represented by zero or more characters followed by '\n', '\r', "\r\n" or the end of the stream. The string does not include the newline sequence.

Returns
  • the contents of the line or null if no characters were read before the end of the source stream has been reached.
Throws
IOException if a problem occurs while reading from this stream.

public final long readLong ()

Since: API Level 1

Reads a 64-bit long value from this stream.

Returns
  • the next long value from the source stream.
Throws
EOFException if the end of the filtered stream is reached before eight bytes have been read.
IOException if a problem occurs while reading from this stream.
See Also

public final short readShort ()

Since: API Level 1

Reads a 16-bit short value from this stream.

Returns
  • the next short value from the source stream.
Throws
EOFException if the end of the filtered stream is reached before two bytes have been read.
IOException if a problem occurs while reading from this stream.
See Also

public static final String readUTF (DataInput in)

Since: API Level 1

Reads a string encoded in modified UTF-8 from the DataInput stream in.

Parameters
in the input stream to read from.
Returns
  • the next MUTF-8 encoded string from the source stream.
Throws
IOException if a problem occurs while reading from this stream.

public final String readUTF ()

Since: API Level 1

Reads an string encoded in modified UTF-8 from this stream.

Returns
  • the next MUTF-8 encoded string read from the source stream.
Throws
EOFException if the end of the input is reached before the read request can be satisfied.
IOException if a problem occurs while reading from this stream.

public final int readUnsignedByte ()

Since: API Level 1

Reads an unsigned 8-bit byte value from this stream and returns it as an int.

Returns
  • the next unsigned byte value from the source stream.
Throws
EOFException if the end of the filtered stream has been reached before one byte has been read.
IOException if a problem occurs while reading from this stream.
See Also

public final int readUnsignedShort ()

Since: API Level 1

Reads a 16-bit unsigned short value from this stream and returns it as an int.

Returns
  • the next unsigned short value from the source stream.
Throws
EOFException if the end of the filtered stream is reached before two bytes have been read.
IOException if a problem occurs while reading from this stream.
See Also

public final int skipBytes (int count)

Since: API Level 1

Skips count number of bytes in this stream. Subsequent read()s will not return these bytes unless reset() is used. This method will not throw an EOFException if the end of the input is reached before count bytes where skipped.

Parameters
count the number of bytes to skip.
Returns
  • the number of bytes actually skipped.
Throws
IOException if a problem occurs during skipping.
See Also