Convert Contents Of A ByteArrayInputStream To String

A ByteArrayOutputStream can read from any InputStream and at the end yield a byte[]. However with a ByteArrayInputStream it is simpler: int n = in.available(); byte[] bytes = new byte[n]; in.read(bytes, 0, n); String s = new String(bytes, StandardCharsets.UTF_8); // Or any encoding. For a ByteArrayInputStream available() yields the total number of bytes. Addendum 2021-11-16 … Read more