Java Utililty Methods ByteArrayOutputStream Write

List of utility methods to do ByteArrayOutputStream Write

Description

The list of methods to do ByteArrayOutputStream Write are organized into topic(s).

Method

byte[]readAll(InputStream s)
read All
byte[] temp = new byte[1024];
ByteArrayOutputStream os = new ByteArrayOutputStream();
try {
    while (true) {
        int read = s.read(temp, 0, temp.length);
        if (read <= 0)
            break;
        os.write(temp, 0, read);
...
byte[]readAll(InputStream stream)
Reads all the available bytes in the provided stream to a byte array.
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] buffer = new byte[100];
int len = 0;
while ((len = stream.read(buffer)) != -1)
    outStream.write(buffer, 0, len);
return outStream.toByteArray();
StringreadAllAndClose(InputStream is)
read All And Close
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
    int read;
    while ((read = is.read()) != -1) {
        out.write(read);
} finally {
    try {
...
byte[]readAllAndKeepOpen(InputStream bytes, int bufferSize)
Read all of the bytes in an input stream.
try (ByteArrayOutputStream builder = new ByteArrayOutputStream()) {
    byte[] buffer = new byte[bufferSize];
    int read;
    while ((read = bytes.read(buffer)) != -1) {
        builder.write(buffer, 0, read);
    return builder.toByteArray();
byte[]readAllBinary(InputStream stream)
fully reads a stream
ByteArrayOutputStream o = new ByteArrayOutputStream();
byte[] buffer = new byte[256];
while (true) {
    int bytesRead = stream.read(buffer);
    if (bytesRead == -1)
        break;
    o.write(buffer, 0, bytesRead);
return o.toByteArray();
byte[]readAllFrom(InputStream in, boolean chunked)
read All From
if (in == null)
    throw new RuntimeException("Stream can't be null");
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
    byte[] buf = new byte[1024];
    int len;
    len = in.read(buf);
    while (len > 0) {
...
StringreadAllFromFile(File f)
read All From File
FileInputStream fis = new FileInputStream(f);
byte[] buf = new byte[2048];
int read;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
while ((read = fis.read(buf)) != -1) {
    bos.write(buf, 0, read);
fis.close();
...
byte[]readAllFromFile(String file)
read All From File
FileInputStream fis = null;
try {
    fis = new FileInputStream(file);
    return readAllFrom(fis, false);
} catch (Exception e) {
    throw new RuntimeException("Unable to read file:" + file, e);
} finally {
    if (fis != null) {
...
StringreadAllTextFromResource(String resourceName)
Reads the entire resource text file.
String s = "";
;
InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(resourceName);
try {
    ByteArrayOutputStream result = new ByteArrayOutputStream();
    byte[] buffer = new byte[1024];
    int length;
    while ((length = inputStream.read(buffer)) != -1) {
...