Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Performance of DataInputStream\DataOutputStream

I am currently using a buffered streams to read write some files. In between I do some mathematical processing where a symbol is a byte.

To read :

InputStream input = new FileInputStream(outputname)
input.read(byte[] b,int off,int len)

To write :

OutputStream output = new BufferedOutputStream(
                           new FileOutputStream(outputname),
                           OUTPUTBUFFERSIZE
                       )
output.write((byte)byteinsideaint);

Now I need to add some header data, and to support short symbols too. I want to use DataInputStream and DataOutputStream to avoid converting other types to bytes myself and I am wondering if what is their performance.

Do I need to use

OutputStream output = new DataOutputStream(
                             new BufferedOutputStream(
                                  new FileOutputStream(outputname),
                                  OUTPUTBUFFERSIZE
                             ) 
                       );

to keep the advantages of the data buffering or it is good enough to use

OutputStream output = new DataOutputStream(
                           new FileOutputStream(outputname)
                       )
like image 974
UmNyobe Avatar asked Mar 05 '12 14:03

UmNyobe


1 Answers

You should add BufferedOutputStream in between. DataOutputStream does not implement any caching (which is good: separation of concerns) and its performance will be very poor without caching the underlying OutputStream. Even the simplest methods like writeInt() can result in four separate disk writes.

As far as I can see only write(byte[], int, int) and writeUTF(String) are writing data in one byte[] chunk. Others write primitive values (like int or double) byte-by-byte.

like image 189
Tomasz Nurkiewicz Avatar answered Sep 28 '22 00:09

Tomasz Nurkiewicz