Java.io.ObjectInputStream Class in Java | Set 2
Last Updated :
12 Dec, 2021
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
Similar Reads
Java Tutorial Java is a high-level, object-oriented programming language used to build web apps, mobile applications, and enterprise software systems. It is known for its Write Once, Run Anywhere capability, which means code written in Java can run on any device that supports the Java Virtual Machine (JVM).Java s
10 min read
Java OOP(Object Oriented Programming) Concepts Java Object-Oriented Programming (OOPs) is a fundamental concept in Java that every developer must understand. It allows developers to structure code using classes and objects, making it more modular, reusable, and scalable.The core idea of OOPs is to bind data and the functions that operate on it,
13 min read
Java Interview Questions and Answers Java is one of the most popular programming languages in the world, known for its versatility, portability, and wide range of applications. Java is the most used language in top companies such as Uber, Airbnb, Google, Netflix, Instagram, Spotify, Amazon, and many more because of its features and per
15+ min read
Arrays in Java Arrays in Java are one of the most fundamental data structures that allow us to store multiple values of the same type in a single variable. They are useful for storing and managing collections of data. Arrays in Java are objects, which makes them work differently from arrays in C/C++ in terms of me
15+ min read
Collections in Java Any group of individual objects that are represented as a single unit is known as a Java Collection of Objects. In Java, a separate framework named the "Collection Framework" has been defined in JDK 1.2 which holds all the Java Collection Classes and Interface in it. In Java, the Collection interfac
15+ min read
Inheritance in Java Java Inheritance is a fundamental concept in OOP(Object-Oriented Programming). It is the mechanism in Java by which one class is allowed to inherit the features(fields and methods) of another class. In Java, Inheritance means creating new classes based on existing ones. A class that inherits from an
13 min read
Java Exception Handling Exception handling in Java allows developers to manage runtime errors effectively by using mechanisms like try-catch block, finally block, throwing Exceptions, Custom Exception handling, etc. An Exception is an unwanted or unexpected event that occurs during the execution of a program, i.e., at runt
10 min read
Java Programs - Java Programming Examples In this article, we will learn and prepare for Interviews using Java Programming Examples. From basic Java programs like the Fibonacci series, Prime numbers, Factorial numbers, and Palindrome numbers to advanced Java programs.Java is one of the most popular programming languages today because of its
8 min read
Java Interface An Interface in Java programming language is defined as an abstract type used to specify the behaviour of a class. An interface in Java is a blueprint of a behaviour. A Java interface contains static constants and abstract methods. Key Properties of Interface:The interface in Java is a mechanism to
12 min read
Introduction to Java Java is a high-level, object-oriented programming language developed by Sun Microsystems in 1995. It is platform-independent, which means we can write code once and run it anywhere using the Java Virtual Machine (JVM). Java is mostly used for building desktop applications, web applications, Android
4 min read