Example usage for java.io ByteArrayOutputStream close

List of usage examples for java.io ByteArrayOutputStream close

Introduction

In this page you can find the example usage for java.io ByteArrayOutputStream close.

Prototype

public void close() throws IOException 

Source Link

Document

Closing a ByteArrayOutputStream has no effect.

Usage

From source file:Main.java

/*************************************************************************************************
 * Reads the stream to the byte array//w w w  . j a v  a 2 s  .  co m
 */
public static byte[] ReadStream(InputStream input) throws IOException {
    final int DEFAULT_INIT_BUF_SIZE = 8192;
    byte[] buffer = new byte[DEFAULT_INIT_BUF_SIZE];
    byte[] arrayOut;
    int bytesRead;

    ByteArrayOutputStream output = new ByteArrayOutputStream();
    while ((bytesRead = input.read(buffer)) != -1) {
        output.write(buffer, 0, bytesRead);
    }
    arrayOut = output.toByteArray();
    output.close();

    return arrayOut;
}

From source file:Main.java

public 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 a va 2s . c om
    }

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

    return result;
}

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();//from w  w w.  j  ava  2s . co m
    }

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

    return result;
}

From source file:Main.java

public static byte[] downloadImageFromURL(String strUrl) throws Exception {
    InputStream in;/*w  w  w.j ava 2 s . 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

public static byte[] toByteArray(InputStream input) throws IOException {
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    write(input, output);//from w w  w .  j  a  v a  2s  .c o m
    output.close();
    return output.toByteArray();
}

From source file:Main.java

public static byte[] toByteArray(Reader input) throws IOException {
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    write(input, output);/*from  w  w  w  . j  a  v  a  2 s  . com*/
    output.close();
    return output.toByteArray();
}

From source file:Main.java

public static byte[] bitmap2ByteArray(final Bitmap bmp, final boolean needRecycle) {
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    bmp.compress(Bitmap.CompressFormat.PNG, 100, output);
    if (needRecycle) {
        bmp.recycle();//from ww w . j ava  2s.  c  om
    }

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

    return result;
}

From source file:Main.java

public static byte[] bmpToByteArray(final Bitmap bmp, final boolean needRecycle) {
    if (bmp == null || bmp.isRecycled())
        return null;
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    bmp.compress(Bitmap.CompressFormat.PNG, 100, output);
    if (needRecycle) {
        bmp.recycle();//from w  w w.ja  v a 2s  .c o m
    }

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

From source file:Main.java

public static byte[] File2byte(String filePath) {
    byte[] buffer = null;
    try {/*from  w  w w.j a v  a2s .c  o  m*/
        File file = new File(filePath);
        FileInputStream fis = new FileInputStream(file);
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        byte[] b = new byte[1024];
        int n;
        while ((n = fis.read(b)) != -1) {
            bos.write(b, 0, n);
        }
        fis.close();
        bos.close();
        buffer = bos.toByteArray();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return buffer;
}

From source file:Main.java

public static String readFromStream(InputStream in) throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    int len = 0;//from w w w.  jav a2 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;
}