Java Streams  «Prev 

Obtaining desired Byte Array

Getting as many bytes as you want

Reads do not always succeed in getting as many bytes as you want.
The following code loops repeatedly until it either fills the array or sees the end-of-stream:

try{
 byte[] b = new byte[100];
 int offset = 0;
 while (offset < b.length) {
  int bytesRead = System.in.read(b, offset, b.length - offset);
  if (bytesRead == -1) 
   break; // end-of-stream
  offset += bytesRead;
 } // end -while
}// end -try
catch (IOException e) {
 System.err.println("Couldn't read from System.in!");
}