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:StreamsUtils.java

public static String readString(InputStream stream) throws IOException {
    ByteArrayOutputStream b = new ByteArrayOutputStream();
    int readedBytes;
    byte[] buf = new byte[1024];
    while ((readedBytes = stream.read(buf)) > 0) {
        b.write(buf, 0, readedBytes);//w  ww  .java 2 s  . c o m
    }
    b.close();
    return b.toString();
}

From source file:com.cloud.utils.security.CertificateHelper.java

public static byte[] buildAndSaveKeystore(List<Ternary<String, String, String>> certs, String storePassword)
        throws KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException,
        InvalidKeySpecException {
    KeyStore ks = KeyStore.getInstance("JKS");
    ks.load(null, storePassword != null ? storePassword.toCharArray() : null);

    //name,cert,key
    for (Ternary<String, String, String> cert : certs) {
        if (cert.third() == null) {
            Certificate c = buildCertificate(cert.second());
            ks.setCertificateEntry(cert.first(), c);
        } else {/*from   ww w .  j  a  v a 2  s  . c  o m*/
            Certificate[] c = new Certificate[certs.size()];
            int i = certs.size();
            for (Ternary<String, String, String> ct : certs) {
                c[i - 1] = buildCertificate(ct.second());
                i--;
            }
            ks.setKeyEntry(cert.first(), buildPrivateKey(cert.third()),
                    storePassword != null ? storePassword.toCharArray() : null, c);
        }
    }

    ByteArrayOutputStream os = new ByteArrayOutputStream();
    ks.store(os, storePassword != null ? storePassword.toCharArray() : null);
    os.close();
    return os.toByteArray();
}

From source file:Main.java

/**
 * Read a string content from a stream./*from  www  .j av  a2  s . c o m*/
 * @param in the stream to read from.
 * @return the raw bytes extracted from the stream.
 * @throws Exception if any error occurs.
 */
static byte[] readBytes(InputStream in) throws Exception {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    byte[] buffer = new byte[2048];
    int n;
    while ((n = in.read(buffer)) > 0) {
        out.write(buffer, 0, n);
    }
    out.close();
    return out.toByteArray();
}

From source file:Main.java

public static byte[] gzip(byte[] data) {
    byte[] b = null;
    try {// ww  w.ja  v  a 2  s . com
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        GZIPOutputStream gzip = new GZIPOutputStream(bos);
        gzip.write(data);
        gzip.finish();
        gzip.close();

        b = bos.toByteArray();
        bos.close();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return b;
}

From source file:Main.java

public static byte[] readAll(InputStream is) throws Exception {
    ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
    byte[] buf = new byte[1024];
    int c = is.read(buf);
    while (-1 != c) {
        baos.write(buf, 0, c);/*from w w  w  .j av a2 s  . com*/
        c = is.read(buf);
    }
    baos.flush();
    baos.close();
    return baos.toByteArray();
}

From source file:Main.java

private 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  w  w . j a  v  a 2 s. co  m*/
    }

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

    return result;
}

From source file:Main.java

public static String readStream(InputStream inStream) throws Exception {
    ByteArrayOutputStream outStream = new ByteArrayOutputStream();
    byte[] buffer = new byte[1024];
    int len = -1;
    while ((len = inStream.read(buffer)) != -1) {
        outStream.write(buffer, 0, len);
    }/* w w  w.  j  av  a2  s.co m*/
    outStream.close();
    inStream.close();
    return outStream.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 w w. j  a v  a 2s .c  om
    }

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

    return result;
}

From source file:com.polivoto.networking.ServicioDeIPExterna.java

public static String obtenerIPExterna() {
    String ip = null;/* w  ww.j  a v  a2  s. c  om*/
    try {
        HttpURLConnection con = (HttpURLConnection) new URL(GET_EXTERNAL_HOST).openConnection();
        DataInputStream entrada = new DataInputStream(con.getInputStream());
        int length;
        byte[] chunk = new byte[64];
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        while ((length = entrada.read(chunk)) != -1)
            baos.write(chunk, 0, length);
        ip = baos.toString();
        baos.close();
        entrada.close();
        con.disconnect();
    } catch (IOException e) {
        e.printStackTrace();
    }
    System.out.println("IP exterior: " + ip);
    return ip;
}

From source file:Main.java

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