I am deserializing an object from a file that is 350KB in size, and its taking rather a long time. My computer science TA told me that there is a way to use a Buffered reader along with the ObjectInputStream to greatly increase performance. I however can not find anything about this on Google.
ObjectInputStream ensures that the types of all objects in the graph created from the stream match the classes present in the Java Virtual Machine. Classes are loaded as required using the standard mechanisms. Only objects that support the java. io.
1. The read() method of BufferedReader class in Java is used to read a single character from the given buffered reader. This read() method reads one character at a time from the buffered stream and return it as an integer value.
Return Value This method returns the object read from the stream.
An ObjectOutputStream writes primitive data types and graphs of Java objects to an OutputStream. The objects can be read (reconstituted) using an ObjectInputStream. Persistent storage of objects can be accomplished by using a file for the stream.
You use decoration to buffer the input stream. Like this
InputStream in = ...; // your underlying stream (e.g. FileInputStream)
ObjectInputStream oin = new ObjectInputStream(new BufferedInputStream(in));
This will ensure that each call to ObjectInputStream does not call the base stream in
, such as the OS's file read system call. Instead, each call goes to the buffered input stream, which fetches and caches blocks of data (8K by default), and reads from that. This is faster, since reading from the stream is now a local method call in java, and the method call overhead of a system call is encountered less often. Cache coherence and JIT optimizations also come into play in improving performance.
No but You can use ObjectInputStream(InputStream in) constructor
To create buffered object intput stream by passing BufferedInputStream as argument to above constructor.
Here is example for reading serialized objects from file:
InputStream file = null;
try {
file = new FileInputStream("Out.test");
InputStream buffer = new BufferedInputStream(file);
ObjectInputStream in = new ObjectInputStream(buffer);
vector = (Vector)in.readObject();
} catch (Exception e) {
e.printStackTrace();
} finally{
if(file != null ) {
file.close();
}
}
Checkout following link :
http://java.sun.com/docs/books/performance/1st_edition/html/JPIOPerformance.fm.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With