简体   繁体   中英

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)
                       )

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.

You absolutely need the BufferedOutputStream in the middle.

I appreciate your concern about the performance, and I have 2 suggestions:

  1. Shrink your streams with java compression. Useful article can be found here .
  2. Use composition instead of inheritance (which is recommended practice anyway). Create a Pipe which contains a PipedInputStream and a PipedOutputStream connected to each other, with getInputStream() and getOutputStream() methods.You can't directly pass the Pipe object to something needing a stream, but you can pass the return value of it's get methods to do it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM