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:org.mrgeo.utils.Base64Utils.java

public static double[] decodeToDoubleArray(String encoded) throws IOException {
    byte[] objBytes = Base64.decodeBase64(encoded.getBytes());

    ByteArrayInputStream bais = null;
    ObjectInputStream ois = null;

    try {// w w w  .jav a2s .  co m
        bais = new ByteArrayInputStream(objBytes);
        ois = new ObjectInputStream(bais);
        int arrayLength = ois.readInt();
        double result[] = new double[arrayLength];
        for (int i = 0; i < arrayLength; i++) {
            result[i] = ois.readDouble();
        }
        return result;
    } finally {
        if (ois != null) {
            ois.close();
        }
        if (bais != null) {
            bais.close();
        }
    }
}

From source file:com.mobicage.rogerthat.mfr.dal.Streamable.java

protected static Object fromBase64(String base64String) {
    try {//from ww  w  .ja  va2s. c o m
        ByteArrayInputStream bis = new ByteArrayInputStream(Base64.decodeBase64(base64String));

        try {
            ZipInputStream zip = new ZipInputStream(bis);
            try {
                zip.getNextEntry();
                ObjectInput in = new ObjectInputStream(zip);
                try {
                    return in.readObject();
                } finally {
                    in.close();
                }
            } finally {
                zip.close();
            }
        } finally {
            bis.close();
        }
    } catch (ClassNotFoundException e) {
        log.log((Level.SEVERE), "ClassNotFoundException while deserializing member", e);
        return null;
    } catch (IOException e) {
        log.log((Level.SEVERE), "IOException while deserializing member", e);
        return null;
    }
}

From source file:org.nuxeo.theme.webwidgets.Utils.java

public static List<WidgetFieldType> extractSchema(String html) {
    final List<WidgetFieldType> schema = new ArrayList<WidgetFieldType>();
    Matcher matcher = preferencesPattern.matcher(html);
    if (matcher.find()) {
        html = matcher.group(0);/*  w  w w .  j a  va2  s .  c  o  m*/
    } else {
        return schema;
    }
    final DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

    try {
        dbf.setFeature("http://xml.org/sax/features/validation", false);
        dbf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
    } catch (ParserConfigurationException e) {
        log.debug("Could not set DTD non-validation feature");
    }

    final Document document;
    ByteArrayInputStream in = null;
    try {
        in = new ByteArrayInputStream(html.getBytes());
        final DocumentBuilder db = dbf.newDocumentBuilder();
        document = db.parse(in);
    } catch (Exception e) {
        log.error("Could not parse widget code." + e);
        return schema;
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
            }
        }
    }

    final String[] attrNames = { "label", "type", "name", "defaultValue", "min", "max", "step", "onchange" };
    final NodeList nodes = document.getElementsByTagName("widget:preferences");
    if (nodes.getLength() != 1) {
        return schema;
    }
    final Node node = nodes.item(0);
    final NodeList childNodes = node.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
        final Node n = childNodes.item(i);
        if (n.getNodeType() == Node.ELEMENT_NODE && n.getNodeName().equals("preference")) {
            final WidgetFieldType widgetFieldType = new WidgetFieldType();
            final NamedNodeMap attrs = n.getAttributes();
            final Node typeAttr = attrs.getNamedItem("type");
            for (String attrName : attrNames) {
                Node attrNode = attrs.getNamedItem(attrName);
                if (attrNode != null) {
                    Field field;
                    try {
                        field = widgetFieldType.getClass().getField(attrName);
                    } catch (Exception e) {
                        log.error("Could not access field: " + attrName + e);
                        continue;
                    }
                    final String value = attrNode.getNodeValue();
                    try {
                        field.set(widgetFieldType, value);
                    } catch (Exception e) {
                        log.error("Could not set field: " + attrName);
                        continue;
                    }

                    if (typeAttr != null && typeAttr.getNodeValue().equals("list")) {
                        NodeList optionNodes = n.getChildNodes();
                        List<WidgetFieldType.Option> options = new ArrayList<WidgetFieldType.Option>();
                        for (int j = 0; j < optionNodes.getLength(); j++) {
                            final Node optionNode = optionNodes.item(j);
                            if (optionNode.getNodeType() == Node.ELEMENT_NODE
                                    && optionNode.getNodeName().equals("option")) {
                                final NamedNodeMap optionAttrs = optionNode.getAttributes();
                                Node optionLabelAttr = optionAttrs.getNamedItem("label");
                                Node optionValueAttr = optionAttrs.getNamedItem("value");
                                if (optionLabelAttr != null && optionValueAttr != null) {
                                    options.add(widgetFieldType.new Option(optionLabelAttr.getNodeValue(),
                                            optionValueAttr.getNodeValue()));
                                }
                            }
                        }
                        widgetFieldType.setOptions(options);
                    }
                }
            }
            schema.add(widgetFieldType);
        }
    }
    return schema;
}

From source file:org.kalypso.ogc.gml.GisTemplateHelper.java

public static void saveGisMapView(final Gismapview mapview, final IFile mapFile, final String encoding)
        throws JAXBException, IOException, CoreException {

    final ByteArrayOutputStream bos = new ByteArrayOutputStream();
    GisTemplateHelper.saveGisMapView(mapview, bos, encoding);
    bos.close();/*from  www.ja  v a  2  s . c  o m*/

    final ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
    if (mapFile.exists())
        mapFile.setContents(bis, false, true, new NullProgressMonitor());
    else
        mapFile.create(bis, true, new NullProgressMonitor());

    bis.close();
}

From source file:org.fuin.units4j.Units4JUtils.java

/**
 * Deserializes a byte array to an object. A <code>null</code> argument
 * returns <code>null</code>.
 * //from  w w  w  . j ava  2  s. c om
 * @param data
 *            Byte array to deserialize.
 * 
 * @return Object created from data.
 * 
 * @param <T>
 *            Type of returned data.
 */
@SuppressWarnings("unchecked")
public static <T> T deserialize(final byte[] data) {
    if (data == null) {
        return null;
    }
    try {
        final ByteArrayInputStream bais = new ByteArrayInputStream(data);
        try {
            final ObjectInputStream in = new ObjectInputStream(bais);
            return (T) in.readObject();
        } finally {
            bais.close();
        }
    } catch (final ClassNotFoundException ex) {
        throw new RuntimeException(ex);
    } catch (final IOException ex) {
        throw new RuntimeException(ex);
    }
}

From source file:com.ery.ertc.estorm.util.ToolUtil.java

public static Object deserializeObject(String serStr, boolean isGzip, boolean urlEnCode) throws IOException {
    byte[] bts = null;
    if (urlEnCode) {
        bts = org.apache.commons.codec.binary.Base64.decodeBase64(serStr.getBytes("ISO-8859-1"));
    } else {// www  . j a v a2s.c o  m
        bts = serStr.getBytes("ISO-8859-1");
    }
    if (isGzip)
        bts = GZIPUtils.unzip(bts);
    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bts);
    ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);
    try {
        return objectInputStream.readObject();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
        throw new IOException(e);
    } finally {
        objectInputStream.close();
        byteArrayInputStream.close();
    }
}

From source file:es.juntadeandalucia.panelGestion.negocio.utiles.Utils.java

public static byte[] compressXMLFiles(XMLFileVO[] xmlFiles) throws TransformerException, IOException {
    byte[] compressedFiles = null;

    ByteArrayOutputStream byteOS = new ByteArrayOutputStream();
    ZipOutputStream zipOS = new ZipOutputStream(byteOS);
    for (XMLFileVO xmlFile : xmlFiles) {
        ZipEntry entry = new ZipEntry(xmlFile.getName());
        zipOS.putNextEntry(entry);/*from w ww .  jav a2  s  .c  o  m*/
        byte[] xml = documentToByte(xmlFile.getXml());
        ByteArrayInputStream byteIS = new ByteArrayInputStream(xml);
        IOUtils.copy(byteIS, zipOS);
        byteIS.close();
        zipOS.closeEntry();
    }
    zipOS.close();

    compressedFiles = byteOS.toByteArray();

    return compressedFiles;
}

From source file:Main.java

@Nullable
public static String decompress(String input) {
    StringBuilder sb = new StringBuilder();
    ByteArrayInputStream is = null;
    GZIPInputStream gis = null;//from   w w w  .j  a va 2  s.  com
    try {
        final byte[] bytes = Base64.decode(input, Base64.DEFAULT);
        is = new ByteArrayInputStream(bytes);
        gis = new GZIPInputStream(is, BUFFER_SIZE);

        int cache;
        final byte[] data = new byte[BUFFER_SIZE];
        while ((cache = gis.read(data)) != -1) {
            sb.append(new String(data, 0, cache));
        }
    } catch (IOException e) {
        return null;
    } finally {
        try {
            if (gis != null)
                gis.close();
            if (is != null)
                is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return sb.toString();
}

From source file:org.apache.ojb.broker.Identity.java

/**
 * Factory method that returns an Identity object created from a serializated representation.
 * //from  w ww. j a  v a2 s . co m
 * @param anArray The serialized representation
 * @return The identity
 * @see {@link #serialize}.
 * @deprecated
 */
public static Identity fromByteArray(final byte[] anArray) throws PersistenceBrokerException {
    // reverse of the serialize() algorithm:
    // read from byte[] with a ByteArrayInputStream, decompress with
    // a GZIPInputStream and then deserialize by reading from the ObjectInputStream
    try {
        final ByteArrayInputStream bais = new ByteArrayInputStream(anArray);
        final GZIPInputStream gis = new GZIPInputStream(bais);
        final ObjectInputStream ois = new ObjectInputStream(gis);
        final Identity result = (Identity) ois.readObject();
        ois.close();
        gis.close();
        bais.close();
        return result;
    } catch (Exception ex) {
        throw new PersistenceBrokerException(ex);
    }
}

From source file:com.netsteadfast.greenstep.util.SimpleUtils.java

/**
 * Decode string to image/*from  w  ww  . j  a  v a2s.  c  o m*/
 * @param imageString The string to decode
 * @return decoded image
 */
public static BufferedImage decodeToImage(String imageString) {
    BufferedImage image = null;
    try {
        ByteArrayInputStream bis = new ByteArrayInputStream(DatatypeConverter.parseBase64Binary(imageString));
        image = ImageIO.read(bis);
        bis.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return image;
}