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

static byte[] decompress(byte[] compressed) throws IOException {
    int nRead;/*w  ww .  j a v  a2 s.  com*/
    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);
    }

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

From source file:Main.java

public static byte[] readAll(InputStream is) throws Exception {
    ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
    byte[] buf = new byte[1024];
    int c = is.read(buf);
    while (-1 != c) {
        baos.write(buf, 0, c);
        c = is.read(buf);//ww  w .j a  va2 s. com
    }
    baos.flush();
    baos.close();
    return baos.toByteArray();
}

From source file:io.undertow.testutils.HttpClientUtils.java

public static String readResponse(InputStream stream) throws IOException {

    byte[] data = new byte[100];
    int read;//from ww w  . j  ava 2s.c  o m
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    while ((read = stream.read(data)) != -1) {
        out.write(data, 0, read);
    }
    return new String(out.toByteArray(), StandardCharsets.UTF_8);
}

From source file:Main.java

public static String readFromStream(InputStream in) throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    int len = 0;/* w w w. j  av a 2  s. com*/
    byte[] buf = new byte[1024];
    if ((len = in.read(buf)) != -1) {
        out.write(buf, 0, len);
    }
    String result = out.toString();
    in.close();
    out.close();
    return result;
}

From source file:Main.java

/*************************************************************************************************
 * Reads the stream to the byte array// ww w . j ava  2 s. c  o m
 */
public static byte[] ReadStream(InputStream input) throws IOException {
    final int DEFAULT_INIT_BUF_SIZE = 8192;
    byte[] buffer = new byte[DEFAULT_INIT_BUF_SIZE];
    byte[] arrayOut;
    int bytesRead;

    ByteArrayOutputStream output = new ByteArrayOutputStream();
    while ((bytesRead = input.read(buffer)) != -1) {
        output.write(buffer, 0, bytesRead);
    }
    arrayOut = output.toByteArray();
    output.close();

    return arrayOut;
}

From source file:Main.java

public static byte[] load(InputStream is) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    byte[] buffer = new byte[1024];
    int len;//w w w .  j ava 2s  .co  m
    while ((len = is.read(buffer)) != -1)
        baos.write(buffer, 0, len);
    baos.close();
    is.close();
    return baos.toByteArray();
}

From source file:Main.java

public static byte[] unZip(byte[] bContent) {
    byte[] b = null;
    try {//from www .  j  av a  2 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 byte[] unZip(byte[] data) {
    byte[] b = null;
    try {/*from  w ww  .  j  a va  2 s .c  o m*/
        ByteArrayInputStream bis = new ByteArrayInputStream(data);
        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 byte[] readInput(InputStream in) throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    int len = 0;// w  w w .  jav  a 2  s  . c  om
    byte[] buffer = new byte[1024];
    while ((len = in.read(buffer)) > 0) {
        out.write(buffer, 0, len);
    }
    out.close();
    in.close();
    return out.toByteArray();
}

From source file:Main.java

public static String responseAsString(HttpResponse response) throws IOException {

    StatusLine status = response.getStatusLine();
    if (status.getStatusCode() != HTTP_STATUS_OK) {
        throw new IOException("Invalid response from server! " + status.toString());
    }//from   w  ww .  j av  a2  s.c  o  m

    //do the herdy gerdy and convert the response to a String
    InputStream ist = response.getEntity().getContent();

    ByteArrayOutputStream content = new ByteArrayOutputStream();
    int readCount = 0;
    while ((readCount = ist.read(buff)) != -1)
        content.write(buff, 0, readCount);

    String thePagesContent = EncodingUtils.getString(content.toByteArray(), "UTF-8");

    return thePagesContent;
}