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[] compress(byte[] src) throws IOException {

    GZIPOutputStream gzip = null;
    ByteArrayOutputStream baos = null;
    try {/* w w  w. ja  va 2 s . c  o m*/
        baos = new ByteArrayOutputStream();

        gzip = new GZIPOutputStream(baos);
        gzip.write(src);
        gzip.finish();

        return baos.toByteArray();

    } finally {
        if (gzip != null) {
            gzip.close();
        }
        if (baos != null) {
            baos.close();
        }
    }
}

From source file:Main.java

public static String readAssets(Context context, String fileName) {
    InputStream is = null;/*  www .  j a v  a  2  s . c  o  m*/
    String content = null;
    try {
        is = context.getAssets().open(fileName);
        if (is != null) {

            byte[] buffer = new byte[1024];
            ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
            while (true) {
                int readLength = is.read(buffer);
                if (readLength == -1)
                    break;
                arrayOutputStream.write(buffer, 0, readLength);
            }
            is.close();
            arrayOutputStream.close();
            content = new String(arrayOutputStream.toByteArray());

        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
        content = null;
    } finally {
        try {
            if (is != null)
                is.close();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
    }
    return content;
}

From source file:com.ibm.watson.developer_cloud.professor_languo.ingestion.indexing.StackExchangeThreadSerializer.java

/**
 * Serialize a StackExchangeThread into a byte array
 * //w  ww  .j  av a 2s  . co  m
 * @param threadToSerialize - StackExchangeThread to be serialized
 * @return a byte array serialized from the StackExchangeThread
 * @throws IngestionException
 */
public static byte[] serializeThreadToBinArr(StackExchangeThread threadToSerialize) throws IngestionException {
    ByteArrayOutputStream binOut = new ByteArrayOutputStream();
    try {
        ObjectOutputStream out = new ObjectOutputStream(binOut);
        out.writeObject(threadToSerialize);
        out.close();
        binOut.close();
    } catch (IOException e) {
        throw new IngestionException(e);
    }
    return binOut.toByteArray();
}

From source file:Main.java

public static byte[] compress(byte[] source) throws IOException {
    if (source == null || source.length == 0) {
        return source;
    }//from   w ww. ja v  a  2s  .co m
    ByteArrayInputStream sourceStream = new ByteArrayInputStream(source);
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream(source.length / 2);
    try (OutputStream compressor = new GZIPOutputStream(outputStream)) {
        ByteStreams.copy(sourceStream, compressor);
        compressor.close();
    }
    try {
        return outputStream.toByteArray();
    } finally {
        sourceStream.close();
        outputStream.close();
    }
}

From source file:com.facebook.infrastructure.utils.FBUtilities.java

public static byte[] compress(byte[] input) throws IOException {
    // Create an expandable byte array to hold the compressed data.
    // You cannot use an array that's the same size as the orginal because
    // there is no guarantee that the compressed data will be smaller than
    // the uncompressed data.
    ByteArrayOutputStream bos = new ByteArrayOutputStream(input.length);
    compressToStream(input, bos);//ww w .  ja  va2s.  c  o  m
    bos.close();

    // Get the compressed data
    return bos.toByteArray();
}

From source file:com.taobao.tair.etc.TranscoderUtil.java

public static byte[] serialize(Object o) {
    if (o == null) {
        throw new NullPointerException("Can't serialize null");
    }//from   w ww  .ja va  2  s. c o m

    byte[] rv = null;

    try {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutputStream os = new ObjectOutputStream(bos);

        os.writeObject(o);
        os.close();
        bos.close();
        rv = bos.toByteArray();
    } catch (IOException e) {
        throw new IllegalArgumentException("Non-serializable object", e);
    }

    return rv;
}

From source file:Main.java

/**
 * Uncompresses a GZIP file.//  www .  j ava2 s  . c  om
 * 
 * @param bytes
 *            The compressed bytes.
 * @return The uncompressed bytes.
 * @throws IOException
 *             if an I/O error occurs.
 */
public static byte[] gunzip(byte[] bytes) throws IOException {
    /* create the streams */
    InputStream is = new GZIPInputStream(new ByteArrayInputStream(bytes));
    try {
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        try {
            /* copy data between the streams */
            byte[] buf = new byte[4096];
            int len = 0;
            while ((len = is.read(buf, 0, buf.length)) != -1) {
                os.write(buf, 0, len);
            }
        } finally {
            os.close();
        }

        /* return the uncompressed bytes */
        return os.toByteArray();
    } finally {
        is.close();
    }
}

From source file:Main.java

/**
 * returns an XML string.//ww w . j a va  2 s.  c o m
 *
 * @param pNode         Node XML Document node
 * @return              String XML string
 */
public static String getXML(Node pNode) {
    String retString = null;
    try {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        Transformer serializer = TransformerFactory.newInstance().newTransformer();
        serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        serializer.transform(new DOMSource(pNode), new StreamResult(out));
        retString = out.toString();
        out.close();
    } catch (Exception ex) {
    }
    return retString;
}

From source file:com.aerofs.baseline.http.HttpUtils.java

public static byte[] readStreamToBytes(InputStream in) throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();

    try {//from  w  w w .  jav a 2s .  c  o  m
        int bytesRead;
        byte[] chunk = new byte[1024];

        while ((bytesRead = in.read(chunk)) != -1) {
            out.write(chunk, 0, bytesRead);
        }

        return out.toByteArray();
    } finally {
        try {
            out.close();
        } catch (IOException e) {
            // noop
        }
    }
}

From source file:com.example.CrmTest.java

private static String getStringFromInputStream(InputStream in) throws Exception {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    int c = 0;/*from  w ww.  j  a v  a2s. c  o  m*/
    while ((c = in.read()) != -1) {
        bos.write(c);
    }
    in.close();
    bos.close();
    return bos.toString();
}