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:com.aerofs.baseline.http.HttpUtils.java

public static byte[] readStreamToBytes(InputStream in) throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();

    try {/*from   w  w w  .ja v a  2 s .  com*/
        int bytesRead;
        byte[] chunk = new byte[1024];

        while ((bytesRead = in.read(chunk)) != -1) {
            out.write(chunk, 0, bytesRead);
        }

        return out.toByteArray();
    } finally {
        try {
            out.close();
        } catch (IOException e) {
            // noop
        }
    }
}

From source file:ZipDemo.java

public static final String uncompress(final byte[] compressed) throws IOException {
    String uncompressed = "";

    try {/*  w  w  w.j a v  a  2  s  .co m*/
        ByteArrayInputStream bais = new ByteArrayInputStream(compressed);
        GZIPInputStream zis = new GZIPInputStream(bais);

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        int numBytesRead = 0;
        byte[] tempBytes = new byte[DEFAULT_BUFFER_SIZE];
        while ((numBytesRead = zis.read(tempBytes, 0, tempBytes.length)) != -1) {
            baos.write(tempBytes, 0, numBytesRead);
        }

        uncompressed = new String(baos.toByteArray());
    } catch (ZipException e) {
        e.printStackTrace(System.err);
    }

    return uncompressed;
}

From source file:com.bigdata.rockstor.console.RockStorSender.java

private static HttpResp buildHttpResponse(HttpResponse response) {
    HttpResp resp = new HttpResp();
    resp.setStatus(response.getStatusLine().getStatusCode());
    Map<String, String> head = new HashMap<String, String>();
    for (Header header : response.getAllHeaders()) {
        head.put(header.getName(), header.getValue());
    }//from   w  w  w  .j  a  v  a 2s .  com
    resp.setHead(head);

    if ("chunked".equals(head.get("Transfer-Encoding"))
            || (head.containsKey("Content-Length") && !head.get("Content-Length").equals("0"))) {
        try {
            InputStream is = response.getEntity().getContent();
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            int len = 0;
            byte[] b = new byte[4096];
            while ((len = is.read(b)) > 0) {
                baos.write(b, 0, len);
            }
            baos.flush();
            resp.setBody(new String(baos.toByteArray()));
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (NullPointerException e) {
            // HEAD ?
        }
    }
    return resp;
}

From source file:Main.java

public static byte[] readInputStream(InputStream is) {
    ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
    final int blockSize = 8192;
    byte[] buffer = new byte[blockSize];
    int count = 0;
    try {//from ww w  .j  a v  a 2s  .c  o m
        while ((count = is.read(buffer, 0, blockSize)) > 0) {
            byteStream.write(buffer, 0, count);
        }
        return byteStream.toByteArray();
    } catch (Exception e) {
        return null;
    } finally {
        if (is != null) {
            try {
                is.close();
            } catch (Exception e) {
            }
        }
    }
}

From source file:Main.java

public static String compressAndB64EncodeUTF8Bytes(byte[] bytes) throws Exception {

    byte[] input = bytes;

    // Compressor with highest level of compression
    Deflater compressor = new Deflater();
    compressor.setLevel(Deflater.BEST_COMPRESSION);

    // Give the compressor the data to compress
    compressor.setInput(input);// w w w.  ja  va 2  s  . c  o m
    compressor.finish();

    // Create an expandable byte array to hold the compressed data.
    // It is not necessary that the compressed data will be smaller than
    // the uncompressed data.
    ByteArrayOutputStream bos = new ByteArrayOutputStream(input.length);

    // Compress the data
    byte[] buf = new byte[32];
    while (!compressor.finished()) {
        int count = compressor.deflate(buf);
        bos.write(buf, 0, count);
    }
    try {
        bos.close();
    } catch (IOException e) {
    }

    // Get the compressed data
    byte[] compressedData = bos.toByteArray();

    return new String(Base64.encode(compressedData), "UTF-8");
}

From source file:Main.java

public static String readFile(Activity activity, String fileName) {
    AssetManager assets = activity.getAssets();
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    byte buf[] = new byte[1024];
    int len;//from  ww w .j  a v  a 2s .  c  o  m
    try {
        InputStream inputStream = assets.open(fileName);
        while ((len = inputStream.read(buf)) != -1) {
            outputStream.write(buf, 0, len);
        }
        outputStream.close();
        inputStream.close();
        return outputStream.toString();
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}

From source file:UpdateMySqlClobServlet.java

public static String getClobsContentAsString(String urlAsString) throws Exception {
    InputStream content = null;/*w  w w .j av a2  s .  c  o  m*/
    try {
        java.net.URL url = new java.net.URL(urlAsString);
        java.net.URLConnection urlConn = url.openConnection();
        urlConn.connect();
        content = urlConn.getInputStream();

        int BUFFER_SIZE = 1024;
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        int length;
        byte[] buffer = new byte[BUFFER_SIZE];

        while ((length = content.read(buffer)) != -1) {
            output.write(buffer, 0, length);
        }
        return new String(output.toByteArray());

    } finally {
        content.close();
    }
}

From source file:com.micro.rent.common.comm.aio.AsyncClientHttpExchangeFutureCallback.java

public static String readInputStream(InputStream input) throws IOException {
    byte[] buffer = new byte[128];
    int len = 0;//ww  w. ja  v  a 2  s . co m
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    while ((len = input.read(buffer)) >= 0) {
        bytes.write(buffer, 0, len);
    }
    return bytes.toString();
}

From source file:Main.java

public static byte[] readStream(InputStream in) throws IOException {
    byte[] ret = null;
    if (in != null) {
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        byte[] buf = new byte[128];
        int len;/*from w  w  w .  j  a  v  a 2 s  .  co m*/
        while (true) {
            len = in.read(buf);
            if (len == -1) {
                break;
            }
            bout.write(buf, 0, len);
        }
        buf = null;
        ret = bout.toByteArray();
    }
    return ret;
}

From source file:Main.java

public static byte[] readFromFile(File file) throws IOException {
    FileInputStream fin = null;//from w  w  w  . j a  v  a  2s  .  c om
    try {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        fin = new FileInputStream(file);
        byte[] buffer = new byte[8192];
        int read = 0;
        while ((read = fin.read(buffer)) != -1) {
            out.write(buffer, 0, read);
        }
        byte[] data = out.toByteArray();
        out.close();
        return data;
    } finally {
        if (fin != null) {
            try {
                fin.close();
            } catch (IOException e) {
            }
        }
    }
}