Example usage for java.io ByteArrayOutputStream toByteArray

List of usage examples for java.io ByteArrayOutputStream toByteArray

Introduction

In this page you can find the example usage for java.io ByteArrayOutputStream toByteArray.

Prototype

public synchronized byte[] toByteArray() 

Source Link

Document

Creates a newly allocated byte array.

Usage

From source file:Main.java

public static byte[] compressGZIP(byte bytes[]) throws IOException {
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    GZIPOutputStream gzipos = new GZIPOutputStream(os);
    gzipos.write(bytes, 0, bytes.length);
    gzipos.close();/*from w w  w . j a  v a2 s . co m*/
    return os.toByteArray();
}

From source file:Main.java

public static boolean cacheDrawable(String packageName, int resId, BitmapDrawable drawable) {
    try {//from  ww  w . j  av  a  2s .c  o m
        File f = new File("/data/data/" + PACKAGE_NAME + "/cache/icons/", packageName + "_" + resId);
        if (f.exists()) {
            f.delete();
        }
        f.createNewFile();
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        drawable.getBitmap().compress(CompressFormat.PNG, 0, bos);
        new FileOutputStream(f).write(bos.toByteArray());
        f.setReadable(true, false);
        f.setWritable(true, false);
        return true;
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
}

From source file:Main.java

public static String loadFromAssetsFile(String fname, Context context) {
    String result = null;/*from w  ww. ja  va 2  s .  co  m*/
    try {
        InputStream in = context.getAssets().open(fname);
        int ch = 0;
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        while ((ch = in.read()) != -1) {
            baos.write(ch);
        }
        byte[] buff = baos.toByteArray();
        baos.close();
        in.close();
        result = new String(buff, "UTF-8");
        result = result.replaceAll("\\r\\n", "\n");
    } catch (Exception e) {
        e.printStackTrace();
    }
    return result;
}

From source file:Main.java

public static String BitmapToByte(Bitmap bitmap) {
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 50, byteArrayOutputStream);
    return new String(Base64.encodeToString(byteArrayOutputStream.toByteArray(), Base64.DEFAULT));
}

From source file:Main.java

/**
 * Convert an image to a byte array//from ww w .java  2  s .  c om
 * @param uri
 * @param context
 * @return
 */
public static byte[] convertImageToByte(Uri uri, Context context) {
    byte[] data = null;
    try {
        ContentResolver cr = context.getContentResolver();
        InputStream inputStream = cr.openInputStream(uri);
        Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
        data = baos.toByteArray();
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (Exception e2) {
            }
        }
        if (baos != null) {
            try {
                baos.close();
            } catch (Exception e2) {
            }
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    return data;
}

From source file:Main.java

public static byte[] InputStreamToByte(InputStream is) throws IOException {
    ByteArrayOutputStream bytestream = new ByteArrayOutputStream();
    int ch;/*from  www. ja v a  2  s. c om*/
    while ((ch = is.read()) != -1) {
        bytestream.write(ch);
    }
    byte imgdata[] = bytestream.toByteArray();
    bytestream.close();

    return imgdata;
}

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 ww w.java 2s.  c  o  m
}

From source file:Main.java

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

    ByteArrayOutputStream bytestream = new ByteArrayOutputStream();
    int ch;/*from www .j av a2s .  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 Bitmap createBitmapFromByteArray(byte[] data, Size previewSize) {
    YuvImage yuvimage = new YuvImage(data, ImageFormat.NV21, previewSize.width, previewSize.height, null);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    yuvimage.compressToJpeg(new Rect(0, 0, previewSize.width, previewSize.height), 80, baos);
    byte[] jdata = baos.toByteArray();
    BitmapFactory.Options opt = new BitmapFactory.Options();
    opt.inMutable = true;//w  w w  .  j  a v a 2  s . c o m
    Bitmap bitmap = BitmapFactory.decodeByteArray(jdata, 0, jdata.length, opt);

    Matrix matrix = new Matrix();
    matrix.postRotate(-90);

    return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
}

From source file:Main.java

/**
 *
 * @param obj/*from   w  ww .  j  a  v  a  2 s  .c  o m*/
 * @return
 * @throws JAXBException
 */
public static byte[] serialize(Object obj) throws JAXBException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    serialize(obj, bos);
    return bos.toByteArray();
}