ObjectInputStream readFully(byte[], int, int) in Java

readFully(byte[], int, int): This method is available in the java.io.ObjectInputStream class of Java.

Syntax:

void java.io.ObjectInputStream.readFully(byte[] buf, int off, int len) throws IOException

This method takes three arguments. This method reads bytes, blocking until all bytes are read.

Parameters: Three parameters are required for this method.

buf: the buffer into which the data is read.

off: the start offset into the data array buf.

len: the maximum number of bytes to read.

Throws:

1. NullPointerException - If buf is null.

2. IndexOutOfBoundsException - If off is negative, len is negative, or len is greater than buf.length - off.

3. EOFException - If the end of the file is reached.

4. IOException - If an  I/O error has occurred.

Approach 1: When no exception

Java

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;

public class ObjectInputStreamreadFully2 {
    public static void main(String[] args) throws IOException {

        InputStream in = new FileInputStream("D:\\hello.txt");
        ObjectInputStream objectInputStream =
new ObjectInputStream(in);

        byte buf[] = { 'a', 'b', 'c', 'd', 'e', 'f' };
        int off = 0, len = 4;
        objectInputStream.readFully(buf, off, len);
        System.out.println("Successfully read fully");
        objectInputStream.close();

    }
}

Output:

Successfully read fully


Approach 2: NullPointerException

Java

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;

public class ObjectInputStreamreadFully2 {
    public static void main(String[] args) throws IOException {

        InputStream in = new FileInputStream("D:\\hello.txt");
        ObjectInputStream objectInputStream =
new ObjectInputStream(in);

        byte buf[] = null;
        int off = 0, len = 4;
        objectInputStream.readFully(buf, off, len);
        System.out.println("Successfully read fully");
        objectInputStream.close();

    }
}

Output:

Exception in thread "main" java.lang.NullPointerException: Cannot read the array length because "buf" is null at java.base/java.io.ObjectInputStream.readFully(ObjectInputStream.java:1199) at com.ObjectInputStream.ObjectInputStreamreadFully2.main(ObjectInputStreamreadFully2.java:16)


Approach 3: IndexOutOfBoundsException

Java

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;

public class ObjectInputStreamreadFully2 {
    public static void main(String[] args) throws IOException {

        InputStream in = new FileInputStream("D:\\hello.txt");
        ObjectInputStream objectInputStream =
new ObjectInputStream(in);

        byte buf[] = { 'a', 'b', 'c', 'd', 'e', 'f' };
        int off = 0, len = 14;
        objectInputStream.readFully(buf, off, len);
        System.out.println("Successfully read fully");
        objectInputStream.close();

    }
}

Output:

Exception in thread "main" java.lang.IndexOutOfBoundsException at java.base/java.io.ObjectInputStream.readFully(ObjectInputStream.java:1200) at com.ObjectInputStream.ObjectInputStreamreadFully2.main(ObjectInputStreamreadFully2.java:16)


Approach 4: EOFException 

Java

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;

public class ObjectInputStreamreadFully2 {
    public static void main(String[] args) throws IOException {

        InputStream in = new FileInputStream("D:\\hello.txt");
        ObjectInputStream objectInputStream =
new ObjectInputStream(in);

        byte buf[] = { 'a', 'b', 'c', 'd', 'e', 'f' };
        int off = 0, len = 4;

        objectInputStream.close();
        objectInputStream.readFully(buf, off, len);
        System.out.println("Successfully read fully");

    }
}

Output:

Exception in thread "main" java.io.EOFException at java.base/java.io.ObjectInputStream$BlockDataInputStream.readFully(ObjectInputStream.java:3377) at java.base/java.io.ObjectInputStream.readFully(ObjectInputStream.java:1202) at com.ObjectInputStream.ObjectInputStreamreadFully2.main(ObjectInputStreamreadFully2.java:18)


Some more methods of ObjectInputStream

available(): This method returns the number of bytes that can be read without blocking.

close(): This method closes the input stream. Must be called to release any resources associated with the stream.

defaultReadObject(): This method read the non-static and non-transient fields of the current class from this stream.

getObjectInputFilter(): This method returns the serialization filter for this stream.

read(): This method reads a byte of data.

read(byte[], int, int): This method reads into an array of bytes.

readBoolean(): This method reads in a boolean.

readByte(): This method reads an 8-bit byte.

readChar(): This method reads a 16-bit char.

readDouble(): This method reads a 64-bit double.

readFields(): This method reads the persistent fields from the stream and makes them available by name.

readFloat(): This method reads a 32-bit float.

readFully(byte[]): This method reads bytes, blocking until all bytes are read.

readInt(): This method reads a 32-bit int.

readLong(): This method reads 64-bit long.

readObject(): This method read an object from the ObjectInputStream.

readShort(): This method reads a 16-bit short.

readUnshared(): This method reads an "unshared" object from the ObjectInputStream.

readUnsignedByte(): This method reads an unsigned 8-bit byte.

readUnsignedShort(): This method reads an unsigned 16-bit short.

readUTF(): This method reads a String in modified UTF-8 format.

registerValidation(ObjectInputValidation, int): This method registers an object to be validated before the graph is returned.

setObjectInputFilter(ObjectInputFilter): This method set the serialization filter for the stream.

skipBytes(int): This method skips bytes.

No comments:

Post a Comment