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() 

Source Link

Document

Creates a new ByteArrayOutputStream .

Usage

From source file:Main.java

public static byte[] compressBitmap(Bitmap bitmap) {
    ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
    if (bitmap.compress(Bitmap.CompressFormat.JPEG, 80, byteStream)) {
        try {//  w  w w.  ja  v a2  s . com
            byteStream.flush();
            byteStream.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    return byteStream.toByteArray();
}

From source file:Main.java

public static byte[] getBitmap2Bytes(Bitmap bitmap) {
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
    byte[] byteArray = stream.toByteArray();
    return byteArray;
}

From source file:Main.java

public static byte[] bitmap2Bytes(Bitmap bitmap) {
    ByteArrayOutputStream baos;//from w w  w. ja va 2s .c o  m
    try {
        baos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
        byte[] b = baos.toByteArray();
        baos.flush();
        baos.close();
        return b;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static String readData(InputStream inSream, String charsetName) throws Exception {
    ByteArrayOutputStream outStream = new ByteArrayOutputStream();
    byte[] buffer = new byte[1024];
    int len = -1;
    while ((len = inSream.read(buffer)) != -1) {
        outStream.write(buffer, 0, len);
    }//w  w  w . ja  v  a2 s.  c  o m
    byte[] data = outStream.toByteArray();
    outStream.close();
    inSream.close();
    return new String(data, charsetName);
}

From source file:Main.java

public static byte[] InputStram2byteArray(InputStream in) {
    try {//w w  w . j a  v a2 s. c o  m
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        if (in != null) {
            byte[] buffer = new byte[1024];
            int length = 0;
            while ((length = in.read(buffer)) != -1) {
                out.write(buffer, 0, length);
            }
            out.close();
            in.close();
            return out.toByteArray();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    return null;
}

From source file:Main.java

public static String readStream(InputStream in) throws IOException {

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    int i;// ww w  .j a v  a 2s.  c o m
    while ((i = in.read()) != -1) {
        baos.write(i);
    }
    return baos.toString();
}

From source file:Main.java

public static byte[] bmpToByteArray(final Bitmap bmp, final boolean needRecycle) {
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    bmp.compress(Bitmap.CompressFormat.PNG, 100, output);
    if (needRecycle) {
        bmp.recycle();/*from   w  w  w  . jav  a2  s .co m*/
    }

    byte[] result = output.toByteArray();
    try {
        output.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return result;
}

From source file:Main.java

public static byte[] getBytes(Object obj) throws IOException {
    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    ObjectOutputStream out = new ObjectOutputStream(bout);
    out.writeObject(obj);//from www.j a va2 s  .c  o m
    out.flush();
    byte[] bytes = bout.toByteArray();
    bout.close();
    out.close();
    return bytes;
}

From source file:Main.java

public static String readString(InputStream is) throws IOException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    byte[] buffer = new byte[1024];
    int len = -1;
    while ((len = is.read(buffer)) > 0) {
        bos.write(buffer, 0, len);//  w w  w  . j  ava2s .co m
    }
    bos.close();

    return bos.toString();
}

From source file:Main.java

public static byte[] readStream(InputStream inStream) {
    try {//from   w  w w  .j ava  2s.c  o  m
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int len = 0;
        while ((len = inStream.read(buffer)) != -1) {
            outStream.write(buffer, 0, len);
        }

        byte[] data = outStream.toByteArray();
        outStream.flush();
        outStream.close();
        inStream.close();

        return data;
    } catch (OutOfMemoryError e) {
        return null;
    } catch (Exception e) {
        return null;
    }
}