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

private static byte[] bitmap2Bytes(Bitmap bm) {
    try {/*from  ww w .jav  a 2  s.  c  o  m*/
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
        byte[] bytes = baos.toByteArray();
        baos.close();
        return bytes;
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static byte[] bitmap2ByteArray(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  .j  a  v  a  2s. c om
    }

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

    return result;
}

From source file:Main.java

/**
 * Converts a bitmap to a byte array/*ww  w .j  av  a2s.co m*/
 * @param bitmap
 * @return
 */
public static byte[] getImageBytes(Bitmap bitmap) {
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 0, stream);
    return stream.toByteArray();
}

From source file:Main.java

private static byte[] bmpToByteArray(final Bitmap bmp, final boolean needRecycle) {
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    bmp.compress(CompressFormat.PNG, 100, output);
    if (needRecycle) {
        bmp.recycle();/*from ww  w.j a v a2s  .  c  o m*/
    }

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

    return result;
}

From source file:Main.java

public static byte[] objectToByte(Object obj) throws IOException {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    ObjectOutputStream os = new ObjectOutputStream(outputStream);
    os.writeObject(obj);/*from w w  w  .j  a  v a2 s . c o m*/
    return outputStream.toByteArray();
}

From source file:Main.java

private static byte[] bitmap2Bytes(final Bitmap bitmap) {
    if (bitmap == null)
        return null;
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
    return baos.toByteArray();
}

From source file:Main.java

public static byte[] bitmap2Bytes(Bitmap bm, Bitmap.CompressFormat format, int quality) {
    try {//from   w w  w  . jav a  2  s .com
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bm.compress(format, quality, baos);
        return baos.toByteArray();
    } finally {
        bm.recycle();
    }
}

From source file:Main.java

public static byte[] Bitmap2Bytes(Bitmap bm) {
    if (bm == null) {
        return null;
    }/*  w ww  . j av a 2 s  .c  o  m*/
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
    return baos.toByteArray();
}

From source file:Main.java

private static String inputStream2String(InputStream is) {
    try {/*  www.j av a2  s  . com*/
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] array = new byte[1024];
        int len;
        while ((len = is.read(array, 0, array.length)) != -1) {
            baos.write(array, 0, len);
        }
        return baos.toString();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static byte[] getBytes(InputStream inputStream) throws IOException {
    ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream();
    int bufferSize = 1024;
    byte[] buffer = new byte[bufferSize];

    int len = 0;/*from  www.  ja v  a2s .  c om*/
    while ((len = inputStream.read(buffer)) != -1) {
        byteBuffer.write(buffer, 0, len);
    }
    return byteBuffer.toByteArray();
}