The following code snippet reads 5 bytes from the InputStream (in) and stores them in the byte array (input). If end of stream is detected, the loop is terminated early:
byte[] input = new byte[5];
for (int i = 0; i < input.length; i++) {
int b = in.read( );
if (b == -1) break;
input[i] = (byte) b;
}
Note: read( ) reads a byte but it returns an int.