Spread the love

Class DataInputStream in Java

Spread the love

DataInputStream in Java

DataInputStream class lets an application read binary representation of primitive java data types in a portable way from an underlying input stream in a machine-dependent way. It is used in the concert with DataOutputStreams so that we can read the primitives from a Stream in a portable fashion.

The DataInputStream class provides methods for reading primitive data types and lines of text from an underlying input stream in a machine-independent manner. Many of the methods of DataInputStream read a single primitive data type, in binary format, from an underlying input stream. All multibyte quantities are assumed to be in a format that stores the most significant byte as the first byte and the least significant byte as the last byte.

Many of this class’s methods read and return a single Java primitive type, in binary format, from the stream.

readUnsignedByte() and readUnsignedShort() read unsigned values and return them as int values, since unsigned byte and short types are not supported in Java.

read() reads data into an array of bytes, blocking until at least some data are available. By contrast, readFully() reads data into an array of bytes, but blocks until all of the requested data become available.

skipBytes() blocks until the specified number of bytes have been read and discarded.
readLine() reads characters from the stream until it encounters a newline, a carriage return, or a newline carriage return pair. The returned string is not terminated with a newline or carriage return. This method is now deprecated.

readUTF() reads a string of Unicode text encoded in a slightly modified version of the UTF-8 “transformation format.” UTF-8 is an ASCII-compatible encoding of Unicode characters that is often used for the transmission and storage of Unicode text. This class uses a modified UTF-8 encoding that never contains embedded null characters.
DataInputStream only reads primitive Java types. Use ObjectInputStream to read object values.

An application uses a data output stream to write data that can later read by a data input stream. Data input streams and data output streams represent Unicode Strings in a format that is a slight modification of UFT-8.

The null byte ‘u0000’ is encoded in a two-byte format rather than one byte, so that the encoded Strings never have embedded null. Only one byte, two bytes, and three bytes are used.

The constructor of DataInputStream takes InputStream as an input to reads primitives. We need to create a DataInputStream by specifying the InputStream that is to be filtered in the call to the constructor.

The class structure of the DataInputStream is given as:

public class java.io.DataInputStream extends java.io.FilterInputStream implements java.io.DataInput{
//constructors
public DataInputStream(InputStream in) //Creates a new DataInputStream to read data from the specified input stream
//methods
public final int read(byte b[])throws IOException;
public final int read(byte b[],int offSet,int length)throws IOException;
public final boolean readBoolean()throws IOException;
public final byte readByte()throws IOException;
public final char readChar()throws IOException;
public final double readDouble()throws IOException;
public final float readFloat()throws IOException;
public final void readFully(byte b[])throws IOException;
public final void readFully(byte b[],int offSet,int length)throws IOException;
public final int readInt()throws IOException;
public final String readLine()throws IOException;
public final long readLong()throws IOException;
public final short readShort()throws IOException;
public final int readUnsignedByte()throws IOException;
public final int readUnsignedShort()throws IOException;
public final String readUTF()throws IOException;
public final int skipBytes(int n)throws IOException;
public static final static String readUTF(DataInput in) throws IOException;
}
 

In addition, all methods may throw an IOException if an IO error occurs apart from their regular Exceptions.
The details of the class structure are given as follows:

public DataInputStream(InputStream in);

public DataInputStream(InputStream in) constructor creates a DataInputStream object that reads from, or wraps, the given input stream.

READ  Learn Abstract Class in Java in a Simple and Fast Manner
READ  Class Double in Java

Parameter
in –  the input stream to use.

public final static String readUTF(DataInput in);

public final static String readUTF(DataInput in) method reads a UTF-8 encoded string from the given DataInput object. To get the number of bytes in the encoded string, the first two bytes are read as an unsigned short value.

Then the following bytes are read and interpreted as UTF-8 encoded bytes; these bytes are converted into characters for the resulting String.

This method blocks until all of the bytes in the encoded string have been read, the end of the stream is encountered, or an exception is thrown.

This method returns the String read from the stream.

Parameter
in – the input stream to use.

public final int read(byte[] b);

public final int read(byte[] b) method reads bytes of input into the given array by calling the read() method of the underlying stream.

The method reads up to b.length bytes of data from the stream. The method blocks until there is some input available.

This method returns the number of bytes read, or -1 if the end of file is encountered immediately.

Parameter
b – An array of bytes to be filled from the stream.

public final int read(byte[] b, int off, int len);

public final int read(byte[] b, int off, int len) method reads up to len bytes of input into the given array starting at index off.

The method reads the bytes by calling the read() method of the underlying stream and blocks until there is some input available.

This method returns the number of bytes read, or -1 if the end of file is encountered immediately.

Parameter
b – An array of bytes to be filled from the stream.
off – An offset into the byte array.
len – The number of bytes to read.

public final boolean readBoolean();

public final boolean readBoolean() method reads a byte as a boolean value from the underlying input stream. A byte that contains a zero is read as false; that which contains any other value is read as true.

The method blocks until the byte is read, the end of the stream is encountered, or an exception is thrown.

This method returns the boolean value read from the stream.

public final byte readByte();

public final byte readByte() method reads a signed 8-bit value–a byte–from the underlying input stream.

The method blocks until the byte is read, the end of the stream is encountered, or an exception is thrown.

This method returns the byte value read from the stream.

public final char readChar();

public final char readChar() method reads a 16-bit Unicode character from the stream. The method reads two bytes from the underlying input stream and then creates a char value, using the first byte read as the most significant byte.

READ  Finally Keyword in Java Simplified

The method blocks until the two bytes are read, the end of the stream is encountered, or an exception is thrown.

This method returns the char value read from the stream.

public final double readDouble();

public final double readDouble() method reads a 64-bit double quantity from the stream. The method reads a long value from the underlying input stream as if using the readLong() method. The long value is then converted to a double using the longBitsToDouble() method in Double. The method blocks until the necessary eight bytes are read, the end of the stream is encountered, or an exception is thrown.

This method returns the double value read from the stream.

public final float readFloat();

public final float readFloat() method reads a 32-bit float quantity from the stream. The method reads an int value from the underlying input stream as if using the readInt() method. The int value is then converted to a float using the intBitsToFloat() method in Float. The method blocks until the necessary four bytes are read, the end of the stream is encountered, or an exception is thrown.

READ  Multimedia Audio in Java Concept Simplified

This method returns the float value read from the stream.

public final void readFully(byte[] b);

public final void readFully(byte[] b) method reads bytes into the given array b until the array is full. The method reads repeatedly from the underlying stream to fill the array. The method blocks until all of the bytes are read, the end of the stream is encountered, or an exception is thrown.

Parameter
b – The array to fill.

public final void readFully(byte[] b, int off, int len);

public final void readFully(byte[] b, int off, int len) method reads len bytes into the given array, starting at offset off. The method reads repeatedly from the underlying stream to fill the array. The method blocks until all the bytes are read, the end of the stream is encountered, or an exception is thrown.

Parameter
b – The array to fill.
off – An offset into the array.
len – The number of bytes to read.

public final int readInt();

public final int readInt() method reads a signed 32-bit int quantity from the stream. The method reads four bytes from the underlying input stream and then creates an int quantity, using the first byte read as the most significant byte. The method blocks until the four bytes are read, the end of the stream is encountered, or an exception is thrown.

This method returns the int value read from the stream.

public final String readLine();

public final String readLine() method reads the next line of text from the stream. The method reads bytes of data from the underlying input stream until it encounters a line terminator. A line terminator is a carriage return (“\r”), a newline character (“\n”), a carriage return immediately followed by a newline character or the end of the stream.

The method blocks until a line terminator is read, the end of the stream is encountered, or an exception is thrown.

READ  Class AWTException in Java

This method is deprecated as of JDK 1.1 because it does not convert bytes to characters correctly. It’s replaced by BufferedReader.readLine().

This method returns a String that contains the line read from the stream.

public final long readLong();

public final long readLong() method reads a signed 64-bit long quantity from the stream. The method reads eight bytes from the underlying input stream and then creates a long quantity, using the first byte read as the most significant byte.

The method blocks until the eight bytes are read, the end of the stream is encountered, or an exception is thrown.

This method returns the long value read from the stream.

public final short readShort();

public final short readShort() method reads a signed 16-bit short quantity from the stream. The method reads two bytes from the underlying input stream and then creates a short quantity, using the first byte read as the most significant byte.

The method blocks until the two bytes are read, the end of the stream is encountered, or an exception is thrown.

This method returns the short value read from the stream.
Parameter

public final int readUnsignedByte();

public final int readUnsignedByte() method reads an unsigned 8-bit quantity from the stream.

The method reads a byte from the underlying input stream and returns that byte, and blocks until the byte is read, the end of the stream is encountered, or an exception is thrown.

This method returns the unsigned byte value read from the stream.

public final int readUnsignedShort();

public final int readUnsignedShort() method reads an unsigned 16-bit quantity from the stream. The method reads two bytes from the underlying input stream and creates an unsigned short quantity, using the first byte read as the most significant byte.

READ  Learn DataTypes in Java Quick and Easy Way

The method blocks until the two bytes are read, the end of the stream is encountered, or an exception is thrown.

This method returns the unsigned short value read from the stream.

public final String readUTF();

public final String readUTF() method reads a UTF-8 encoded string from the stream. See the description of the readUTF(DataInput) class method for more information.

This method returns the String read from the stream.

public final int skipBytes(int n);

public final int skipBytes(int n) method skips over n bytes in the underlying input stream. The method blocks until all of the bytes are skipped, the end of the stream is encountered, or an exception is thrown.

This method returns the actual number of skipped bytes.

Parameter
n – The number of bytes to skip.
Apart from these DataInputStream class also has inherited methods from class- Object. They are as follows:

  • clone()
  • finalize()
  • hashCode()
  • notifyAll()
  • wait()
  • wait(long, int)
  • equals(Object)
  • getClass()
  • notify()
  • toString()
  • wait(long)

DataInputStream class also has inherited methods from class- FilterInputStream. They are as follows:

  • available()
  • close()
  • markSupported()
  • reset()
  • mark(int)
  • read()
  • skip(long)

How to read data using DataInputStream?

The below code will tell us how to read data using DataInputStream.

package testPackage;
import java.io.DataInputStream;
public class ReadData {
	public static void main(String[] args) {
		DataInputStream in= new DataInputStream(System.in);
		int intNumber=0;
		float floatNumber=0.0f;
		try {
			System.out.println("enter an integer");
			intNumber=Integer.parseInt(in.readLine());
			System.out.println("enter a float number");
			floatNumber=Float.valueOf(in.readLine()).floatValue();
		}
		catch(Exception e) {}
			System.out.println("int Number"+intNumber);
			System.out.println("float Number"+floatNumber);
	}
}




Spread the love
Animesh Chatterjeehttps://techtravelhub.com/
I am the founder and owner of the blog - TechTravelHub.com, always love to share knowledge on test automation,tools,techniques and tips.I am a passionate coder of Java and VBScript.I also publish articles on Travel ideas and great honeymoon destinations.Apart from these, I am a gear-head,love to drive across India. I have shared lots of articles here on How to travel several parts of India.Customization of cars aka car modification is my another hobby.Get in touch with me on ani01104@gamil.com

Related Articles

Learn How to Use Jacoco Java code Coverage

Introduction to Jacoco Jacoco provides a wide range of coverage(analysis of instructions,...

Comments

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Recent Posts

Learn Quickly About DevOps Tools in a Fast and Efficient Manner

Introduction to DevOps tools DevOps tools are a collection of...

How to Collaborate between Development and Operations Team Fast and Effectively

Collaborate between Development and Operations Teams are the key...

Learn How to Use Jacoco Java code Coverage

Introduction to Jacoco Jacoco provides a wide range of coverage(analysis...

EDITOR PICKS


Spread the love