Example usage for java.io ByteArrayOutputStream ByteArrayOutputStream

List of usage examples for java.io ByteArrayOutputStream ByteArrayOutputStream

Introduction

In this page you can find the example usage for java.io ByteArrayOutputStream ByteArrayOutputStream.

Prototype

public ByteArrayOutputStream(int size) 

Source Link

Document

Creates a new ByteArrayOutputStream , with a buffer capacity of the specified size, in bytes.

Usage

From source file:Main.java

public static byte[] compress(byte[] uncompressedBuffer) {
    Deflater deflater = new Deflater();
    deflater.setInput(uncompressedBuffer);
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream(uncompressedBuffer.length);

    try {//  w ww  . j a  v a 2  s. co m
        deflater.finish();
        byte[] buffer = new byte[1024];
        while (!deflater.finished()) {
            int count = deflater.deflate(buffer);
            outputStream.write(buffer, 0, count);
        }
        byte[] output = outputStream.toByteArray();
        return output;

    } finally {
        try {
            deflater.end();
            outputStream.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}

From source file:Main.java

@NonNull
public static byte[] bitmapToByteArray(@NonNull Bitmap bitmap) {
    ByteArrayOutputStream out = null;
    try {// ww  w  . j  a v  a2 s.com
        out = new ByteArrayOutputStream(bitmap.getWidth() * bitmap.getHeight());
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
        return out.toByteArray();
    } finally {
        if (out != null)
            try {
                out.close();
            } catch (Exception ignore) {
            }
    }
}

From source file:Main.java

protected static ByteArrayOutputStream inflate(final FileInputStream pInputStream) throws IOException {
    final ByteArrayOutputStream uncompressed = new ByteArrayOutputStream(ONE_MB);
    final GZIPInputStream zin = new GZIPInputStream(pInputStream);

    int read;/*from  w  w  w  .  ja v a 2 s  .  c  om*/
    final byte[] buf = new byte[ONE_MB];
    while ((read = zin.read(buf)) != -1) {
        uncompressed.write(buf, 0, read);
    }

    return uncompressed;
}

From source file:Main.java

public static String toXml(Object model) throws JAXBException, IOException {
    ByteArrayOutputStream output = new ByteArrayOutputStream(1024);
    marshal(model, output);/*from   w  ww .  ja v a  2  s.  c o m*/
    output.flush();
    return new String(output.toByteArray(), "UTF-8");
}

From source file:Main.java

public static byte[] decompress(byte[] data) {
    byte[] output = new byte[0];

    Inflater decompresser = new Inflater();
    decompresser.reset();/*from  w w w . ja v  a  2s  .  co  m*/
    decompresser.setInput(data);

    ByteArrayOutputStream bos = new ByteArrayOutputStream(2 * data.length);
    try {
        byte[] buf = new byte[1024];
        while (!decompresser.finished()) {
            int length = decompresser.inflate(buf);
            bos.write(buf, 0, length);
        }
        output = bos.toByteArray();
        bos.close();
    } catch (Exception e) {
        output = data;
        e.printStackTrace();
    }

    decompresser.end();
    return output;
}

From source file:Main.java

public static final byte[] unCompress(byte[] buf) throws IOException {
    GZIPInputStream gzi = new GZIPInputStream(new ByteArrayInputStream(buf));
    ByteArrayOutputStream bos = new ByteArrayOutputStream(buf.length);

    int count;//from   w  w w .  j a va2s  .c  o m
    byte[] tmp = new byte[2048];
    while ((count = gzi.read(tmp)) != -1) {
        bos.write(tmp, 0, count);
    }

    // store uncompressed back to buffer      
    gzi.close();
    return bos.toByteArray();
}

From source file:Main.java

public static byte[] compress(byte[] data) throws IOException {
    Deflater deflater = new Deflater();
    deflater.setInput(data);// w ww  .ja  v a2s.c o  m

    ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length);

    deflater.finish();
    byte[] buffer = new byte[1024];
    while (!deflater.finished()) {
        int count = deflater.deflate(buffer); // returns the generated code... index  
        outputStream.write(buffer, 0, count);
    }
    outputStream.close();
    byte[] output = outputStream.toByteArray();

    System.out.println("Original: " + data.length / 1024 + " Kb");
    System.out.println("Compressed: " + output.length / 1024 + " Kb");
    return output;
}

From source file:Main.java

public static ByteArrayInputStream toByteStream(InputStream istream) {
    ByteArrayInputStream byteistream = new ByteArrayInputStream(new byte[0]);
    try {//  ww  w.  j a va 2 s. co  m
        ByteArrayOutputStream byteostream = new ByteArrayOutputStream(8192);
        byte[] buffer = new byte[8192];
        int lenght;
        while ((lenght = istream.read(buffer)) != -1) {
            byteostream.write(buffer, 0, lenght);
        }
        byteistream = new ByteArrayInputStream(byteostream.toByteArray());
        byteostream.close();
    } catch (Exception e) {
    } finally {
        try {
            istream.close();
        } catch (Exception e) {
        }
    }
    return byteistream;
}