Example usage for java.io ByteArrayOutputStream close

List of usage examples for java.io ByteArrayOutputStream close

Introduction

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

Prototype

public void close() throws IOException 

Source Link

Document

Closing a ByteArrayOutputStream has no effect.

Usage

From source file:Main.java

public static String soapMessageToString(SOAPMessage message) {
    String result = null;/*ww w  . j ava  2s. co  m*/
    if (message != null) {
        ByteArrayOutputStream baos = null;
        try {
            baos = new ByteArrayOutputStream();
            message.writeTo(baos);
            result = baos.toString();
        } catch (Exception e) {
        } finally {
            if (baos != null) {
                try {
                    baos.close();
                } catch (IOException ioe) {
                }
            }
        }
    }
    return result;
}

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 a va 2 s  .  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

private static byte[] getBitmapBytes(Bitmap bitmap, boolean paramBoolean, int width, int heigth) {
    Bitmap localBitmap = Bitmap.createBitmap(width, heigth, Bitmap.Config.RGB_565);
    Canvas localCanvas = new Canvas(localBitmap);
    int i;//from w  ww. j  a  v  a2  s .  c om
    int j;
    if (bitmap.getHeight() > bitmap.getWidth()) {
        i = bitmap.getWidth();
        j = bitmap.getWidth();
    } else {
        i = bitmap.getHeight();
        j = bitmap.getHeight();
    }
    while (true) {
        localCanvas.drawBitmap(bitmap, new Rect(0, 0, i, j), new Rect(0, 0, width, heigth), null);
        if (paramBoolean)
            bitmap.recycle();
        ByteArrayOutputStream localByteArrayOutputStream = new ByteArrayOutputStream();
        localBitmap.compress(Bitmap.CompressFormat.JPEG, 100, localByteArrayOutputStream);
        localBitmap.recycle();
        byte[] arrayOfByte = localByteArrayOutputStream.toByteArray();
        try {
            localByteArrayOutputStream.close();
            return arrayOfByte;
        } catch (Exception e) {
            e.printStackTrace();
        }
        i = bitmap.getHeight();
        j = bitmap.getHeight();
    }
}

From source file:Main.java

public static byte[] compress(byte[] data) throws IOException {
    Deflater deflater = new Deflater();
    deflater.setInput(data);//ww  w.  ja v  a 2s.com

    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 byte[] compressInZlib(byte[] originalData, int offset, int length) throws IOException {

    Deflater compresser = new Deflater();
    compresser.setInput(originalData, offset, length);
    compresser.finish();/*from   w w w  .j av  a 2  s.com*/

    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

private static byte[] compress(byte[] data) throws IOException {
    Deflater deflater = new Deflater();
    deflater.setInput(data);/*from   w  w w.  j  a v  a2 s.com*/

    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();
    deflater.end();

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

From source file:Main.java

public static byte[] readByteArray(Context context, String fileName) throws IOException {

    byte[] data;// w w w.j a v  a2 s. c om
    int c;

    FileInputStream fis = context.openFileInput(fileName);
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    BufferedInputStream bos = new BufferedInputStream(fis);

    while ((c = bos.read()) != -1) {
        byteArrayOutputStream.write(c);
    }

    data = byteArrayOutputStream.toByteArray();

    bos.close();
    byteArrayOutputStream.close();
    fis.close();

    return data;
}

From source file:Main.java

public static String streamToString(InputStream inputStream) {
    try {/*from  ww w. j av a2s  . c  o m*/
        ByteArrayOutputStream out = new ByteArrayOutputStream();

        byte[] buffer = new byte[1024];
        int len = 0;
        while ((len = inputStream.read(buffer)) != -1) {
            out.write(buffer, 0, len);
            out.flush();
        }

        String result = out.toString();
        out.close();
        inputStream.close();
        return result;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return "";
}

From source file:Main.java

public static byte[] compress(byte[] content) throws IOException {
    ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
    byte[] byteMe = null;
    try {/*from   ww w . j  av a  2 s .c  o m*/
        GZIPOutputStream gzipOutputStream = new GZIPOutputStream(byteOut);
        gzipOutputStream.write(content);
        gzipOutputStream.close();
        byteMe = byteOut.toByteArray();
    } finally {
        byteOut.close();
    }
    return byteMe;
}

From source file:Main.java

public static byte[] streamToByteArray(InputStream stream) throws IOException {
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    byte[] buffer = new byte[1024];
    try {//from w w  w . j a  v  a  2s . c  o  m
        int count;
        while ((count = stream.read(buffer)) != -1) {
            bytes.write(buffer, 0, count);
        }
        return bytes.toByteArray();
    } finally {
        stream.close();
        bytes.close();
    }
}