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 void writeBitmapToFile(Bitmap bitmap, File file) throws IOException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, bos);
    byte[] bitmapData = bos.toByteArray();

    //write the bytes in file
    FileOutputStream fos = new FileOutputStream(file);
    fos.write(bitmapData);/* w w  w  .  j  a  va 2 s .c  om*/
    fos.flush();
    fos.close();
}

From source file:Main.java

private static byte[] readInputStream(InputStream inputStream) throws IOException {
    byte[] buffer = new byte[8192];
    int len = 0;/*from   w  w w .  java 2 s . c o m*/
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    while ((len = inputStream.read(buffer)) != -1) {
        bos.write(buffer, 0, len);
    }
    bos.close();
    return bos.toByteArray();
}

From source file:Main.java

public static InputStream getInputStream(Bitmap bitmap) {
    ByteArrayOutputStream bStream = new ByteArrayOutputStream();

    bitmap.compress(CompressFormat.JPEG, 100, bStream);

    return new ByteArrayInputStream(bStream.toByteArray());
}

From source file:Main.java

public static byte[] compress(String str) throws Exception {
    if (str == null || str.length() == 0) {
        return null;
    }//from   ww w .  j  a v a  2s .  c om

    ByteArrayOutputStream obj = new ByteArrayOutputStream();
    GZIPOutputStream gzip = new GZIPOutputStream(obj);

    gzip.write(str.getBytes());
    gzip.close();

    byte[] compressed = obj.toByteArray();
    obj.close();

    return compressed;
}

From source file:Main.java

public static byte[] serializeObject(Object o) {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();

    try {//w  w w  .java 2 s . c  o m
        ObjectOutput out = new ObjectOutputStream(bos);
        out.writeObject(o);
        out.close();

        byte[] buf = bos.toByteArray();

        return buf;
    } catch (IOException ioe) {
        //         Log.e("serializeObject", "error", ioe);

        return null;
    }
}

From source file:Main.java

public static InputStream getBitmapPngStream(Bitmap theBitmap) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    theBitmap.compress(Bitmap.CompressFormat.PNG, 95, baos);

    byte[] data = baos.toByteArray();
    return new ByteArrayInputStream(data);
}

From source file:Main.java

public static InputStream getBitmapJpegStream(Bitmap theBitmap) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    theBitmap.compress(Bitmap.CompressFormat.JPEG, 95, baos);

    byte[] data = baos.toByteArray();
    return new ByteArrayInputStream(data);
}

From source file:Main.java

public static void saveThumbnail(String filePath, Bitmap bitmap) {
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();

    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);

    // you can create a new file name "test.jpg" in sdcard folder.

    File f = new File(filePath);

    try {//w  w w. j a v a 2  s.c o  m

        f.createNewFile();
        // write the bytes in file
        FileOutputStream fo = new FileOutputStream(f);
        fo.write(bytes.toByteArray());
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

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

    return null;
}

From source file:Main.java

public static String inputStream2String(InputStream in) throws IOException {
    byte[] buf = new byte[1024];
    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    for (int i; (i = in.read(buf)) != -1;) {
        baos.write(buf, 0, i);//ww  w .  j  a v  a  2 s  .  c  o  m
    }

    return baos.toString("UTF-8");
}