ObjectOutputStream useProtocolVersion(int) in Java

useProtocolVersion(int): This method is available in the java.io.ObjectOutputStream class of Java.

Syntax:

void java.io.ObjectOutputStream.useProtocolVersion(int version) throws IOException

This method takes one argument. This method specifies the stream protocol version to use when writing the stream.

This routine provides a hook to enable the current version of serialization to write in a format that is backward compatible with a previous version of the stream format.

Parameters: One parameter is required for this method.

version: use ProtocolVersion from java.io.ObjectStreamConstants.

Throws:

1. IllegalStateException - if called after any objects have been serialized.

2. IllegalArgumentException - if the invalid version is passed in.

3. IOException - if I/O errors occur

Approach 1: When no exception

Java

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.ObjectStreamConstants;

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

        FileOutputStream fileStream = new FileOutputStream("D:\\hello.txt");

        ObjectOutputStream objStream = new ObjectOutputStream(fileStream);

        int version = ObjectStreamConstants.PROTOCOL_VERSION_1;
        objStream.useProtocolVersion(version);
        System.out.println("Protocol version");

        objStream.close();
    }
}

Output:

Protocol version


Approach 2: IllegalArgumentException

Java

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;

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

        FileOutputStream fileStream = new FileOutputStream("D:\\hello.txt");

        ObjectOutputStream objStream = new ObjectOutputStream(fileStream);

        int version = 0;
        objStream.useProtocolVersion(version);
        System.out.println("Protocol version");

        objStream.close();
    }
}

Output:

Exception in thread "main" java.lang.IllegalArgumentException: unknown version: 0 at java.base/java.io.ObjectOutputStream.useProtocolVersion(ObjectOutputStream.java:321) at com.ObjectOutputStream.ObjectOutputStreamuseProtocolVersion.main(ObjectOutputStreamuseProtocolVersion.java:15)


Some more methods of ObjectOutputStream

close()This method must be called to release any resources associated with the stream.

defaultWriteObject()This method writes the non-static and non-transient fields of the current class to this stream.

flush()This method flushes the stream.

putFields()This method retrieves the object used to buffer persistent fields to be written to the stream.

reset()The state is reset to be the same as a new ObjectOutputStream.

write(byte[])This method writes an array of bytes. 

write(int)This method writes a byte.

write(byte[], int, int)This method writes a sub array of bytes.

writeBoolean(boolean)This method writes a boolean.

writeByte(int)This method writes an 8-bit byte.

writeBytes(String)This method writes a String as a sequence of bytes.

writeChar(int)This method writes a 16-bit char.

writeChars(String)This method writes a String as a sequence of chars.

writeDouble(double)This method writes a 64-bit double.

writeFields()This method writes the buffered fields to the stream.

writeFloat(float)This method writes a 32-bit float.

writeInt(int)This method writes a 32-bit int.

writeLong(long)This method writes a 64-bit long.

writeObject(Object)This method writes the specified object to the ObjectOutputStream.

writeShort(int)This method writes a 16-bit short.

writeUnshared(Object)This method writes an "unshared" object to the ObjectOutputStream.

writeUTF(String)This method is primitive data are written of this String in modified UTF-8 format.

No comments:

Post a Comment