Example usage for java.io ByteArrayInputStream ByteArrayInputStream

List of usage examples for java.io ByteArrayInputStream ByteArrayInputStream

Introduction

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

Prototype

public ByteArrayInputStream(byte buf[]) 

Source Link

Document

Creates a ByteArrayInputStream so that it uses buf as its buffer array.

Usage

From source file:Main.java

public static Element unmarshalDOMElement(byte[] input)
        throws SAXException, IOException, ParserConfigurationException {

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();

    Document doc = db.parse(new ByteArrayInputStream(input));

    return doc.getDocumentElement();
}

From source file:Main.java

/**
 * Unmarshals an XML ({@link String}) to a bean.
 * //from w w  w .j av  a 2  s  .  com
 * @param xml {@link String}
 * @param clazz {@link Class}
 * @return bean
 * @throws JAXBException
 */
public static <T> T unmarshal(String xml, Class<T> clazz) throws JAXBException {
    return unmarshal(new ByteArrayInputStream(xml.getBytes()), clazz);
}

From source file:Main.java

public static byte[] readCompressedData(ByteArrayInputStream bais) {
    int uncompressedLength = getLength(bais);
    byte[] output = new byte[uncompressedLength];
    byte[] compressed = readData(bais);
    GZIPInputStream gs;/*from   ww w  .j a  v a2s  .c o m*/
    try {
        gs = new GZIPInputStream(new ByteArrayInputStream(compressed));
        for (int i = 0; i < uncompressedLength; ++i) {
            output[i] = (byte) gs.read();
        }
    } catch (IOException e) {
        throw new RuntimeException("Cannot decompress", e);
    }

    return output;
}

From source file:Main.java

public static <T> T unmarshalString(final String string, final Class<T> clazz) throws JAXBException {
    return unmarshalObject(new ByteArrayInputStream(string.getBytes()), clazz);
}

From source file:Main.java

public static String prettyXML(String xml, int indent) {
    try {//from w w w.j a  v a  2 s.c  o m
        // Turn xml string into a document
        Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder()
                .parse(new InputSource(new ByteArrayInputStream(xml.getBytes("utf-8"))));

        // Remove whitespaces outside tags
        XPath xPath = XPathFactory.newInstance().newXPath();
        NodeList nodeList = (NodeList) xPath.evaluate("//text()[normalize-space()='']", document,
                XPathConstants.NODESET);

        for (int i = 0; i < nodeList.getLength(); ++i) {
            Node node = nodeList.item(i);
            node.getParentNode().removeChild(node);
        }
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        transformerFactory.setAttribute("indent-number", indent);
        Transformer transformer = transformerFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");

        StringWriter stringWriter = new StringWriter();
        transformer.transform(new DOMSource(document), new StreamResult(stringWriter));
        return stringWriter.toString();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static void printNodesAndAttributes(String xmlStr) {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    try {//from  w ww .java2  s. c om
        logger.info("Xml processing:");
        DocumentBuilder db = dbf.newDocumentBuilder();
        InputStream inStream = new ByteArrayInputStream(xmlStr.getBytes(StandardCharsets.UTF_8));
        // or InputSource inputSource = new InputSource( new StringReader(
        // xmlStr ) );
        Document doc = db.parse(inStream);
        DocumentTraversal dt = (DocumentTraversal) doc;
        NodeIterator i = dt.createNodeIterator(doc, NodeFilter.SHOW_ELEMENT, null, false);
        Node node = i.nextNode();
        while (node != null) {
            logger.info("Node type: " + node.getNodeType() + " Node name: " + node.getNodeName());
            logger.info("    Attributes: " + attributesStr(node));
            node = i.nextNode();
        }
    } catch (Exception ex) {
        logger.error(ex);
    }
}

From source file:Main.java

private static Object bytes2Object(final byte[] bytes) {
    if (bytes == null)
        return null;
    ObjectInputStream ois = null;
    try {//ww w  .j  a v a 2s.  c o m
        ois = new ObjectInputStream(new ByteArrayInputStream(bytes));
        return ois.readObject();
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    } finally {
        try {
            if (ois != null) {
                ois.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

From source file:Main.java

public static Document parse(String xmlStr) throws ParserConfigurationException, SAXException, IOException {

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

    factory.setValidating(false);/*from   w  w  w .  j  a va  2 s .c o m*/

    factory.setNamespaceAware(false);

    factory.setIgnoringComments(true);

    factory.setIgnoringElementContentWhitespace(false);

    factory.setCoalescing(false);

    factory.setExpandEntityReferences(true);

    DocumentBuilder builder = factory.newDocumentBuilder();

    ByteArrayInputStream inputStream = new ByteArrayInputStream(xmlStr.getBytes());

    return builder.parse(inputStream);

}

From source file:Main.java

public static ByteArrayInputStream getRandomDataStream(int length) {
    final Random randGenerator = new Random();
    final byte[] buff = new byte[length];
    randGenerator.nextBytes(buff);/*  w w  w  .  j  a  v a2  s .c o  m*/
    return new ByteArrayInputStream(buff);
}

From source file:Main.java

public static String extractValue(String xml, String xpathExpression) {
    String actual;//  w  w  w. j a va  2 s .  com
    try {
        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        documentBuilderFactory.setNamespaceAware(true);
        documentBuilderFactory.setIgnoringElementContentWhitespace(true);
        DocumentBuilder docBuilder = documentBuilderFactory.newDocumentBuilder();

        byte[] bytes = xml.getBytes("UTF-8");
        InputStream inputStream = new ByteArrayInputStream(bytes);
        Document doc = docBuilder.parse(inputStream);
        XPathFactory xPathFactory = XPathFactory.newInstance();
        XPath xpath = xPathFactory.newXPath();

        actual = xpath.evaluate(xpathExpression, doc, XPathConstants.STRING).toString();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }

    return actual;
}