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[] decompressInZlib(byte[] compressData, int offset, int length) throws Exception {

    Inflater decompresser = new Inflater();
    decompresser.setInput(compressData, 0, compressData.length);

    ByteArrayOutputStream bos = new ByteArrayOutputStream(length);

    int count;//  w ww.  j a v  a 2s  .  co m
    byte[] buf = new byte[1024];
    while (!decompresser.finished()) {
        count = decompresser.inflate(buf);
        bos.write(buf, 0, count);
    }

    byte[] originalData = bos.toByteArray();
    decompresser.end();

    return originalData;
}

From source file:Main.java

public static byte[] compressInZlib(byte[] originalData, int offset, int length) throws IOException {

    Deflater compresser = new Deflater();
    compresser.setInput(originalData, offset, length);
    compresser.finish();/*w  ww .j  a  v  a  2 s .c  om*/

    ByteArrayOutputStream bos = new ByteArrayOutputStream(length);

    int count;
    byte[] buf = new byte[1024];
    while (!compresser.finished()) {
        count = compresser.deflate(buf);
        bos.write(buf, 0, count);
    }
    compresser.end();

    byte[] compressData = bos.toByteArray();
    bos.close();

    return compressData;
}

From source file:Main.java

public static byte[] readFileToByteArray(File file) throws IOException {
    InputStream inputStream = null;
    try {// w  w  w .  j a  v a 2s . c om
        inputStream = new FileInputStream(file);
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024 * 4];
        int n = 0;
        while (-1 != (n = inputStream.read(buffer))) {
            output.write(buffer, 0, n);
        }
        return output.toByteArray();
    } finally {
        try {
            if (inputStream != null) {
                inputStream.close();
            }
        } catch (IOException e) {
            // Do nothing
        }
    }
}

From source file:simple.crawler.http.HttpClientUtil.java

public static String getContentBodyAsString(HttpResponse res) throws IOException {
    InputStream is = res.getEntity().getContent();
    BufferedInputStream bis = new BufferedInputStream(is);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    byte[] buff = new byte[1024];
    for (int l = bis.read(buff); l != -1; l = bis.read(buff)) {
        baos.write(buff, 0, buff.length);
        buff = new byte[1024];
    }//from   w  w  w. j a  va2 s  .com
    return new String(baos.toByteArray(), "UTF-8");
}

From source file:Main.java

private static String changeInputStream(InputStream inputStream) {

    String jsonString = "";
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    int len = 0;/*from   w w w  . jav  a  2s.  c o  m*/
    byte[] data = new byte[1024];
    try {
        while ((len = inputStream.read(data)) != -1) {
            outputStream.write(data, 0, len);
        }
        jsonString = new String(outputStream.toByteArray());
    } catch (IOException e) {

        e.printStackTrace();
    }
    return jsonString;
}

From source file:Main.java

public static byte[] zipCompress(byte[] input, int level) {
    Deflater compressor = new Deflater();
    compressor.setLevel(level);// w w  w . ja  v a  2 s  .com
    compressor.setInput(input);
    compressor.finish();
    ByteArrayOutputStream bos = new ByteArrayOutputStream(input.length);
    byte[] buf = new byte[1024];
    while (!compressor.finished()) {
        int count = compressor.deflate(buf);
        bos.write(buf, 0, count);
    }
    try {
        bos.close();
    } catch (IOException e) {
    }
    return bos.toByteArray();
}

From source file:Main.java

public static byte[] getBytesFromFile(File file) {
    byte[] buffer = null;
    try {//w  ww  .  j a  v a2 s  .  co  m
        if (file.exists()) {
            FileInputStream fis = new FileInputStream(file);
            ByteArrayOutputStream bos = new ByteArrayOutputStream(1000);
            byte[] b = new byte[1000];
            int n;
            while ((n = fis.read(b)) != -1) {
                bos.write(b, 0, n);
            }
            fis.close();
            bos.close();
            buffer = bos.toByteArray();
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return buffer;
}

From source file:Main.java

public static byte[] getBytesFromFile(String fileFullPath) {
    byte[] buffer = null;
    try {/*from  ww  w .ja v a  2  s .c  o m*/
        File file = new File(fileFullPath);
        if (file.exists()) {
            FileInputStream fis = new FileInputStream(file);
            ByteArrayOutputStream bos = new ByteArrayOutputStream(1000);
            byte[] b = new byte[1000];
            int n;
            while ((n = fis.read(b)) != -1) {
                bos.write(b, 0, n);
            }
            fis.close();
            bos.close();
            buffer = bos.toByteArray();
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return buffer;
}

From source file:Main.java

public static byte[] decompress(byte[] data) throws IOException, DataFormatException {
    Inflater decompresser = new Inflater();
    decompresser.setInput(data);//  w  w w  . j av  a 2 s.c  om
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length);
    byte[] buffer = new byte[1024];
    while (!decompresser.finished()) {
        int count = decompresser.inflate(buffer);
        outputStream.write(buffer, 0, count);
    }
    decompresser.end();
    outputStream.close();
    return outputStream.toByteArray();
}

From source file:Main.java

public static String fromStream(InputStream inputStream) {
    String jsonStr = "";
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    byte[] buffer = new byte[1024];
    int len = 0;/*from  ww w  . j  a  va  2  s.  co m*/
    try {
        while ((len = inputStream.read(buffer, 0, buffer.length)) != -1) {
            out.write(buffer, 0, len);
        }
        jsonStr = new String(out.toByteArray());
    } catch (IOException e) {
        e.printStackTrace();
    }
    return jsonStr;
}