Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does a Buffered ObjectInputStream exist?

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.

like image 775
Aymon Fournier Avatar asked Jul 29 '10 17:07

Aymon Fournier


People also ask

How does ObjectInputStream work?

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.

Which of this method is used with buffer reader object to read a single character?

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.

What is the return type of Readobject () method?

Return Value This method returns the object read from the stream.

What is Java ObjectOutputStream?

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.


2 Answers

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.

like image 199
mdma Avatar answered Oct 16 '22 18:10

mdma


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

like image 28
YoK Avatar answered Oct 16 '22 20:10

YoK