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[] longToByteArray(long number) {
    try {//from  ww  w  . j  a v a 2  s  .  c o  m
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        DataOutputStream dos = new DataOutputStream(bos);
        dos.writeLong(number);
        dos.flush();
        // lenth have to be 2 byte
        byte[] d = bos.toByteArray();
        dos.close();

        return d;
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static String bmpToStrBase64(Bitmap bitmap) {

    Bitmap bm = bitmap;//from  www  . ja v  a  2 s  . com
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bm.compress(Bitmap.CompressFormat.JPEG, 80, stream); //bm is the bitmap object
    byte[] b = stream.toByteArray();
    String strBase64 = Base64.encodeToString(b, 0);

    return strBase64;
}

From source file:Main.java

public static long getBitmapSize(Bitmap bitmap) {
    long length = 0;
    if (bitmap != null) {
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
        byte[] imageInByte = stream.toByteArray();
        length = imageInByte.length;/*from  ww  w  . ja  v  a  2  s  .com*/
    }
    return length;
}

From source file:Main.java

public static byte[] bmpToByteArray(Bitmap bmp) {
    if (bmp == null)
        return null;
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    bmp.compress(Bitmap.CompressFormat.PNG, 80, output);

    byte[] result = output.toByteArray();
    try {/*  w  ww  . j a va 2  s  . c o m*/
        output.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return result;
}

From source file:Main.java

public static InputStream Bitmap2IS(Bitmap bm) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    // What about 50...
    bm.compress(Bitmap.CompressFormat.JPEG, 50, baos);
    InputStream sbs = new ByteArrayInputStream(baos.toByteArray());
    return sbs;/*from w  w w .  jav a2s . c o m*/
}

From source file:Main.java

public static byte[] InputStreamToByte(InputStream is) throws IOException {

    ByteArrayOutputStream bytestream = new ByteArrayOutputStream();
    int ch;//from w ww. ja  v a  2 s  . c o m
    while ((ch = is.read()) != -1) {
        bytestream.write(ch);
    }
    byte byteData[] = bytestream.toByteArray();
    bytestream.close();
    return byteData;
}

From source file:Main.java

public static String bitmapToBase64(Bitmap bitmap) {

    try {//from   w ww.j a  v a  2s  .co  m
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
        baos.close();
        byte[] buffer = baos.toByteArray();
        String photo = Base64.encodeToString(buffer, 0, buffer.length, Base64.DEFAULT);
        return photo;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static byte[] shortToByteArray(short number) {
    try {/*from w  w w  . j av  a 2  s  .c  o m*/
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        DataOutputStream dos = new DataOutputStream(bos);
        dos.writeShort(number);
        dos.flush();
        // lenth have to be 2 byte
        byte[] d = bos.toByteArray();
        dos.close();

        return d;
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

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

From source file:Main.java

public static byte[] serialize(Object obj) throws Exception {
    //System.err.println(" ser ##"+obj);
    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    ObjectOutputStream oout = new ObjectOutputStream(bout);
    oout.writeObject(obj);/*from  w  ww .j  a va  2  s.  c  o m*/
    oout.flush();
    byte array[] = bout.toByteArray();
    oout.close();
    bout.close();
    //System.err.println(" ser #"+new String(array));
    return array;
}