Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unknown buffer size to be read from a DataInputStream in java

I have the following statement:

DataInputStream is = new DataInputStream(process.getInputStream());

I would like to print the contents of this input stream but I dont know the size of this stream. How should I read this stream and print it?

like image 719
nikhil Avatar asked Sep 19 '11 18:09

nikhil


People also ask

What is buffer size in Java?

It is best to use buffer sizes that are multiples of 1024 bytes. That works best with most built-in buffering in hard disks etc. Except for adding buffering to your input streams, the Java BufferedOutputStream behaves exactly like an OutputStream .

What is DataInputStream in Java with example?

A data input stream lets an application read primitive Java data types from an underlying input stream in a machine-independent way. An application uses a data output stream to write data that can later be read by a data input stream.

What is the difference between DataInputStream and BufferedInputStream in Java?

DataInputStream is a kind of InputStream to read data directly as primitive data types. BufferedInputStream is a kind of inputStream that reads data from a stream and uses a buffer to optimize speed access to data. data is basicaly read ahead of time and this reduces disk or network access.

Can we open output stream twice in Java?

If you can mark the stream when you read, then call reset() to go back to begin. If you can't you'll have to open a stream again. Another solution would be to convert InputStream to byte array, then iterate over the array as many time as you need.


3 Answers

It is common to all Streams, that the length is not known in advance. Using a standard InputStream the usual solution is to simply call read until -1 is returned.

But I assume, that you have wrapped a standard InputStream with a DataInputStream for a good reason: To parse binary data. (Note: Scanner is for textual data only.)

The JavaDoc for DataInputStream shows you, that this class has two different ways to indicate EOF - each method either returns -1 or throws an EOFException. A rule of thumb is:

  • Every method which is inherited from InputStream uses the "return -1" convention,
  • Every method NOT inherited from InputStream throws the EOFException.

If you use readShort for example, read until an exception is thrown, if you use "read()", do so until -1 is returned.

Tip: Be very careful in the beginning and lookup each method you use from DataInputStream - a rule of thumb can break.

like image 138
A.H. Avatar answered Oct 06 '22 14:10

A.H.


Call is.read(byte[]) repeadely, passing a pre-allocated buffer (you can keep reusing the same buffer). The function will return the number of bytes actually read, or -1 at the end of the stream (in which case, stop):

byte[] buf = new byte[8192];
int nread;
while ((nread = is.read(buf)) >= 0) {
  // process the first `nread` bytes of `buf`
}
like image 45
NPE Avatar answered Oct 06 '22 15:10

NPE


byte[] buffer = new byte[100];
int numberRead = 0;
do{
   numberRead = is.read(buffer);
   if (numberRead != -1){
      // do work here
   }
}while (numberRead == buffer.length);

Keep reading a set buffer size in a loop. If the return value is ever less than the size of the buffer you know you have reached the end of the stream. If the return value is -1, there is no data in the buffer.

DataInputStream.read

like image 41
John B Avatar answered Oct 06 '22 16:10

John B