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 String readInputStreamAsString(InputStream is) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    byte[] bytes = new byte[4096];
    int lenRead;//from w w  w . ja v a2 s.c o m
    while ((lenRead = is.read(bytes)) != -1) {
        if (lenRead > 0)
            baos.write(bytes, 0, lenRead);
    }

    if (baos.size() > 0)
        return baos.toString("utf-8");
    return null;
}

From source file:Main.java

public static byte[] InputStreamTOByteArray(InputStream in) throws IOException {
    ByteArrayOutputStream outStream = new ByteArrayOutputStream();
    int count = -1;
    byte[] data = new byte[BUFFER_SIZE];
    while ((count = in.read(data, 0, BUFFER_SIZE)) != -1) {
        outStream.write(data, 0, count);
    }/*from  w w w. j a  v a  2s .com*/
    data = null;
    outStream.close();
    return outStream.toByteArray();

}

From source file:Main.java

public static String bitmaptoString(Bitmap bitmap) {
    String string = null;/*from   w ww  . j av a 2 s.  c o m*/
    ByteArrayOutputStream bStream = new ByteArrayOutputStream();
    bitmap.compress(CompressFormat.PNG, 50, bStream);
    byte[] bytes = bStream.toByteArray();
    string = Base64.encodeToString(bytes, Base64.DEFAULT);
    return string;
}

From source file:Main.java

public static byte[] toBytes(Bitmap bmp) {
    // Default size is 32 bytes
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    try {//  w w  w  .  j  ava  2 s.  c o  m
        bmp.compress(CompressFormat.JPEG, 100, bos);
        bos.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return bos.toByteArray();
}

From source file:Main.java

public static InputStream bitmap2InputStream(Bitmap bm) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
    InputStream is = new ByteArrayInputStream(baos.toByteArray());
    return is;/*from w ww .ja v a  2  s  .  c o m*/
}

From source file:Main.java

public static String objectToString(Serializable serializableObject) throws Exception {
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
    objectOutputStream.writeObject(serializableObject);
    objectOutputStream.close();/*from   ww w.j a v a2s  .  co  m*/
    return new String(Base64.encode(byteArrayOutputStream.toByteArray(), 0));
}

From source file:Main.java

public static Uri getImageUri(Context inContext, Bitmap inImage) {
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
    String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "croppedImage",
            null);/* w  ww . j a  v a 2s.  c o  m*/
    return Uri.parse(path);
}

From source file:Main.java

public static String InputStream2String(final InputStream is) {
    try {/*from   ww  w.j a  v  a 2s  .  c o  m*/
        final ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int length = 0;
        while ((length = is.read(buffer)) != -1) {
            baos.write(buffer, 0, length);
        }
        return baos.toString();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static InputStream bitmap2InputStream4Gif(Bitmap bm) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bm.compress(Bitmap.CompressFormat.WEBP, 100, baos);
    InputStream is = new ByteArrayInputStream(baos.toByteArray());
    return is;//from  w w  w .j  av  a  2 s .com
}

From source file:Main.java

public static String streamToString(InputStream inputStream) {
    try {//from  www .  ja v  a 2  s  .co m
        ByteArrayOutputStream out = new ByteArrayOutputStream();

        byte[] buffer = new byte[1024];
        int len = 0;
        while ((len = inputStream.read(buffer)) != -1) {
            out.write(buffer, 0, len);
            out.flush();
        }

        String result = out.toString();
        out.close();
        inputStream.close();
        return result;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return "";
}