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 Object deserialize(byte[] bytes) {
    ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
    ObjectInput in = null;/* w w  w  .ja  v  a 2  s  . c  o  m*/
    Object o = null;
    try {
        in = new ObjectInputStream(bis);
        o = in.readObject();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            bis.close();
        } catch (IOException ex) {
            // ignore close exception
        }
        try {
            if (in != null) {
                in.close();
            }
        } catch (IOException ex) {
            // ignore close exception
        }
    }
    return o;
}

From source file:Main.java

public static Object fromByteArray(byte[] inputBytes) {
    Object obj = null;//from   ww w . j  a  v  a2  s.c  om
    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(inputBytes);
    ObjectInput objIn = null;
    try {
        objIn = new ObjectInputStream(byteArrayInputStream);
        obj = objIn.readObject();
    } catch (java.io.IOException ioe) {
        return null;
    } catch (java.lang.ClassNotFoundException cnfe) {
        return null;
    } finally {
        try {
            byteArrayInputStream.close();
            objIn.close();
        } catch (java.io.IOException closeIOE) {

        }
    }
    return obj;
}

From source file:Main.java

public static byte[] compress(byte[] source) throws IOException {
    if (source == null || source.length == 0) {
        return source;
    }/*from   w  w  w  . jav a2s  .  c  o  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:Main.java

public static byte[] decompress(byte[] compressed) throws IOException {
    if (compressed == null || compressed.length == 0) {
        return compressed;
    }/*from www  .  j av  a  2  s.  co 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:femr.business.services.PhotoService.java

/**
 * Decodes a base64 encoded string to an image
 *
 * @param imageString base64 encoded string that has been parsed to only include imageBytes
 * @return the decoded image//from   w  w w . jav a2  s.co  m
 */
private static BufferedImage decodeToImage(String imageString) {

    BufferedImage image = null;
    byte[] imageByte;
    try {
        Base64 newDecoder = new Base64();
        byte[] bytes = imageString.getBytes(Charset.forName("UTF-8"));
        imageByte = newDecoder.decode(bytes);
        ByteArrayInputStream bis = new ByteArrayInputStream(imageByte);
        image = ImageIO.read(bis);
        bis.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return image;
}

From source file:com.evilisn.DAO.CertMapper.java

public static X509Certificate getX509Certificate(byte[] bcert) throws CertificateException, IOException {
    if (bcert == null)
        return null;
    CertificateFactory cf = CertificateFactory.getInstance("X.509");
    ByteArrayInputStream bais = new ByteArrayInputStream(bcert);
    X509Certificate x509cert = (X509Certificate) cf.generateCertificate(bais);

    cf = null;//from   w w  w .  ja  v  a 2s  .c o  m
    bais.close();
    return x509cert;
}

From source file:org.opencb.commons.utils.StringUtils.java

public static String gunzip(byte[] bytes) throws IOException {
    ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
    BufferedInputStream bufis = new BufferedInputStream(new GZIPInputStream(bis));
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    byte[] buffer = new byte[1024];
    int len;/*from   w w w  .java  2s  . c o m*/
    while ((len = bufis.read(buffer)) >= 0) {
        bos.write(buffer, 0, len);
    }
    String retval = bos.toString();
    bis.close();
    bufis.close();
    bos.close();
    return retval;
}

From source file:org.mrgeo.utils.Base64Utils.java

public static Object decodeToObject(String encoded) throws IOException, ClassNotFoundException {
    byte[] objBytes = Base64.decodeBase64(encoded.getBytes());

    ByteArrayInputStream bais = null;
    ObjectInputStream ois = null;

    try {//from ww w . j  av a 2s  .  c  o m
        bais = new ByteArrayInputStream(objBytes);
        ois = new ObjectInputStream(bais);
        Object obj = ois.readObject();

        return obj;
    } finally {
        if (ois != null) {
            ois.close();
        }
        if (bais != null) {
            bais.close();
        }
    }
}

From source file:free.yhc.netmbuddy.share.ExporterPlaylist.java

private static Err exportShareJson(ZipOutputStream zos, JSONObject jo, String shareName) {
    ByteArrayInputStream bais;
    try {/*ww w.ja  v  a2  s .c  o  m*/
        bais = new ByteArrayInputStream(jo.toString().getBytes("UTF-8"));
    } catch (UnsupportedEncodingException e) {
        return Err.UNKNOWN;
    }

    try {
        FileUtils.zip(zos, bais, shareName);
    } catch (IOException e) {
        return Err.IO_FILE;
    } finally {
        try {
            bais.close();
        } catch (IOException ignored) {
        }
    }
    return Err.NO_ERR;
}

From source file:org.openmrs.module.drawing.DrawingUtil.java

/**
 *  Converts a Base64 String to image /*from w ww.  j a v  a 2s . co m*/
 * @param base64Data
 * @return image
 * @throws IOException
 */

public static BufferedImage base64ToImage(String base64Data) throws IOException {

    if (StringUtils.isBlank(base64Data) || !base64Data.contains(",")
            || !(base64Data.split(",")[1].length() % 4 == 0)) {
        log.error("String is not Base64 encoded");
        return null;
    }
    byte[] raw = Base64.decodeBase64(base64Data.split(",")[1]);
    ByteArrayInputStream bs = new ByteArrayInputStream(raw);
    BufferedImage img = null;
    try {
        img = ImageIO.read(bs);
    } catch (IOException e) {
        log.error("Unable to convert Base64 String to image", e);
    } finally {
        bs.close();
    }

    return img;

}