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 byte[] getBytesFromInput(InputStream in) throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    byte[] buffer = new byte[in.available()];
    in.read(buffer);/* ww  w.  j a  va2 s  .  c o m*/
    out.write(buffer);
    return out.toByteArray();
}

From source file:Main.java

private static byte[] bmpToByteArray(final Bitmap bmp, final boolean needRecycle) {
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    bmp.compress(Bitmap.CompressFormat.PNG, 100, output);
    if (needRecycle) {
        bmp.recycle();/* w  w  w.j av a 2s.  co  m*/
    }

    byte[] result = output.toByteArray();
    try {
        output.close();
    } catch (Exception e) {
    }

    return result;
}

From source file:Main.java

public static byte[] convertToByteArray(Bitmap bmp) {
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bmp.compress(Bitmap.CompressFormat.JPEG, 80, stream);
    byte[] byteArray = stream.toByteArray();
    try {/*  www  .  j  av a 2s. c om*/
        stream.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return byteArray;
}

From source file:Main.java

public static byte[] bitmapToByteArray(Bitmap bmp) {
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bmp.compress(CompressFormat.JPEG, 60, stream);
    byte[] byteArray = stream.toByteArray();

    return byteArray;
}

From source file:Main.java

public static InputStream convertToInputStream(Bitmap bitmap) {

    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);

    byte[] imageInByte = stream.toByteArray();
    System.out.println("........length......" + imageInByte);

    ByteArrayInputStream bis = new ByteArrayInputStream(imageInByte);
    return bis;// w  ww .j a v  a 2  s .c  om
}

From source file:Main.java

public static String bmpToString(Bitmap bitmap) {
    String bmpStr = "";
    try {//from ww  w .  j  a v  a  2 s .  c om
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream);
        byte[] bts = outputStream.toByteArray();
        bmpStr = Base64.encodeToString(bts, Base64.DEFAULT);
    } catch (Exception e) {
        // TODO: handle exception
    }
    return bmpStr;
}

From source file:Main.java

public static byte[] getByteArray(InputStream inputstream) {
    try {//from www .ja v a 2  s. co m
        ByteArrayOutputStream bytearrayoutputstream = new ByteArrayOutputStream();
        byte abyte1[] = new byte[1024];
        do {
            int i = inputstream.read(abyte1);
            if (i == -1)
                break;
            bytearrayoutputstream.write(abyte1, 0, i);
        } while (true);
        byte result[] = bytearrayoutputstream.toByteArray();
        bytearrayoutputstream.close();
        inputstream.close();
        return result;
    } catch (IOException e) {
        return null;
    }

}

From source file:Main.java

public static String inputStream2String(InputStream is) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    int i = -1;//from  w ww . j  av  a  2  s . c om
    while ((i = is.read()) != -1) {
        baos.write(i);
    }
    return baos.toString();
}

From source file:Main.java

public static byte[] bmpToByteArray(final Bitmap bmp, final boolean needRecycle) {
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    bmp.compress(CompressFormat.PNG, 100, output);
    if (needRecycle) {
        bmp.recycle();/*from w ww.j  a v  a  2  s .  c om*/
    }
    byte[] result = output.toByteArray();
    try {
        output.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return result;
}

From source file:Main.java

public static byte[] readStreamContent(InputStream stream) {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    byte[] buf = new byte[1024];

    while (true) {
        try {/*from ww w.  ja  va  2 s.co m*/
            int read = stream.read(buf);
            if (read == -1)
                break;
            bos.write(buf, 0, read);
        } catch (IOException e) {
            return null;
        }
    }
    return bos.toByteArray();
}