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

private static byte[] readInputStream(InputStream inputStream) throws IOException {
    byte[] buffer = new byte[8192];
    int len = 0;/*from  w  w  w.ja  va  2s . c  o m*/
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    while ((len = inputStream.read(buffer)) != -1) {
        bos.write(buffer, 0, len);
    }
    bos.close();
    return bos.toByteArray();
}

From source file:Main.java

private static String toString(InputStream is) throws IOException {

    byte[] bytes = new byte[1024];
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    int lidos;//from   w w w .  j  a va 2 s .co  m
    while ((lidos = is.read(bytes)) > 0) {
        baos.write(bytes, 0, lidos);
    }
    return new String(baos.toByteArray());
}

From source file:Main.java

/**
 * Convert input stream to string//from www . j ava 2 s  . co m
 * 
 * @param input
 * @return
 * @throws IOException
 */
public static byte[] getBytes(InputStream input) throws IOException {
    byte[] buffer = new byte[8192];
    int bytesRead;
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    while ((bytesRead = input.read(buffer)) != -1) {
        output.write(buffer, 0, bytesRead);
    }
    return output.toByteArray();
}

From source file:Main.java

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

}

From source file:org.apache.jetspeed.util.Base64.java

public static String encodeAsString(byte[] plaindata) throws Exception {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    ByteArrayOutputStream inStream = new ByteArrayOutputStream();

    inStream.write(plaindata, 0, plaindata.length);

    // pad/*from   www  .  j  av a  2  s .  c o m*/
    if ((plaindata.length % 3) == 1) {
        inStream.write(0);
        inStream.write(0);
    } else if ((plaindata.length % 3) == 2) {
        inStream.write(0);
    }

    inStream.writeTo(MimeUtility.encode(out, "base64"));
    return out.toString();
}

From source file:Main.java

public static byte[] readInputStream(InputStream is) throws IOException {
    byte[] buffer = new byte[1024];
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    int len;/*from w ww  .  j a  va 2  s .  c  om*/
    while ((len = is.read(buffer)) != -1) {
        baos.write(buffer, 0, len);
    }
    baos.flush();
    baos.close();
    is.close();
    return baos.toByteArray();
}

From source file:Main.java

public static byte[] getFileByte(InputStream is) {
    try {//from w ww  .j  a  v a2  s.  com
        byte[] buffer = new byte[1024];
        int len = -1;
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        while ((len = is.read(buffer)) != -1) {
            outStream.write(buffer, 0, len);
        }
        byte[] data = outStream.toByteArray();
        outStream.close();
        is.close();
        return data;
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static byte[] readToEndAsArray(InputStream input) throws IOException {
    DataInputStream dis = new DataInputStream(input);
    byte[] stuff = new byte[1024];
    ByteArrayOutputStream buff = new ByteArrayOutputStream();
    int read = 0;
    while ((read = dis.read(stuff)) != -1) {
        buff.write(stuff, 0, read);
    }//w  w w . j av  a2 s  .c  o m
    dis.close();
    return buff.toByteArray();
}

From source file:Main.java

public static byte[] unGzip(byte[] data) {
    byte[] b = null;
    try {//from   w w  w  .  j a v  a 2s.  c  om
        ByteArrayInputStream bis = new ByteArrayInputStream(data);
        GZIPInputStream gzip = new GZIPInputStream(bis);
        byte[] buf = new byte[1024];
        int num = -1;
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        while ((num = gzip.read(buf, 0, buf.length)) != -1) {
            baos.write(buf, 0, num);
        }
        b = baos.toByteArray();
        baos.flush();
        baos.close();
        gzip.close();
        bis.close();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return b;
}

From source file:Main.java

static byte[] decompress(byte[] compressed) throws IOException {
    int nRead;//from   ww  w . j  av  a2 s.  c om
    byte[] data = new byte[2048];
    ByteArrayInputStream bis = new ByteArrayInputStream(compressed);
    GZIPInputStream gzip = new GZIPInputStream(bis);
    ByteArrayOutputStream buffer = new ByteArrayOutputStream();

    while ((nRead = gzip.read(data, 0, data.length)) != -1) {
        buffer.write(data, 0, nRead);
    }

    bis.close();
    gzip.close();
    return buffer.toByteArray();
}