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 byte[] toByte(InputStream input) throws IOException {
    byte[] buf = new byte[1024];
    int len = -1;
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    while ((len = input.read(buf)) != -1) {
        output.write(buf, 0, len);/*from w  w w .ja  va  2  s .  com*/
    }
    byte[] data = output.toByteArray();
    output.close();
    input.close();
    return data;
}

From source file:Main.java

public static String objectToString(Serializable object) {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    try {/*  w  ww.  j a  v  a 2 s  .  co  m*/
        new ObjectOutputStream(out).writeObject(object);
        byte[] data = out.toByteArray();
        out.close();

        out = new ByteArrayOutputStream();
        Base64OutputStream b64 = new Base64OutputStream(out, Base64.DEFAULT);
        b64.write(data);
        b64.close();
        out.close();

        return new String(out.toByteArray());
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static byte[] compressBitmap(Bitmap bitmap) {
    ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
    if (bitmap.compress(Bitmap.CompressFormat.JPEG, 80, byteStream)) {
        try {//from w w  w  .j a  v  a  2 s .c o  m
            byteStream.flush();
            byteStream.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    return byteStream.toByteArray();
}

From source file:Main.java

/**
 * Serialize an XML Element into a String.
 * @param e Element to be serialized./*from w ww  .  j  a  v  a2 s .c  o  m*/
 * @param omitXMLDeclaration boolean representing whether or not to omit the XML declaration.
 * @return String representation of the XML document fragment.
 */
public static final String serialize(Element e, boolean omitXMLDeclaration) {
    if (e != null) {
        try {
            DOMSource domSource = new DOMSource(e);
            Transformer serializer = TransformerFactory.newInstance().newTransformer();
            serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION,
                    ((omitXMLDeclaration) ? "yes" : "no"));
            serializer.setOutputProperty(OutputKeys.INDENT, "yes");
            serializer.setOutputProperty(OutputKeys.ENCODING, UTF8_ENCODING);
            serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            serializer.transform(domSource, new StreamResult(baos));
            baos.close();
            return new String(baos.toByteArray(), UTF8_ENCODING);
        } catch (Throwable t) {
        }
    }
    return null;
}

From source file:Main.java

/**
 * Serialize an XML Element into a String.
 * @param df DocumentFragment to be serialized.
 * @param omitXMLDeclaration boolean representing whether or not to omit the XML declaration.
 * @return String representation of the XML document fragment.
 *///from  w  ww. jav  a 2 s  . c o m
public static final String serialize(DocumentFragment df, boolean omitXMLDeclaration) {
    if (df != null) {
        try {
            DOMSource domSource = new DOMSource(df);
            Transformer serializer = TransformerFactory.newInstance().newTransformer();
            serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION,
                    ((omitXMLDeclaration) ? "yes" : "no"));
            serializer.setOutputProperty(OutputKeys.INDENT, "yes");
            serializer.setOutputProperty(OutputKeys.ENCODING, UTF8_ENCODING);
            serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            serializer.transform(domSource, new StreamResult(baos));
            baos.close();
            return new String(baos.toByteArray(), UTF8_ENCODING);
        } catch (Throwable t) {
        }
    }
    return null;
}

From source file:Main.java

public static String exception2String(Exception e) {
    ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
    PrintWriter err = new PrintWriter(arrayOutputStream);
    e.printStackTrace(err);//from  ww  w.  j a  v  a  2 s  . c o m
    err.close();
    try {
        arrayOutputStream.close();
    } catch (IOException e1) {
    }
    return arrayOutputStream.toString();
}

From source file:Main.java

public static byte[] convertIntToByteArray(int my_int) throws IOException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    DataOutputStream dos = new DataOutputStream(bos);
    dos.writeInt(my_int);
    dos.close();//  ww w. j av  a 2 s  .c o m
    byte[] int_bytes = bos.toByteArray();
    bos.close();
    return int_bytes;
}

From source file:Main.java

public static byte[] readInput(InputStream in) throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    int len = 0;//  www. j  a v a  2  s .  c  o m
    byte[] buffer = new byte[1024];
    while ((len = in.read(buffer)) > 0) {
        out.write(buffer, 0, len);
    }
    out.close();
    in.close();
    return out.toByteArray();
}

From source file:Main.java

public static byte[] ObjectToByte(Serializable obj) {
    byte[] bytes = null;
    try {//from  w w  w  . ja v  a2 s.  c o  m
        // object to bytearray
        ByteArrayOutputStream bo = new ByteArrayOutputStream();
        ObjectOutputStream oo = new ObjectOutputStream(bo);
        oo.writeObject(obj);

        bytes = bo.toByteArray();

        bo.close();
        oo.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return bytes;
}

From source file:Main.java

public static byte[] getBytes(Object obj) throws IOException {
    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    ObjectOutputStream out = new ObjectOutputStream(bout);
    out.writeObject(obj);/*from   ww  w  .jav  a  2 s  . co  m*/
    out.flush();
    byte[] bytes = bout.toByteArray();
    bout.close();
    out.close();
    return bytes;
}