Example usage for java.io ByteArrayInputStream close

List of usage examples for java.io ByteArrayInputStream close

Introduction

In this page you can find the example usage for java.io ByteArrayInputStream close.

Prototype

public void close() throws IOException 

Source Link

Document

Closing a ByteArrayInputStream has no effect.

Usage

From source file:Main.java

public static byte[] unGzip(byte[] data) {
    byte[] b = null;
    try {/*  ww  w  .  ja  v a  2 s .  c om*/
        ByteArrayInputStream bis = new ByteArrayInputStream(data);
        GZIPInputStream gzip = new GZIPInputStream(bis);
        byte[] buf = new byte[1024];
        int num = -1;
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        while ((num = gzip.read(buf, 0, buf.length)) != -1) {
            baos.write(buf, 0, num);
        }
        b = baos.toByteArray();
        baos.flush();
        baos.close();
        gzip.close();
        bis.close();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return b;
}

From source file:Main.java

public static final byte[] uncompress(final byte[] bytes) {
    if (bytes == null) {
        throw new NullPointerException("byte[] is NULL !");
    }//from w w w  .  j  a  va 2 s  .  c o m
    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:org.apache.myfaces.util.zip.ZipUtils.java

/**
 *///ww w.  j ava 2s.  c om
public static String unzipString(String s) {
    try {
        Base64 base64Codec = new Base64();
        ByteArrayInputStream decodedStream = new ByteArrayInputStream(
                base64Codec.decode(s.getBytes(ZIP_CHARSET)));
        InputStream unzippedStream = new GZIPInputStream(decodedStream);

        StringBuffer buf = new StringBuffer();
        int c;
        while ((c = unzippedStream.read()) != -1) {
            buf.append((char) c);
        }

        unzippedStream.close();
        decodedStream.close();

        return buf.toString();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.lxht.emos.data.cache.intf.HessianUtil.java

/**
 * ???./*  w ww. j  a v a 2 s .  co  m*/
 *
 * @param by ??
 * @return object ?
 * @throws IOException
 */
public static <T extends Object> T deserialize(byte[] by) throws IOException {
    if (by == null) {
        throw new NullPointerException();
    }
    ByteArrayInputStream is = new ByteArrayInputStream(by);
    HessianInput hi = new HessianInput(is);
    Object obj = hi.readObject();
    hi.close();
    is.close();
    return (T) obj;
}

From source file:com.couchbase.client.java.transcoder.TranscoderUtils.java

/**
 * Takes the input content and deserializes it.
 *
 * @param content the content to deserialize.
 * @return the serializable object.//  w  w w . j av a 2 s  .  c o  m
 * @throws Exception if something goes wrong during deserialization.
 */
public static Serializable deserialize(final ByteBuf content) throws Exception {
    byte[] serialized = new byte[content.readableBytes()];
    content.getBytes(0, serialized);
    ByteArrayInputStream bis = new ByteArrayInputStream(serialized);
    ObjectInputStream is = new ObjectInputStream(bis);
    Serializable deserialized = (Serializable) is.readObject();
    is.close();
    bis.close();
    return deserialized;
}

From source file:Main.java

public static Object getAsObject(String key) {
    byte[] data = getAsBinary(key);
    if (data != null) {
        ByteArrayInputStream bais = null;
        ObjectInputStream ois = null;
        try {/*  www .j  a v  a  2s .c  o m*/
            bais = new ByteArrayInputStream(data);
            ois = new ObjectInputStream(bais);
            Object reObject = ois.readObject();
            return reObject;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            try {
                if (bais != null)
                    bais.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (ois != null)
                    ois.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return null;
}

From source file:com.breadwallet.tools.util.BRCompressor.java

public static byte[] bz2Extract(byte[] compressed) {
    if (compressed == null || compressed.length == 0)
        return null;

    ByteArrayInputStream is = new ByteArrayInputStream(compressed);
    InputStream bin = null;//from ww w .j a  va 2 s . c  o m
    try {
        bin = new BZip2CompressorInputStream(is);
        return IOUtils.toByteArray(bin);

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

From source file:com.taobao.tddl.jdbc.atom.common.TAtomConfParser.java

private static Properties parserConfStr2Properties(String data) {
    Properties prop = new Properties();
    if (TStringUtil.isNotBlank(data)) {
        ByteArrayInputStream byteArrayInputStream = null;
        try {//from   ww  w  . j  a v  a 2s  .co  m
            byteArrayInputStream = new ByteArrayInputStream((data).getBytes());
            prop.load(byteArrayInputStream);
        } catch (IOException e) {
            logger.error("parserConfStr2Properties Error", e);
        } finally {
            try {
                byteArrayInputStream.close();
            } catch (IOException e) {
                logger.error("parserConfStr2Properties Error,can not close ByteArrayInputStream", e);
            }
        }
    }
    return prop;
}

From source file:Main.java

/**
 * Convert bytes array to object./*from  w w  w .  j  a  v a2s .  co m*/
 *
 * @param bytes object bytes
 * @param <T> object class
 * @return object
 */
public static <T> T fromBytes(byte[] bytes) {
    Object result = null;
    ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
    ObjectInput in = null;
    try {
        in = new ObjectInputStream(bis);
        result = in.readObject();
    } catch (IOException | ClassNotFoundException e) {
        throw new RuntimeException(e);
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (Exception e) {
            }
        }
        if (bis != null) {
            try {
                bis.close();
            } catch (Exception e) {
            }
        }
    }
    return (T) result;
}

From source file:net.yacy.cora.document.feed.RSSReader.java

public static RSSReader parse(final int maxsize, final byte[] a) throws IOException {

    // check integrity of array
    if (a == null || a.length < 100) {
        return null; // returning null instead of throwing an IOException is expected in most calling methods where a fail is checked against null
    }//from  w w  w  . j  av a 2s  .  c o  m

    // make input stream
    final ByteArrayInputStream bais = new ByteArrayInputStream(a);

    // parse stream
    RSSReader reader = null;
    try {
        reader = new RSSReader(maxsize, bais);
    } catch (final Exception e) {
        throw new IOException("parse exception: " + e.getMessage(), e);
    }
    try {
        bais.close();
    } catch (final IOException e) {
    }
    return reader;
}