Example usage for java.io ByteArrayOutputStream write

List of usage examples for java.io ByteArrayOutputStream write

Introduction

In this page you can find the example usage for java.io ByteArrayOutputStream write.

Prototype

public synchronized void write(byte b[], int off, int len) 

Source Link

Document

Writes len bytes from the specified byte array starting at offset off to this ByteArrayOutputStream .

Usage

From source file:Main.java

public static byte[] unZip(byte[] bContent) throws IOException {
    byte[] b = null;
    try {//from  w w  w  . j av  a2 s  .c  o  m
        ByteArrayInputStream bis = new ByteArrayInputStream(bContent);
        ZipInputStream zip = new ZipInputStream(bis);

        while (zip.getNextEntry() != null) {
            byte[] buf = new byte[1024];
            int num = -1;
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            while ((num = zip.read(buf, 0, buf.length)) != -1) {
                baos.write(buf, 0, num);
            }
            b = baos.toByteArray();
            baos.flush();
            baos.close();
        }
        zip.close();
        bis.close();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return b;
}

From source file:Main.java

public static String readString(InputStream is) throws IOException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    byte[] buffer = new byte[1024];
    int len = -1;
    while ((len = is.read(buffer)) > 0) {
        bos.write(buffer, 0, len);
    }//  w w  w .  j a va  2  s.  com
    bos.close();

    return bos.toString();
}

From source file:Main.java

public static byte[] transStreamToBytes(InputStream is, int buffSize) {
    if (is == null) {
        return null;
    }//from ww w.  j  a v a2s  . c  o m
    if (buffSize <= 0) {
        throw new IllegalArgumentException("buffSize can not less than zero.....");
    }
    byte[] data = null;
    byte[] buffer = new byte[buffSize];
    int actualSize = 0;
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {
        while ((actualSize = is.read(buffer)) != -1) {
            baos.write(buffer, 0, actualSize);
        }
        data = baos.toByteArray();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            baos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return data;
}

From source file:Main.java

public static byte[] streamToBytes(InputStream is) throws IOException {
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    byte[] buffer = new byte[1024];

    int len;/*from ww  w .ja v a 2  s . co m*/
    while ((len = is.read(buffer)) != -1) {
        os.write(buffer, 0, len);
    }
    return os.toByteArray();
}

From source file:Main.java

private static byte[] transStreamToBytes(InputStream is, int buffSize) {
    if (is == null) {
        return null;
    }//  w ww .j  a v  a 2  s.c o m
    if (buffSize <= 0) {
        throw new IllegalArgumentException("buffSize can not less than zero.....");
    }
    byte[] data = null;
    byte[] buffer = new byte[buffSize];
    int actualSize = 0;
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {
        while ((actualSize = is.read(buffer)) != -1) {
            baos.write(buffer, 0, actualSize);
        }
        data = baos.toByteArray();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            baos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return data;
}

From source file:Main.java

public static byte[] getBytesFromInputStream(InputStream in) throws Exception {
    ByteArrayOutputStream outStream = new ByteArrayOutputStream();
    byte[] data = new byte[4096];
    int count;// w w  w  .  j  a v a 2 s .  co  m
    while ((count = in.read(data, 0, 4096)) != -1)
        outStream.write(data, 0, count);
    return outStream.toByteArray();
}

From source file:Main.java

public static byte[] readStream(InputStream is) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    byte[] buf = new byte[1024 * 10];
    int readlen;/* w  w w . ja  v a2s.  c  o m*/
    while ((readlen = is.read(buf)) >= 0) {
        baos.write(buf, 0, readlen);
    }
    baos.close();

    return baos.toByteArray();
}

From source file:Main.java

/**
 * Load an asset from a resource and return the content as byte array.
 *//*  w ww  .ja  va2 s .co m*/
public static byte[] assetAsByteArray(Resources res, String path) throws IOException {
    InputStream is = res.getAssets().open(path);

    ByteArrayOutputStream buf = new ByteArrayOutputStream();
    byte[] temp = new byte[1024];
    int read;

    while ((read = is.read(temp)) > 0) {
        buf.write(temp, 0, read);
    }
    is.close();
    return buf.toByteArray();
}

From source file:Main.java

/**
 * Converts the entirety of an {@link InputStream} to a byte array.
 *
 * @param inputStream the {@link InputStream} to be read. The input stream is not closed by this
 *    method./* ww  w. j ava  2  s  .  c om*/
 * @return a byte array containing all of the inputStream's bytes.
 * @throws IOException if an error occurs reading from the stream.
 */
public static byte[] toByteArray(InputStream inputStream) throws IOException {
    byte[] buffer = new byte[1024 * 4];
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    int bytesRead;
    while ((bytesRead = inputStream.read(buffer)) != -1) {
        outputStream.write(buffer, 0, bytesRead);
    }
    return outputStream.toByteArray();
}

From source file:Main.java

public static byte[] readStream(InputStream inStream) throws Exception {
    ByteArrayOutputStream outStream = new ByteArrayOutputStream();
    byte[] buffer = new byte[1024];
    int len = 0;/*from ww w . jav  a 2s . c om*/
    while ((len = inStream.read(buffer)) != -1) {
        outStream.write(buffer, 0, len);
    }
    outStream.close();
    return outStream.toByteArray();
}