Java.io.ObjectInputStream Class in Java | Set 1
Note :
Java codes mentioned in this article won’t run on Online IDE as the file used in the code doesn’t exists online. So, to verify the working of the codes, you can copy them to your System and can run it over there.
More Methods of ObjectInputStream Class :
- defaultReadObject() : java.io.ObjectInputStream.defaultReadObject() reads the non-static field of the current class from the Input Stream. We use readObject() method of the serialized class to call this method.
Syntax :
public void defaultReadObject() Parameters : ----------- Return : void Exception : -> IOException : in case of any IO error occurs. -> ClassNotFoundException : if the class of Object(being serialized) is not found -> NotActiveException : if the Stream is not reading.
- readObject() : java.io.ObjectInputStream.readObject() reads an object from the serialized class. This method is used to call the defaultReadObject. If the class is deserialized by default, then it can be overridden using the readObject and writeObject methods.
Syntax :
public void defaultReadObject() Parameters : public final Object readObject() Return : void Exception : -> IOException : in case of any IO error occurs. -> ClassNotFoundException : if the class of Object(being serialized) is not found -> OptionalDataException : if instead of object, primitive data is found. -> InvalidClassException : is there is something wrong with serialized class
- available() : java.io.ObjectInputStream.available() tells the no. of bytes of that can be read without being blocked
Syntax :
public int available() Parameters : ----------- Return : no. of bytes of that can be read without being blocked Exception : -> IOException : in case of any IO error occurs.
- close() : java.io.ObjectInputStream.close() closes the Input Stream and releases all the resources allocated to the Stream
Syntax :
public void close() Parameters : ----------- Return : void Exception : -> IOException : in case of any IO error occurs.
- readShort() : java.io.ObjectInputStream.readShort()reads 16 bit short.
Syntax :
public short readShort() Parameters : public final Object readObject() Return : reads 16 bit short. Exception : -> IOException : in case of any IO error occurs. -> EOFException : if End of stream is reached
- readUTF() : java.io.ObjectInputStream.readUTF()reads String in modified UTF-8 (Unicode Transformation Format) format. UTF -8 means it uses 8-bit blocks to represent a character.
Syntax :
public String readUTF() Parameters : public final Object readObject() Return : reads String in modified UTF-8 (Unicode Transformation Format) format Exception : -> IOException : in case of any IO error occurs.
- skipBytes(int maxlen) : java.io.ObjectInputStream.skipBytes(int maxlen)skips ‘maxlen’ no. of bytes while reading.
Syntax :
public int skipBytes(int maxlen) Parameters : maxlen : max. no. of bytes to be skipped Return : no. of bytes to be skipped Exception : -> IOException : in case of any IO error occurs.
- readFully(byte[] destination) : java.io.ObjectInputStream.readFully(byte[] destination)reads all the bytes from source to the destination array.
Syntax :
public void readFully(byte[] destination) Parameters : destination : the buffer in which the data is to be read Return : returns the 32 bit float read Exception : -> IOException : in case of any IO error occurs. -> EOFException : if End of stream is reached
- readFully(byte[] destination, int offset, int maxlen) : java.io.ObjectInputStream.readFully(byte[] destination, int offset, int maxlen)reads some the bytes (starting from offset to maxlen position) from source to the destination array .
Syntax :
public void readFully(byte[] destination, int offset, int maxlen) Parameters : destination : the buffer in which the data is to be read offset : starting position of the buffer maxlen : max no. of bytes to be read Return : void Exception : -> IOException : in case of any IO error occurs. -> EOFException : if End of stream is reached
- readFields() : java.io.ObjectInputStream.readFields() reads the constant field from the Input Stream and indicates the name.
Syntax :
public ObjectInputStream.GetField readFields() Parameters : ------- Return : GetField object reading the constant fields Exception : -> IOException : in case of any IO error occurs. -> ClassNotFoundException : if class of serialized object is not found
- resolveClass() : java.io.ObjectInputStream.resolveClass(ObjectStreamClass INS_class) loads an instance class to Specified Stream Class in place of it.
Syntax :
protected Class resolveClass(ObjectStreamClass desc) : means that the class object can be of any type, it is to be specified by the coder. Parameters : INS_class : instance of the specified Stream Class Return : Class Object equivalent to the Specified Stream Class Exception : -> IO Exception : if any IO exception occurs -> ClassNotFoundException : if the argumented class is not available.
- registerValidation() : java.io.ObjectInputStream.registerValidation(ObjectInputValidation object, int order) registers the object to validate it.
Syntax :
public void registerValidation(ObjectInputValidation object, int order) Parameters : ------- Return : object : object to be validated order : Controls the order of callback. These are processed in no particular order Exception : -> NotActiveException : If IO stream is not ready to be read -> InvalidObjectException : if the argumented object to be validated is NULL.
- resolveObject(Object o) : java.io.ObjectInputStream.resolveObject(Object o) substitutes an object to ObjectInputStream from another trusted subclass. We can’t replace object until
enableResolveObject is called. It is called after an object has been read.
Syntax :
protected Object resolveObject(Object o) Parameters : o : object we want to substitute Return : --------- Exception : -> IOException : If IO error occurs.
- enableResolveObject() : java.io.ObjectInputStream.enableResolveObject(boolean check) allows resolveObject method to substitute an object to the ObjectInputStream from another trusted class.
enableResolveObject is called. It is called after an object has been read.
Syntax :
protected boolean enableResolveObject(boolean enable) Parameters : check : "true" to allow resolveObject() method Return : ----
- readClassDescriptor() : java.io.ObjectInputStream.readClassDescriptor() reads class descriptor from the serialized stream, this method is called if descriptor is accepted by the ObjectInputStream. By default, descriptor is called by readClassDescriptor() method acc. to the format defined in Object Serialization
Syntax :
protected ObjectStreamClass readClassDescriptor() Parameters : ------- Return : reads class descriptor
- readObjectOverride() : java.io.ObjectInputStream.readObjectOverride() reads object from the ObjectOutputStream using protected no-argument constructor
Syntax :
protected Object readObjectOverride() Parameters : ------- Return : reads object from the Stream
- readStreamHeader() : java.io.ObjectInputStream.readStreamHeader() allows subclass to read and verify their header and verifying the version number
Syntax :
protected void readStreamHeader() Parameters : ------- Return : void
- resolveProxyClass(String[] in_list) : java.io.resolveProxyClass(String[] in_list) returns a proxy class implementing those interfaces which are named in Proxy Class Descriptor. Using this method, we can read descriptors from the dynamic proxy classes
Syntax :
protected Class resolveProxyClass(String[] in_list) Parameters : in_List : interface names list, deserialized in the proxy class descriptor Return : Proxy class for specific Interface mentioned in the list