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

private static byte[] decompress(byte[] data) throws IOException, DataFormatException {
    Inflater inflater = new Inflater();
    inflater.setInput(data);/*w w  w . j  a  v a  2s . com*/

    ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length);
    byte[] buffer = new byte[1024];
    while (!inflater.finished()) {
        int count = inflater.inflate(buffer);
        outputStream.write(buffer, 0, count);
    }
    outputStream.close();
    byte[] output = outputStream.toByteArray();
    inflater.end();

    // System.out.println("Original: " + data.length + " bytes.");
    // System.out.println("Decompressed: " + output.length + " bytes.");
    return output;
}

From source file:Main.java

private static String readTextFile(InputStream inputStream) {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    byte buf[] = new byte[1024];
    int len;/*from   w  ww  .j a  va2s .co  m*/
    try {
        while ((len = inputStream.read(buf)) != -1) {
            outputStream.write(buf, 0, len);
        }
        outputStream.close();
        inputStream.close();
    } catch (IOException e) {
        Log.i("IO error", e.getMessage());
        return "Sorry, help file not found.";
    }
    return outputStream.toString();
}

From source file:com.giacomodrago.immediatecrypt.messagecipher.Compression.java

public static byte[] compress(byte[] plaintext) {
    try {/*from ww  w  . j  a v  a2 s. co  m*/
        ByteArrayOutputStream writer = new ByteArrayOutputStream();
        GZIPOutputStream gzipStream = new GZIPOutputStream(writer);
        gzipStream.write(plaintext);
        gzipStream.flush();
        gzipStream.close();
        writer.close();
        return writer.toByteArray();
    } catch (IOException ex) {
        throw new RuntimeException(ex);
    }
}

From source file:Main.java

public static String BitmapToBase64(Bitmap bitmap) {

    String result = null;/* www. jav a2s . c om*/
    ByteArrayOutputStream baos = null;
    try {
        if (bitmap != null) {
            baos = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.JPEG, 60, baos);

            baos.flush();
            baos.close();

            byte[] bitmapBytes = baos.toByteArray();
            result = Base64.encodeToString(bitmapBytes, Base64.DEFAULT);
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (baos != null) {
                baos.flush();
                baos.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return result;
}

From source file:com.buddycloud.mediaserver.business.util.ImageUtils.java

public static byte[] imageToBytes(BufferedImage image, String imageFormat) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ImageIO.write(image, imageFormat, baos);
    baos.flush();//from w  ww.j a va 2 s.  co m

    byte[] imageInByte = baos.toByteArray();
    baos.close();

    return imageInByte;
}

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

public static String obtenerIPServidorRemoto() {
    String ip = null;/*w  ww.  j a  v a2 s.c  o  m*/
    try {
        HttpURLConnection con = (HttpURLConnection) new URL(REMOTE_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);
        JSONObject json = new JSONObject(baos.toString());
        baos.close();
        entrada.close();
        con.disconnect();
        ip = json.getString("content");
    } catch (JSONException | IOException e) {
        e.printStackTrace();
    }
    System.out.println("IP servidor remoto: " + ip);
    return ip;
}

From source file:Main.java

public static String convertIconToString(Bitmap bitmap) {
    String result = null;/*from  w  ww  . ja v  a2 s  .  c  om*/
    ByteArrayOutputStream baos = null;
    try {
        if (bitmap != null) {
            baos = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);

            baos.flush();
            baos.close();

            byte[] bitmapBytes = baos.toByteArray();
            result = Base64.encodeToString(bitmapBytes, Base64.DEFAULT);
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (baos != null) {
                baos.flush();
                baos.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return result;

}

From source file:Main.java

public static String documentToString(Document doc)
        throws TransformerConfigurationException, TransformerException, IOException {
    //return doc.getDocumentElement().toString();

    TransformerFactory factory = TransformerFactory.newInstance();
    Transformer transformer = factory.newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");

    DOMSource src = new DOMSource(doc);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {/*from  ww  w.j  a  v a2  s.c om*/
        StreamResult sr = new StreamResult(baos);
        transformer.transform(src, sr);

        String result = baos.toString();
        return result;
    } finally {
        baos.close();
    }
}

From source file:Main.java

public static String readInStream(InputStream in) {
    try {/*from   w  w  w .j  a  v a2 s  . c  o  m*/
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        byte[] buffer = new byte[512];
        int length = -1;
        while ((length = in.read(buffer)) != -1) {
            out.write(buffer, 0, length);
        }
        out.close();
        in.close();
    } catch (IOException e) {
        Log.e("FileTest", e.getMessage());
    }
    return null;
}

From source file:Main.java

public static String readFromStream(InputStream is) throws IOException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    byte[] buff = new byte[1024];
    int len = 0;//from  www  . j a v a 2 s  . c  o m
    while ((len = is.read(buff)) != -1) {
        bos.write(buff, 0, len);
    }
    is.close();
    String result = bos.toString();
    bos.close();
    return result;
}