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 ByteArrayInputStream Bitmap2InputStream(Bitmap bm) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bm.compress(Bitmap.CompressFormat.JPEG, 100, baos);
    ByteArrayInputStream is = new ByteArrayInputStream(baos.toByteArray());
    return is;/*  w  w w . ja  va 2s  .co  m*/
}

From source file:Main.java

public static String exception2String(Exception e) {
    ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
    PrintWriter err = new PrintWriter(arrayOutputStream);
    e.printStackTrace(err);/*ww  w.  ja v  a  2 s  .  c o m*/
    err.close();
    try {
        arrayOutputStream.close();
    } catch (IOException e1) {
    }
    return arrayOutputStream.toString();
}

From source file:Main.java

public static String getBase64FromBitmap(Bitmap bitmap) {
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
    byte[] byteArray = byteArrayOutputStream.toByteArray();
    return Base64.encodeToString(byteArray, Base64.DEFAULT);
}

From source file:Main.java

public static byte[] getBytes(InputStream in) {

    try {/*ww  w . ja  v  a 2s .c  o m*/
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] buffer = new byte[8192];
        int len = 0;
        while ((len = in.read(buffer)) != -1)
            baos.write(buffer, 0, len);
        in.close();
        return baos.toByteArray();
    } catch (IOException e) {
        return null;
    }
}

From source file:Main.java

public static byte[] getByteArray(InputStream is) throws IOException {
    ByteArrayOutputStream buffer = new ByteArrayOutputStream();
    int nRead;/*from ww w .j  ava2s  .  c om*/
    byte[] data = new byte[16384];

    while ((nRead = is.read(data, 0, data.length)) != -1) {
        buffer.write(data, 0, nRead);
    }
    buffer.flush();
    return buffer.toByteArray();
}

From source file:Main.java

public static final byte[] serialize(Object o) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(baos);
    oos.writeObject(o);//from   w  w w  .  j  a v  a2 s .  c om
    oos.flush();
    oos.close();
    return baos.toByteArray();
}

From source file:Main.java

public static byte[] readStream(InputStream is) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    byte[] buf = new byte[1024 * 10];
    int readlen;//from w  w w . ja v  a2s . c o m
    while ((readlen = is.read(buf)) >= 0) {
        baos.write(buf, 0, readlen);
    }
    baos.close();

    return baos.toByteArray();
}

From source file:Main.java

public static byte[] getBytes(Bitmap bitmap) {
    if (bitmap == null)
        return null;
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 0, stream);
    return stream.toByteArray();
}

From source file:Main.java

public static byte[] convertBitmapToByteArray(Bitmap bitmap) {
    if (bitmap != null) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        // TODO see if it raise problems
        bitmap.compress(Bitmap.CompressFormat.JPEG, 80, baos);
        return baos.toByteArray();
    } else {//from w ww  .j ava2 s. co m
        return null;
    }
}

From source file:Main.java

public static String readFromStream(InputStream in) throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    int len = 0;/*from w  w  w .j  a  va  2  s .co m*/
    byte[] buf = new byte[1024];
    if ((len = in.read(buf)) != -1) {
        out.write(buf, 0, len);
    }
    String result = out.toString();
    in.close();
    out.close();
    return result;
}