Various ways to read bytes from an input stream in Java
September 3, 2016 Leave a comment
Say you want to fill a byte array from a source, e.g. a from a randomly simulated data of 50 bytes like the following:
byte[] simulatedSource = new byte[50]; Random random = new Random(); random.nextBytes(simulatedSource); InputStream inputStream = new ByteArrayInputStream(simulatedSource);
At a basic level we can read each byte one by one from the input stream as follows: