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 ConvertToString(InputStream is) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    int i = -1;/*www. j  a  va2 s. c o m*/
    try {
        while ((i = is.read()) != -1) {
            baos.write(i);

        }
        return baos.toString();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally {
        try {
            is.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    return "";
}

From source file:Main.java

public static byte[] decodeBitmapToBytes(Bitmap bitmap) {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    bitmap.compress(CompressFormat.PNG, 100, outputStream);
    return outputStream.toByteArray();
}

From source file:Main.java

public static String getImageAsString(Bitmap bmp) {
    ByteArrayOutputStream bYtE = new ByteArrayOutputStream();

    bmp.compress(Bitmap.CompressFormat.JPEG, 100, bYtE);
    byte[] byteArray = bYtE.toByteArray();
    String image = Base64.encodeToString(byteArray, Base64.DEFAULT);
    bmp.recycle();//  w w  w.j  a  va  2 s.  c  o  m
    Log.d("Converted TO : ", image);
    return image;
}

From source file:Main.java

private static byte[] getSizeData(InputStream is) {
    ByteArrayOutputStream byteBuffer;
    try {/*from w  w  w. j ava 2 s. com*/
        byteBuffer = new ByteArrayOutputStream();

        byte[] buffer = new byte[1024];

        int len = 0;
        while ((len = is.read(buffer)) != -1) {
            byteBuffer.write(buffer, 0, len);
        }
        return byteBuffer.toByteArray();
    } catch (Exception e) {
        return new byte[] {};
    }
}

From source file:Main.java

public static byte[] toByteArray(Bitmap bitmap) {
    if (bitmap == null)
        return null;

    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    // Compress image to lower quality scale 1 - 100
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
    byte[] image = stream.toByteArray();
    return image;
}

From source file:Main.java

public static String readTextFile(InputStream inputStream) {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

    byte buf[] = new byte[1024];
    int len;/* ww w. j  a va2  s.c  o  m*/
    try {
        while ((len = inputStream.read(buf)) != -1) {
            outputStream.write(buf, 0, len);
        }
        outputStream.close();
        inputStream.close();
    } catch (IOException e) {

    }
    return outputStream.toString();
}

From source file:Main.java

public static byte[] inputStreamToBytes(InputStream inputStream) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    byte[] buffer = new byte[2048];
    int read = 0;
    while ((read = inputStream.read(buffer, 0, buffer.length)) != -1)
        baos.write(buffer, 0, read);//from   w w w  .j a  v  a2  s  . com
    baos.flush();
    return baos.toByteArray();
}

From source file:Main.java

public static byte[] intToByteArray(int number) {
    try {/* w w  w  . j ava  2 s  . c o  m*/
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        DataOutputStream dos = new DataOutputStream(bos);
        dos.writeInt(number);
        dos.flush();
        // lenth have to be 2 byte
        byte[] d = bos.toByteArray();
        dos.close();

        return d;
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static byte[] bitmapToBytes(Bitmap bm) {
    byte[] bytes = null;
    if (bm != null) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
        bytes = baos.toByteArray();/*from   w  w w. j a  va  2 s .c om*/
    }
    return bytes;
}

From source file:Main.java

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