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[] getByteArray(InputStream inputstream) {
    try {/*from   ww w.  j a  v  a 2  s  .c  om*/
        ByteArrayOutputStream bytearrayoutputstream = new ByteArrayOutputStream();
        byte abyte1[] = new byte[1024];
        do {
            int i = inputstream.read(abyte1);
            if (i == -1)
                break;
            bytearrayoutputstream.write(abyte1, 0, i);
        } while (true);
        byte result[] = bytearrayoutputstream.toByteArray();
        bytearrayoutputstream.close();
        inputstream.close();
        return result;
    } catch (IOException e) {
        return null;
    }

}

From source file:org.mule.modules.freshbooks.automation.util.RequestAndResponseUtil.java

private static String getResourceAsString(InputStream in) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
    byte[] buf = new byte[1024];
    int sz = 0;//from  w  ww .j ava 2s .c om
    try {
        while (true) {
            sz = in.read(buf);

            baos.write(buf, 0, sz);
            if (sz < buf.length)
                break;
        }
    } finally {
        try {
            in.close();
        } catch (Exception e) {

        }
    }
    return new String(baos.toByteArray());
}

From source file:Main.java

/*****************************************************
 *
 * Returns the bytes from an input stream, or an exception
 * if there was an error./*ww  w  .  ja v a 2s . c  om*/
 *
 *****************************************************/
private static Object getBytes(BufferedInputStream bis) throws Exception {
    ByteArrayOutputStream baos = new ByteArrayOutputStream(bis.available());

    byte[] buffer = new byte[READ_BUFFER_SIZE_IN_BYTES];

    int byteCount = -1;

    while ((byteCount = bis.read(buffer)) >= 0) {
        baos.write(buffer, 0, byteCount);
    }

    return (baos.toByteArray());
}

From source file:Main.java

public static byte[] decompress(byte[] input, boolean GZIPFormat) throws IOException, DataFormatException {
    Inflater decompressor = new Inflater(GZIPFormat);
    decompressor.setInput(input);// w w  w . j a  v  a 2  s.  co m
    ByteArrayOutputStream bao = new ByteArrayOutputStream();
    byte[] readBuffer = new byte[1024];
    int readCount = 0;

    while (!decompressor.finished()) {
        readCount = decompressor.inflate(readBuffer);
        if (readCount > 0) {
            bao.write(readBuffer, 0, readCount);
        }
    }
    decompressor.end();
    return bao.toByteArray();
}

From source file:Main.java

public static byte[] compress(byte[] data) throws IOException {
    Deflater compresser = new Deflater(Deflater.BEST_COMPRESSION);
    compresser.setInput(data);/*from   w w w  .  j  av a 2s. co  m*/
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length);
    compresser.finish();
    byte[] buffer = new byte[1024];
    while (!compresser.finished()) {
        int count = compresser.deflate(buffer);
        outputStream.write(buffer, 0, count);
    }
    compresser.end();
    outputStream.close();
    return outputStream.toByteArray();
}

From source file:Main.java

/**
 * Read and return the entire contents of the supplied {@link InputStream stream}. This method always closes the stream when
 * finished reading./*from  w w  w .  j a va  2s  .  c o m*/
 * 
 * @param stream the stream to the contents; may be null
 * @return the contents, or an empty byte array if the supplied reader is null
 * @throws IOException if there is an error reading the content
 */
public static byte[] readBytes(InputStream stream) throws IOException {
    if (stream == null)
        return new byte[] {};
    byte[] buffer = new byte[1024];
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    boolean error = false;
    try {
        int numRead = 0;
        while ((numRead = stream.read(buffer)) > -1) {
            output.write(buffer, 0, numRead);
        }
    } catch (IOException e) {
        error = true; // this error should be thrown, even if there is an error closing stream
        throw e;
    } catch (RuntimeException e) {
        error = true; // this error should be thrown, even if there is an error closing stream
        throw e;
    } finally {
        try {
            stream.close();
        } catch (IOException e) {
            if (!error)
                throw e;
        }
    }
    output.flush();
    return output.toByteArray();
}

From source file:Main.java

public static byte[] decompress(byte[] data, int off, int len) {

    byte[] output = null;

    Inflater decompresser = new Inflater();
    decompresser.reset();/*from  ww  w .  j  a v a  2  s.com*/
    decompresser.setInput(data, off, len);

    ByteArrayOutputStream out = new ByteArrayOutputStream(data.length);
    try {
        byte[] result = new byte[1024];
        while (!decompresser.finished()) {
            int i = decompresser.inflate(result);
            out.write(result, 0, i);
        }
        output = out.toByteArray();
    } catch (Exception e) {
        throw new RuntimeException(e);
    } finally {
        try {
            out.close();
        } catch (Exception e) {
        }
        decompresser.end();
    }
    return output;
}

From source file:Main.java

private static byte[] compressBytesInflateDeflate(byte[] inBytes) {
    Deflater deflater = new Deflater(Deflater.BEST_SPEED);
    deflater.setInput(inBytes);/*from  w w w  .  ja  v a  2  s . c  om*/
    ByteArrayOutputStream bos = new ByteArrayOutputStream(inBytes.length);
    deflater.finish();
    byte[] buffer = new byte[1024 * 8];
    while (!deflater.finished()) {
        int count = deflater.deflate(buffer);
        bos.write(buffer, 0, count);
    }
    byte[] output = bos.toByteArray();
    return output;
}

From source file:Main.java

public static byte[] compress(byte[] input, int compressionLevel, boolean GZIPFormat) throws IOException {
    Deflater compressor = new Deflater(compressionLevel, GZIPFormat);

    compressor.setInput(input);/*w  w w. ja  va 2  s  .c  om*/

    compressor.finish();

    ByteArrayOutputStream bao = new ByteArrayOutputStream();
    byte[] readBuffer = new byte[1024];
    int readCount = 0;

    while (!compressor.finished()) {
        readCount = compressor.deflate(readBuffer);
        if (readCount > 0) {
            bao.write(readBuffer, 0, readCount);
        }
    }

    compressor.end();
    return bao.toByteArray();
}

From source file:Main.java

public static byte[] readBytes(InputStream in) throws IOException {
    if (!(in instanceof BufferedInputStream)) {
        in = new BufferedInputStream(in);
    }/* ww  w.j  a  va 2s.c o  m*/
    ByteArrayOutputStream out = null;
    try {
        out = new ByteArrayOutputStream();
        byte[] buf = new byte[1024];
        int len;
        while ((len = in.read(buf)) != -1) {
            out.write(buf, 0, len);
        }
    } finally {
        closeQuietly(out);
    }
    return out.toByteArray();
}