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[] decompress(byte[] compressed) throws IOException {
    if (compressed == null || compressed.length == 0) {
        return compressed;
    }/*from  w  w  w  .j  av  a  2 s  .  c o  m*/
    ByteArrayInputStream sourceStream = new ByteArrayInputStream(compressed);
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream(compressed.length * 2);
    try (GZIPInputStream compressor = new GZIPInputStream(sourceStream)) {
        ByteStreams.copy(compressor, outputStream);
        compressor.close();
    }
    try {
        return outputStream.toByteArray();
    } finally {
        sourceStream.close();
        outputStream.close();
    }
}

From source file:$.CrmTest.java

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

From source file:Main.java

public static byte[] Compress(String text) throws Exception {
    Deflater compressor = new Deflater();
    byte[] bytes = text.getBytes("UTF-16LE");
    compressor.setInput(bytes);//w  ww .  jav  a  2 s.  c  o m

    // Create an expandable byte array to hold the compressed data
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    compressor.finish();

    byte[] buffer = new byte[1024];
    try {
        while (!compressor.finished()) {
            int count = compressor.deflate(buffer);
            bos.write(buffer, 0, count);
        }
    } finally {
        compressor.finish();
    }

    bos.close();
    return bos.toByteArray();
}

From source file:Main.java

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

From source file:Main.java

public static byte[] getByteArray(InputStream inputstream) {
    try {/*from ww  w.  ja va  2 s  . c o  m*/
        ByteArrayOutputStream bytearrayoutputstream = new ByteArrayOutputStream();
        byte abyte1[] = new byte[1024];
        do {
            int i = inputstream.read(abyte1);
            if (i == -1)
                break;
            bytearrayoutputstream.write(abyte1, 0, i);
        } while (true);
        byte result[] = bytearrayoutputstream.toByteArray();
        bytearrayoutputstream.close();
        inputstream.close();
        return result;
    } catch (IOException e) {
        return null;
    }

}

From source file:com.useekm.types.AbstractGeo.java

public static byte[] gunzip(byte[] bytes) {
    try {/*from w ww.  ja  va 2 s .  co m*/
        ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
        BufferedInputStream bufis = new BufferedInputStream(new GZIPInputStream(bis));
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        byte[] buf = new byte[1024];
        int len;
        while ((len = bufis.read(buf)) > 0)
            bos.write(buf, 0, len);
        byte[] result = bos.toByteArray();
        bufis.close();
        bos.close();
        return result;
    } catch (IOException e) {
        throw new IllegalStateException("Unexpected IOException on inmemory gunzip", e);
    }
}

From source file:Main.java

public static final byte[] uncompress(final byte[] bytes) {
    if (bytes == null) {
        throw new NullPointerException("byte[] is NULL !");
    }/*from   ww w.java2 s. c  om*/
    ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
    ByteArrayOutputStream bos = new ByteArrayOutputStream(8192);
    byte[] buffer = new byte[bytes.length];
    int length;
    try {
        GZIPInputStream gis = new GZIPInputStream(bais);
        while ((length = gis.read(buffer)) != -1) {
            bos.write(buffer, 0, length);
        }
        byte[] moreBytes = bos.toByteArray();
        bos.close();
        bais.close();
        gis.close();
        bos = null;
        bais = null;
        gis = null;
        return moreBytes;
    } catch (IOException e) {
        return null;
    }
}

From source file:com.asual.lesscss.ResourcePackage.java

private static byte[] inflate(byte[] output) throws DataFormatException, IOException {
    Inflater inflater = new Inflater();
    inflater.setInput(output);/* ww  w  .ja va  2  s.  co m*/
    ByteArrayOutputStream baos = new ByteArrayOutputStream(output.length);
    byte[] buf = new byte[1024];
    while (!inflater.finished()) {
        int count = inflater.inflate(buf);
        baos.write(buf, 0, count);
    }
    baos.close();
    return baos.toByteArray();
}

From source file:com.asual.lesscss.ResourcePackage.java

private static byte[] deflate(byte[] input) throws IOException {
    Deflater deflater = new Deflater();
    deflater.setLevel(Deflater.BEST_COMPRESSION);
    deflater.setInput(input);/*from ww w. ja va2 s. c o  m*/
    deflater.finish();
    ByteArrayOutputStream baos = new ByteArrayOutputStream(input.length);
    byte[] buf = new byte[1024];
    while (!deflater.finished()) {
        int count = deflater.deflate(buf);
        baos.write(buf, 0, count);
    }
    baos.close();
    return baos.toByteArray();
}

From source file:SwingResourceManager.java

/**
 * Returns an image encoded by the specified input stream
 * @param is InputStream The input stream encoding the image data
 * @return Image The image encoded by the specified input stream
 *//*from  w  ww .j av  a2  s.co m*/
private static Image getImage(InputStream is) {
    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte buf[] = new byte[1024 * 4];
        while (true) {
            int n = is.read(buf);
            if (n == -1)
                break;
            baos.write(buf, 0, n);
        }
        baos.close();
        return Toolkit.getDefaultToolkit().createImage(baos.toByteArray());
    } catch (Throwable e) {
        return null;
    }
}