readUnshared(): This method is available in the java.io.ObjectInputStream class of Java.
Syntax:
Object java.io.ObjectInputStream.readUnshared() throws IOException, ClassNotFoundException
This method reads an "unshared" object from the ObjectInputStream. This method is identical to readObject, except that it prevents subsequent calls to readObject and readUnshared from returning additional references to the deserialized instance obtained via this call.
Note:
1. If readUnshared is called to deserialize a back-reference (the stream representation of an object which has been written previously to the stream), an ObjectStreamException will be thrown.
2. If readUnshared returns successfully, then any subsequent attempts to deserialize back-references to the stream handle deserialized by readUnshared will cause an ObjectStreamException to be thrown.
Parameters: NA
Returns: reference to the deserialized object.
Throws:
1. ClassNotFoundException - if the class of an object to deserialize cannot be found.
2. StreamCorruptedException - if control information in the stream is inconsistent.
3. ObjectStreamException - if the object to deserialize has already appeared in the stream.
4. OptionalDataException - if primitive data is next in the stream.
5. IOException - if an I/O error occurs during deserialization.
Approach 1: StreamCorruptedException
Java
package com.ObjectInputStream;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStream;import java.io.ObjectInputStream;public class ObjectInputStreamreadUnshared {public static void main(String[] args)throws IOException, ClassNotFoundException {InputStream in = new FileInputStream("D:\\hell.txt");ObjectInputStream objectInputStream =new ObjectInputStream(in);System.out.println(objectInputStream.readUnshared());objectInputStream.close();}}
Output:
Exception in thread "main" java.io.StreamCorruptedException: invalid stream header: 61686861 at java.base/java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:966) at java.base/java.io.ObjectInputStream.<init>(ObjectInputStream.java:405) at com.ObjectInputStream.ObjectInputStreamreadUnshared.main(ObjectInputStreamreadUnshared.java:12)
Approach 2: OptionalDataException
Java
package com.ObjectInputStream;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStream;import java.io.ObjectInputStream;public class ObjectInputStreamreadUnshared {public static void main(String[] args)throws IOException, ClassNotFoundException {InputStream in = new FileInputStream("D:\\hello.txt");ObjectInputStream objectInputStream =new ObjectInputStream(in);System.out.println(objectInputStream.readUnshared());objectInputStream.close();}}
Output:
Exception in thread "main" java.io.OptionalDataException at java.base/java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1727) at java.base/java.io.ObjectInputStream.readUnshared(ObjectInputStream.java:614) at com.ObjectInputStream.ObjectInputStreamreadUnshared.main(ObjectInputStreamreadUnshared.java:14)
Approach 3: IOException
Java
import java.io.FileInputStream;import java.io.IOException;import java.io.InputStream;import java.io.ObjectInputStream;public class ObjectInputStreamreadUnshared {public static void main(String[] args)throws IOException, ClassNotFoundException {InputStream in = new FileInputStream("D:\\hello.txt");ObjectInputStream objectInputStream =new ObjectInputStream(in);objectInputStream.close();System.out.println(objectInputStream.readUnshared());}}
Output:
Exception in thread "main" java.io.IOException: Stream Closed at java.base/java.io.FileInputStream.read0(Native Method) at java.base/java.io.FileInputStream.read(FileInputStream.java:223) at java.base/java.io.ObjectInputStream$PeekInputStream.peek(ObjectInputStream.java:2892) at java.base/java.io.ObjectInputStream$BlockDataInputStream.peek(ObjectInputStream.java:3219) at java.base/java.io.ObjectInputStream$BlockDataInputStream.peekByte(ObjectInputStream.java:3229) at java.base/java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1663) at java.base/java.io.ObjectInputStream.readUnshared(ObjectInputStream.java:614) at com.ObjectInputStream.ObjectInputStreamreadUnshared.main(ObjectInputStreamreadUnshared.java:15)
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.
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