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[] readFileToByteArray2(File file) {
    try {/*from  w w  w .  jav  a2 s .  c  o m*/
        FileInputStream fis = new FileInputStream(file);
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        byte[] buf = new byte[1024];

        for (int readNum; (readNum = fis.read(buf)) != -1;) {
            bos.write(buf, 0, readNum);
        }
        bos.flush();
        byte[] bytes = bos.toByteArray();
        fis.close();
        bos.close();
        return bytes;

    } catch (IOException e) {
        return null;
    }
}

From source file:Main.java

/**
 * generate base64 from bitmap image/*w ww  . java2 s  . c om*/
 * @param mBitmap bitmap image
 * @return base64 string
 */
public static String ImageToBase64(Bitmap mBitmap) {
    ByteArrayOutputStream baos1 = new ByteArrayOutputStream();
    mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos1);
    byte[] b1 = baos1.toByteArray();
    return Base64.encodeToString(b1, Base64.DEFAULT);
}

From source file:Main.java

public static String openAsBase64(String imgPath) {
    Bitmap photo = BitmapFactory.decodeFile(imgPath);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    photo.compress(Bitmap.CompressFormat.JPEG, 85, baos);
    byte[] b = baos.toByteArray();
    photo.recycle();// w w  w .  j  av a 2s. c o  m
    return Base64.encodeToString(b, Base64.DEFAULT);

}

From source file:Main.java

/**
 * convert Bitmap to InputStream/*from w w w  .j  a v a2  s  .  co m*/
 */
public static InputStream bitmapToStream(Bitmap bitmap, Bitmap.CompressFormat compressFormat) {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    bitmap.compress(compressFormat, 100, bos);
    return new ByteArrayInputStream(bos.toByteArray());
}

From source file:Main.java

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

    if (bm != null) {
        bm.recycle();
        bm = null;
    }

    return baos.toByteArray();
}

From source file:Main.java

public static byte[] bitmapToByteArray(Bitmap bitmap, Bitmap.CompressFormat format) {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    bitmap.compress(format, 100, out);//from  w ww.j  a va2  s  .c  om
    return out.toByteArray();
}

From source file:Main.java

public static byte[] getAppIconByte(Context c, String packageName) {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    try {// w w  w. jav a2s . co m
        BitmapDrawable drawable = (BitmapDrawable) c.getPackageManager().getApplicationIcon(packageName);
        drawable.getBitmap().compress(Bitmap.CompressFormat.PNG, 100, out);
    } catch (NameNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return out.toByteArray();
}

From source file:Main.java

public static byte[] decodeB(byte[] bytes) throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    int i = 0;/*from  ww w.ja v  a2s  .  co m*/
    int len = bytes.length;
    while (i < len) {
        char[] four = new char[4];
        int j = 0;
        while (j < 4) {
            byte b = bytes[i++];
            if (b != '\r' || b != '\n')
                four[j++] = (char) b;
        }
        int k;
        if (four[3] == '=') {
            if (four[2] == '=') {
                k = 1;
            } else {
                k = 2;
            }
        } else {
            k = 3;
        }
        int aux = 0;
        for (j = 0; j < 4; j++) {
            if (four[j] != '=') {
                aux = aux | (chars.indexOf(four[j]) << (6 * (3 - j)));
            }
        }
        for (j = 0; j < k; j++) {
            out.write((aux >>> (8 * (2 - j))) & 0xFF);
        }
    }
    out.close();
    return out.toByteArray();
}

From source file:Main.java

public static String converStream2String(InputStream is, String charset) throws UnsupportedEncodingException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    byte[] buffer = new byte[512];
    int len = 0;/*from  w  w  w  .  j av  a  2 s .c  o  m*/

    try {
        while ((len = is.read(buffer)) != -1) {
            baos.write(buffer, 0, len);
        }
        is.close();
        return new String(baos.toByteArray());
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return new String(baos.toByteArray(), charset);
}

From source file:Main.java

public static byte[] Drawable2ByteArray(Drawable drawable) {
    Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    return stream.toByteArray();
}