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

static byte[] streamToBytes(InputStream paramInputStream) throws IOException {
    ByteArrayOutputStream localByteArrayOutputStream = new ByteArrayOutputStream();
    if (paramInputStream != null) {
        byte[] arrayOfByte = new byte[4096];
        while (true) {
            int i = paramInputStream.read(arrayOfByte);
            if (i == -1)
                break;
            localByteArrayOutputStream.write(arrayOfByte, 0, i);
        }/*from   w  w w .  j  a  va2  s .c om*/
    }
    return localByteArrayOutputStream.toByteArray();
}

From source file:Main.java

private static String toString(InputStream is) throws IOException {

    byte[] bytes = new byte[1024];
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    int lidos;/* ww w  .  j a v  a2 s  .c  o m*/
    while ((lidos = is.read(bytes)) > 0) {
        baos.write(bytes, 0, lidos);
    }
    return new String(baos.toByteArray());
}

From source file:Main.java

public static InputStream getInputStreamFromBitmap(Bitmap bitmap) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
    InputStream isBm = new ByteArrayInputStream(baos.toByteArray());
    return isBm;//from w  w w  .  j  a  va2s  .  c  o m
}

From source file:Main.java

public static byte[] ObjectToByte(Object obj) {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();

    ObjectOutput out = null;/* w ww  .  ja v  a2  s . c o  m*/

    try {
        out = new ObjectOutputStream(bos);

        out.writeObject(obj);

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    byte[] Bytes = bos.toByteArray();

    return Bytes;

}

From source file:Main.java

public static byte[] objectToByte(Object obj) throws Exception {
    ObjectOutputStream oos = null;
    try {//from w w w  .j  a v a 2  s .c  om
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        oos = new ObjectOutputStream(bos);
        oos.writeObject(obj);
        return bos.toByteArray();
    } finally {
        if (oos != null)
            oos.close();
    }
}

From source file:Main.java

public static InputStream Bitmap2InputStream(Bitmap bm, int quality) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bm.compress(Bitmap.CompressFormat.PNG, quality, baos);
    InputStream is = new ByteArrayInputStream(baos.toByteArray());
    return is;/* w ww.  j a va 2  s  .c  o m*/
}

From source file:Main.java

public static byte[] getBitmapData(Bitmap bmp) {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    bmp.compress(CompressFormat.JPEG, 100, bos);
    byte[] bitmapdata = bos.toByteArray();

    return bitmapdata;
}

From source file:Main.java

public static String encodeImageBase64(Bitmap bitmap) {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream);
    return Base64.encodeToString(outputStream.toByteArray(), Base64.DEFAULT);
}

From source file:Main.java

public static byte[] downloadImageFromURL(String strUrl) throws Exception {
    InputStream in;//w  w w  .j  a v a2s . c  o  m
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    URL url = new URL(strUrl);
    in = new BufferedInputStream(url.openStream());
    byte[] buf = new byte[2048];
    int n = 0;
    while (-1 != (n = in.read(buf))) {
        out.write(buf, 0, n);
    }
    out.close();
    in.close();
    byte[] response = out.toByteArray();
    FileOutputStream fos = new FileOutputStream("/Users/image.jpg");
    fos.write(response);
    fos.close();
    return response;
}

From source file:Main.java

private static String getStringFromException(Throwable e) {
    String result = "";
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    PrintStream ps = new PrintStream(bos);
    e.printStackTrace(ps);/*from  w  w w  . ja v  a  2  s  .  com*/
    try {
        result = bos.toString("utf-8");
    } catch (UnsupportedEncodingException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    return result;
}