StreamCorruptedException in Java

StreamCorruptedException:

java.io.StreamCorruptedException

Thrown when control information that was read from an object stream violates internal consistency checks.

Example:

Java

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

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

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

        System.out.println(objectInputStream.available());
        objectInputStream.close();

    }
}

Output:

Exception in thread "main" java.io.StreamCorruptedException: invalid stream header: 48656C6C at java.base/java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:966) at java.base/java.io.ObjectInputStream.<init>(ObjectInputStream.java:405)


Constructor of StreamCorruptedException

Example 1:

java.io.StreamCorruptedException.StreamCorruptedException()

Create a StreamCorruptedException and list no reason why thrown.

Java

import java.io.StreamCorruptedException;

public class StreamCorruptedExceptionExample {
    public static void main(String[] args) {

        StreamCorruptedException streamCorruptedException =
new StreamCorruptedException();

        System.out.println(streamCorruptedException);
    }
}

Output:

java.io.StreamCorruptedException


Example 2:

Java

import java.io.StreamCorruptedException;

public class StreamCorruptedExceptionExample {
    public static void main(String[] args) {

        StreamCorruptedException streamCorruptedException =
new StreamCorruptedException("reason");

        System.out.println(streamCorruptedException);
    }
}

Output:

java.io.StreamCorruptedException: reason


Some other Exceptions of java.io

EOFException: Signals that an end of file or end of the stream has been reached unexpectedly during input.

FileNotFoundException: Signals that an attempt to open the file denoted by a specified path name has failed.

InterruptedIOException: Signals that an I/O operation has been interrupted. 

InvalidClassException: Thrown when the Serialization runtime detects.

InvalidObjectException: Indicates that one or more deserialized objects failed validation tests.

IOError: Thrown when a serious I/O error has occurred.

IOException: Signals that an I/O exception of some sort has occurred.

NotActiveException: Thrown when serialization or deserialization is not active.

NotSerializableException: Thrown when an instance is required to have a Serializable interface.

ObjectStreamException: Superclass of all exceptions specific to Object Stream classes.

OptionalDataException: Exception indicating the failure of an object read operation due to unread primitive data, or the end of data belonging to a serialized object in the stream.

SyncFailedException: Signals that a sync operation has failed.

UncheckedIOException: Wraps an IOException with an unchecked exception.

UnsupportedEncodingException: Character Encoding is not supported.

UTFDataFormatException: Signals that a malformed string in modified UTF-8 format has been read in a data input stream or by any class that implements the data input interface.

WriteAbortedException: Signals that one of the ObjectStreamExceptions was thrown during a write operation.

No comments:

Post a Comment