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 NodeList evalXpath(String inputXML, String path) throws XPathExpressionException {
    InputStream xmlStream = new ByteArrayInputStream(inputXML.getBytes());
    return evalXpath(xmlStream, path);
}

From source file:Main.java

protected static ByteArrayOutputStream inflate(final ByteArrayOutputStream pOutCompressed) throws IOException {
    final ByteArrayOutputStream uncompressed = new ByteArrayOutputStream(ONE_MB);
    final GZIPInputStream zin = new GZIPInputStream(new ByteArrayInputStream(pOutCompressed.toByteArray()));

    int read;//from www.  j  ava  2s .  c o m
    final byte[] buf = new byte[ONE_MB];
    while ((read = zin.read(buf)) != -1) {
        uncompressed.write(buf, 0, read);
    }

    return uncompressed;
}

From source file:Main.java

public static Document parse(String s) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    builder.setErrorHandler(new ErrorHandler() {
        @Override// w  ww  . j  a  v  a2s  .  c om
        public void warning(SAXParseException exception) throws SAXException {

        }

        @Override
        public void error(SAXParseException exception) throws SAXException {

        }

        @Override
        public void fatalError(SAXParseException exception) throws SAXException {

        }
    });
    Document rv = builder.parse(new ByteArrayInputStream(s.getBytes("UTF-8")));
    return rv;
}

From source file:Main.java

public static InputStream newInputStreamFromDocument(Document doc)
        throws TransformerConfigurationException, TransformerException {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    Source xmlSource = new DOMSource(doc);
    Result outputTarget = new StreamResult(outputStream);
    TransformerFactory.newInstance().newTransformer().transform(xmlSource, outputTarget);
    return new ByteArrayInputStream(outputStream.toByteArray());
}

From source file:Main.java

/**
 * Given an XML string, return the textContent of all of the tags that match tagName as a list.
 * @param xmlStr/*from  ww  w .  ja  v  a2  s . c o m*/
 * @param tagName
 * @method getValuesForNode
 * @static
 */
public static List<String> getValuesForNode(String xmlStr, String tagName) {

    List<String> values = new ArrayList<String>();
    try {

        DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();

        InputStream is = new ByteArrayInputStream(xmlStr.getBytes("UTF-8"));

        Document doc = docBuilder.parse(is);

        doc.getDocumentElement().normalize();

        NodeList nodes = doc.getElementsByTagName(tagName);
        int nodeCount = nodes.getLength();

        for (int i = 0; i < nodeCount; i++) {
            Node node = nodes.item(i);
            values.add(node.getTextContent());
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

    return values;

}

From source file:Main.java

/***
 * Deserialize a xml string into an object
 * /*from ww w  .j av a2 s  .c  om*/
 * @param <T>
 *            type of the object to deserialize to
 * @param xmlString
 *            xml string represents an object
 * @param classToCastTo
 *            class to deserialize to
 * @return object deserialized from the xml
 * @throws SAXParseException
 *             if xmlString is not well formatted
 * @throws ClassCastException
 *             if the object is not the an instance of classToCastTo
 */
public static <T> T deserializeObject(final String xmlString, final Class<T> classToCastTo)
        throws SAXParseException, ClassCastException {

    final InputStream fis = new ByteArrayInputStream(xmlString.getBytes());

    return deserializeObject(fis, classToCastTo);

}

From source file:Main.java

private static InputStream getHelloWorldPython() {
    String contents = "# An exmple python script\r\n"
            + "# Please chose run or debug to run the script with the \r\n"
            + "# python interpreter found automatically by the workbench. \r\n"
            + "# For instance go to 'Run' and choose 'example'. \r\n" + "\r\n" + "def hello():\r\n"
            + "    print \"Hello World!\"\r\n" + "\r\n" + "\r\n" + "hello()";

    return new ByteArrayInputStream(contents.getBytes());
}

From source file:Util.java

public static InputStream toUTF8InputStream(String str) {
    InputStream is = null;/*w  ww  . j a  v  a2  s.c om*/
    try {
        is = new ByteArrayInputStream(str.getBytes("UTF-8"));
    } catch (UnsupportedEncodingException e) {
        // UTF-8 should always be supported
        throw new AssertionError();
    }
    return is;
}

From source file:Main.java

public static X509Certificate loadCertificate(byte[] encodedDerCertificate) throws CertificateException {
    X509Certificate certificate = (X509Certificate) CertificateFactory.getInstance("X.509")
            .generateCertificate(new ByteArrayInputStream(encodedDerCertificate));

    return certificate;
}

From source file:Main.java

/**
 * Makes the specified XML string pretty.
 * /*  w w w. j a  v  a  2 s  .  c  o  m*/
 * @param xmlString
 *            The XML string to process.
 * @return Pretty-printed XML string.
 */
public static final String prettyPrint(final String xmlString) {
    try (InputStream inputStream = new ByteArrayInputStream(xmlString.getBytes());
            ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
        final DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        final Document document = documentBuilder.parse(inputStream);

        final TransformerFactory tfactory = TransformerFactory.newInstance();
        final Transformer serializer = tfactory.newTransformer();
        // Setup indenting to "pretty print"
        serializer.setOutputProperty(OutputKeys.INDENT, "yes");
        serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");

        final DOMSource xmlSource = new DOMSource(document);
        final StreamResult outputTarget = new StreamResult(baos);
        serializer.transform(xmlSource, outputTarget);
        return baos.toString("utf-8");
    } catch (ParserConfigurationException | TransformerException | SAXException | IOException ex) {
        throw new RuntimeException("Can't pretty print xml!", ex);
    }
}