Abstract interface ObjectInput in Java

digitization, transformation, binary

Abstract interface ObjectInput in Java

ObjectInput interface extends the DataInput interface and adds methods for serialization and deserializing objects along with reading bytes and arrays of bytes.
While DataInput defines methods for reading primitive types from a stream, ObjectInput defines methods for reading objects and arrays of bytes.

The structure of the ObjectInput interface is given as

public abstract interface java.io.ObjectInput extends java.io.DataInput{
// Public Instance Methods
public abstract int available() throws IOException;
public abstract void close() throws IOException;
public abstract int read() throws IOException;
public abstract int read(byte [ ] b) throws IOException;
public abstract int read(byte [ ] b, int off, int len) throws IOException;
public abstract Object readObject() throws ClassNotFoundException,IOException;
public abstract long skip(long n) throws IOException;
}

The details of the methods are given as:

Ads code goes here
public abstract int available() throws IOException;

public abstract int available() throws IOException method returns the number of bytes that can be read from the stream without accessing a physical device, like a disk or a network.

This method returns the number of bytes that can be read without blocking.

public abstract void close() throws IOException;

public abstract void close() throws IOException method closes the stream and releases any system resources associated with it.

public abstract int read() throws IOException;

public abstract int read() throws IOException method returns the next byte of data from the stream. The method blocks until the byte are read, the end of the stream is detected, or an exception is thrown.

READ  Class PropertyResourceBundle in Java

This method returns the next byte of data or -1 if the end of the stream is encountered.

public abstract int read(byte [ ] b) throws IOException;

public abstract int read(byte [ ] b) throws IOException method reads bytes from the stream to fill the given array.

The method blocks until some data are available.

This method returns the actual number of bytes read or -1 if the end of the stream is encountered immediately.
Parameter
b- An array of bytes to be filled from the stream.

public abstract int read(byte [ ] b, int off, int len) throws IOException;

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

The method blocks until some data are available.

This method returns the actual number of bytes read or -1 if the end of the stream is encountered immediately.
Parameter
b– An array of bytes to be filled from the stream.
off– An offset into the array.
len– The number of bytes to read.

public abstract Object readObject() throws ClassNotFoundException,IOException;

public abstract Object readObject() throws ClassNotFoundException, IOException method reads and returns an object instance from the stream; in other words, it deserializes an object from the stream. The class that implements this interface determines exactly how the object is to be read.

This method returns an Object that has been deserialized from the stream.

public abstract long skip(long n) throws IOException;

public abstract long skip(long n) throws IOException method skips n bytes of input.

READ  Class UnsatisfiedLinkError in Java

This method returns the actual number of bytes skipped.
Parameter
n-The number of bytes to skip.
Apart from these methods ObjectInput interface also has inherited methods from DataInput. They are as follows:

  • readBoolean()
  • readChar()
  • readFloat(byte[])
  • readFully(byte[], int, int)
  • readLine()
  • readShort()
  • readUnsignedChar()
  • skipBytes(int)
  • readByte()
  • readDouble()
  • readFully(byte[])
  • readInt()
  • readLong()
  • readUnsignedByte()
  • readUTF()

Share and Enjoy !

Leave a Comment

Your email address will not be published. Required fields are marked *