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

public static String getStringFromAssets(String strFileName, Resources resources) {
    String result = null;//  w  w w. j a  va2 s  .c om
    try {
        InputStream in = resources.getAssets().open(strFileName);
        int ch = 0;
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        while ((ch = in.read()) != -1) {
            baos.write(ch);
        }
        byte[] buff = baos.toByteArray();
        baos.close();
        in.close();
        result = new String(buff);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return result;
}

From source file:Main.java

/**
 * convert bitmap into byte[]//  www  .j  a  va 2s .c o m
 * @param bitmap the source bitmap
 * @param quality  Hint to the compressor, 0-100. 0 meaning compress for
 *                 small size, 100 meaning compress for max quality. Some
 *                 formats, like PNG which is lossless, will ignore the
 *                 quality setting
 * @return the byte[]
 */
public static byte[] bitmap2bytes(Bitmap bitmap, int quality) {
    if (null == bitmap)
        return new byte[] {};
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.JPEG, quality, baos);
    byte[] result = baos.toByteArray();
    try {
        baos.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return result;
}

From source file:com.grosscommerce.ICEcat.utilities.Downloader.java

public static byte[] download(String urlFrom, String login, String pwd) throws Throwable {
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    download(urlFrom, login, pwd, os);/* w  ww  . j a  va 2 s .  c  o  m*/
    byte[] result = os.toByteArray();
    os.close();
    Logger.getLogger(Downloader.class.getName()).log(Level.INFO, "Downloaded {0} byte(s)",
            new Object[] { result.length });

    return result;
}

From source file:Main.java

public static String readString(InputStream is) throws IOException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    byte[] buffer = new byte[1024];
    int len = -1;
    while ((len = is.read(buffer)) > 0) {
        bos.write(buffer, 0, len);//www  . j  av  a  2  s  .c  o m
    }
    bos.close();

    return bos.toString();
}

From source file:framework.json2java.Example.java

private static void output(JCodeModel codeModel) throws IOException {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    CodeWriter codeWriter = new SingleStreamCodeWriter(outputStream);
    codeModel.build(codeWriter);/*from  w  w  w  . j a v  a2  s . c om*/
    codeWriter.close();
    outputStream.close();
    String string = outputStream.toString("utf-8");
    System.out.println(string);
}

From source file:Main.java

public static String loadFromAssetsFile(String fname, Context context) {
    String result = null;//from   w  w w .j  a v  a 2  s  . c om
    try {
        InputStream in = context.getAssets().open(fname);
        int ch = 0;
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        while ((ch = in.read()) != -1) {
            baos.write(ch);
        }
        byte[] buff = baos.toByteArray();
        baos.close();
        in.close();
        result = new String(buff, "UTF-8");
        result = result.replaceAll("\\r\\n", "\n");
    } catch (Exception e) {
        e.printStackTrace();
    }
    return result;
}

From source file:Main.java

public static byte[] InputStreamToByte(InputStream is) throws IOException {
    ByteArrayOutputStream bytestream = new ByteArrayOutputStream();
    int ch;/*from   w w w.  jav a  2  s .co m*/
    while ((ch = is.read()) != -1) {
        bytestream.write(ch);
    }
    byte imgdata[] = bytestream.toByteArray();
    bytestream.close();

    return imgdata;
}

From source file:Main.java

public static byte[] InputStreamToByte(InputStream is) throws IOException {

    ByteArrayOutputStream bytestream = new ByteArrayOutputStream();
    int ch;/*from   w ww.  j  a v a  2 s.  c  o m*/
    while ((ch = is.read()) != -1) {
        bytestream.write(ch);
    }
    byte byteData[] = bytestream.toByteArray();
    bytestream.close();
    return byteData;
}

From source file:Main.java

public static byte[] readStream(InputStream is) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    byte[] buf = new byte[1024 * 10];
    int readlen;//from ww w  . ja  v  a 2  s. c o m
    while ((readlen = is.read(buf)) >= 0) {
        baos.write(buf, 0, readlen);
    }
    baos.close();

    return baos.toByteArray();
}

From source file:Main.java

public static byte[] bitmap2Bytes(Bitmap bm) {
    if (bm == null || bm.isRecycled()) {
        return null;
    }/*from   w ww.  j  a  v  a  2  s. com*/
    byte[] bytes;
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
    bytes = baos.toByteArray();
    try {
        baos.flush();
        baos.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return bytes;
}