ObjectInputStream setObjectInputFilter(ObjectInputFilter) in Java

setObjectInputFilter(ObjectInputFilter): This method is available in the java.io.ObjectInputStream class of Java.

Syntax:

void java.io.ObjectInputStream.setObjectInputFilter(ObjectInputFilter filter)

This method takes one argument. This method set the serialization filter for the stream.

The filter's checkInput method is called for each class and reference in the stream. The filter can check any or all of the classes, the array length, the number of references, the depth of the graph, and the size of the input stream.

The depth is the number of nested readObject calls starting with the reading of the root of the graph being deserialized and the current object being deserialized.

The number of references is the cumulative number of objects and references to objects already read from the stream including the current object being read.

The filter is invoked only when reading objects from the stream and for not primitives.

Parameters: One parameter is required for this method.

filter: the filter, may be null.

Throws:

1. SecurityException - if there is a security manager and the SerializablePermission("serialFilter") is not granted.

2. IllegalStateException - if the current filter is not null and is not the system-wide filter, or if an object has been read.

Approach

Java

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

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

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

        objectInputStream.setObjectInputFilter(null);
        System.out.println("Successfully set object input filter");
        objectInputStream.close();

    }
}

Output:

Successfully set object input filter


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.

readFully(byte[], int, int)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.

skipBytes(int)This method skips bytes.

No comments:

Post a Comment