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:StreamsUtils.java

public static byte[] readBytes(InputStream stream) throws IOException {
    ByteArrayOutputStream b = new ByteArrayOutputStream();
    int readedBytes;
    byte[] buf = new byte[1024];
    while ((readedBytes = stream.read(buf)) > 0) {
        b.write(buf, 0, readedBytes);/*from  www  .  j  a va  2  s  . c  o  m*/
    }
    b.close();
    return b.toByteArray();
}

From source file:Main.java

public static byte[] getBitmapAsByteArray(Bitmap bitmap) {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    bitmap.compress(CompressFormat.PNG, 0, outputStream);
    return outputStream.toByteArray();
}

From source file:Main.java

public static byte[] getResponeData(int index, int resultCode, String response) {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    outputStream.write(index);//from   www  .  ja v  a2 s .c  om
    outputStream.write(0);
    outputStream.write(resultCode);
    try {
        outputStream.write(response.getBytes());
    } catch (IOException e) {
        e.printStackTrace();
    }
    return outputStream.toByteArray();
}

From source file:Main.java

public static byte[] convertIntToByteArray(int my_int) throws IOException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    DataOutputStream dos = new DataOutputStream(bos);
    dos.writeInt(my_int);
    dos.close();//from  w w  w . j  a  v  a  2  s  .c om
    byte[] int_bytes = bos.toByteArray();
    bos.close();
    return int_bytes;
}

From source file:Main.java

public static byte[] bitmapToByte(Bitmap bitmap) {
    if (bitmap == null)
        return null;
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.JPEG, 50, baos);
    return baos.toByteArray();
}

From source file:Main.java

public static byte[] bitmap2Bytes(Bitmap bm) {
    if (bm == null) {
        return null;
    }//from w w w . j a  v  a2  s. c  o  m
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
    return baos.toByteArray();
}

From source file:Main.java

public static byte[] bitmapToBytes(Bitmap bitmap) {

    if (bitmap == null)
        return null;
    final ByteArrayOutputStream os = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, os);
    return os.toByteArray();
}

From source file:Main.java

public static ByteArrayInputStream Bitmap2ByteArrayInputStream(Bitmap bm) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bm.compress(Bitmap.CompressFormat.JPEG, 100, baos);
    return new ByteArrayInputStream(baos.toByteArray());
}

From source file:Main.java

public static byte[] objectToStream(Object obj) {
    try {// w ww  .j  a va2  s  . com
        ByteArrayOutputStream buff = new ByteArrayOutputStream();
        ObjectOutputStream stream = new ObjectOutputStream(buff);
        stream.writeObject(obj);
        stream.close();
        return buff.toByteArray();
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

private static String isStreamString(InputStream is) {
    ByteArrayOutputStream bo = new ByteArrayOutputStream();
    byte[] buf = new byte[1024];
    int len = -1;
    try {//from  w ww  . ja  v a 2s .  c om
        while ((len = is.read(buf)) != -1) {
            bo.write(buf, 0, len);
        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally {
        if (is != null)
            try {
                is.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    }
    return replaceBr(new String(bo.toByteArray()));
}