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[] compressToBytes(Bitmap bitmap, int quality) {
    ByteArrayOutputStream baos = null;
    try {/*from  ww  w  . j ava 2  s  .  co  m*/
        baos = new ByteArrayOutputStream(65536);
        bitmap.compress(CompressFormat.PNG, quality, baos);
        return baos.toByteArray();
    } finally {
        try {
            baos.flush();
            baos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

From source file:com.predic8.membrane.core.util.ByteUtil.java

public static byte[] getDecompressedData(byte[] compressedData) throws IOException {
    Inflater decompressor = new Inflater(true);
    decompressor.setInput(compressedData);

    byte[] buf = new byte[1024];

    List<Chunk> chunks = new ArrayList<Chunk>();

    while (!decompressor.finished()) {
        int count;
        try {/*from  ww w. j a  va2  s. c o  m*/
            count = decompressor.inflate(buf);
        } catch (DataFormatException e) {
            throw new IOException(e);
        }
        if (buf.length == count) {
            Chunk chunk = new Chunk(buf);
            chunks.add(chunk);
        } else if (count < buf.length) {
            byte[] shortContent = new byte[count];
            for (int j = 0; j < count; j++) {
                shortContent[j] = buf[j];
            }
            Chunk chunk = new Chunk(shortContent);
            chunks.add(chunk);
        }
    }

    log.debug("Number of decompressed chunks: " + chunks.size());
    if (chunks.size() > 0) {

        ByteArrayOutputStream bos = new ByteArrayOutputStream();

        for (Chunk chunk : chunks) {
            chunk.write(bos);
        }

        try {
            bos.close();
        } catch (IOException e) {
        }
        return bos.toByteArray();
    }

    return null;
}

From source file:HexDecoder.java

/**
 * Decodes the HEX input data producing a output stream.
 * @param data Input data to be decoded.
 * @return A byte array representing the decoded input data.
 *//*w  ww  .  j  av a2s  . c  o m*/
public static byte[] decode(final String data) {
    try {
        final ByteArrayOutputStream out = new ByteArrayOutputStream();
        decode(data, out);
        out.close();
        return out.toByteArray();
    } catch (IOException e) {
        e.printStackTrace();
        throw new RuntimeException(e.getMessage(), e);
    }
}

From source file:com.googlesource.gerrit.plugins.github.velocity.VelocityStaticServlet.java

private static byte[] readResource(final Resource p) throws IOException {
    final InputStream in = p.getResourceLoader().getResourceStream(p.getName());
    ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
    try {/*  www . j  a  va2 s.c om*/
        IOUtils.copy(in, byteOut);
    } finally {
        in.close();
        byteOut.close();
    }

    return byteOut.toByteArray();
}

From source file:Main.java

public static byte[] decompressInGzip(byte[] compressData, int offset, int length) throws Exception {

    ByteArrayInputStream bis = new ByteArrayInputStream(compressData, offset, length);
    GZIPInputStream gzipInStream = new GZIPInputStream(bis);

    ByteArrayOutputStream bos = new ByteArrayOutputStream();

    int count;/*from   www  . j a v a2  s  .com*/
    byte[] buf = new byte[1024];
    while ((count = gzipInStream.read(buf)) > 0) {
        bos.write(buf, 0, count);
    }
    gzipInStream.close();

    byte[] originalData = bos.toByteArray();
    bos.close();

    return originalData;
}

From source file:Main.java

/**
 * Decodes the given Base64 encoded String to a new byte array. The byte array holding the
 * decoded data is returned./*  w  ww  .ja v a  2 s.  c om*/
 */

public static byte[] decode(String s) {

    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    try {
        decode(s, bos);
    } catch (IOException e) {
        throw new RuntimeException();
    }
    byte[] decodedBytes = bos.toByteArray();
    try {
        bos.close();
        bos = null;
    } catch (IOException ex) {
        System.err.println("Error while decoding BASE64: " + ex.toString());
    }
    return decodedBytes;
}

From source file:Main.java

private static String readInStream(FileInputStream inStream) {
    try {/*ww w.java  2s .co m*/
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        byte[] buffer = new byte[512];
        int length = -1;
        while ((length = inStream.read(buffer)) != -1) {
            outStream.write(buffer, 0, length);
        }

        outStream.close();
        inStream.close();
        return outStream.toString();
    } catch (IOException e) {
        Log.i("FileTest", e.getMessage());
    }
    return null;
}

From source file:Main.java

public static byte[] InputStram2byteArray(InputStream in) {
    try {//w  ww. ja  va  2 s.c o m
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        if (in != null) {
            byte[] buffer = new byte[1024];
            int length = 0;
            while ((length = in.read(buffer)) != -1) {
                out.write(buffer, 0, length);
            }
            out.close();
            in.close();
            return out.toByteArray();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    return null;
}

From source file:Main.java

public static byte[] transform(Transformer transformer, Document useDocument) {
    byte[] returnData = new byte[0];
    try {/*from  w  w  w.  j av  a2s .  c o  m*/
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        DOMSource domSource = new DOMSource(useDocument.getDocumentElement());
        transformer.transform(domSource, new StreamResult(baos));

        returnData = baos.toByteArray();

        baos.close();
    } catch (TransformerException te) {
        String err = new String(te.toString());
        returnData = err.getBytes();
        te.printStackTrace();
    } catch (IOException ie) {
        String err = new String(ie.toString());
        returnData = err.getBytes();
        ie.printStackTrace();
    }
    return returnData;
}

From source file:eu.planets_project.pp.plato.util.FileUtils.java

public static byte[] inputStreamToBytes(InputStream in) throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream(1024);
    byte[] buffer = new byte[1024];
    int len;/*from  www.  ja  va2 s .  c o  m*/

    while ((len = in.read(buffer)) >= 0)
        out.write(buffer, 0, len);

    in.close();
    out.close();
    return out.toByteArray();
}