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 String tryFormattingString(String s) {
    try {/* www  . j  a va  2s. c  om*/
        ByteArrayInputStream in = new ByteArrayInputStream(s.getBytes());
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        builder.setErrorHandler(new ErrorHandler() {

            @Override
            public void warning(SAXParseException exception) throws SAXException {
            }

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

            @Override
            public void error(SAXParseException exception) throws SAXException {
            }
        });
        Document doc = builder.parse(in);
        TransformerFactory tf = TransformerFactory.newInstance();
        // tf.setAttribute("indent-number", new Integer(2));
        Transformer trans = tf.newTransformer();
        DOMSource source = new DOMSource(doc);
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        StreamResult result = new StreamResult(out);
        trans.setOutputProperty(OutputKeys.INDENT, "yes");
        trans.setOutputProperty(OutputKeys.METHOD, "xml");
        trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");

        trans.transform(source, result);
        return out.toString();

    } catch (Exception e) {
        // Console.println("Could not validate the soapmessage, although I'll show it anyway..");
    }
    return s;
}

From source file:Main.java

public static InputStreamEntity DocToInputStreamEntity(Document doc)
        throws TransformerFactoryConfigurationError, TransformerException {
    return new InputStreamEntity(new ByteArrayInputStream(DocToString(doc).getBytes()), -1);
}

From source file:bench.fix_openfast.RegistryFIX.java

public static TemplateRegistry makeTemplateRegistry(final String templateText) throws Exception {

    final InputStream templateSource = new ByteArrayInputStream(templateText.getBytes("UTF-8"));

    final XMLMessageTemplateLoader templateLoader = new XMLMessageTemplateLoader();

    templateLoader.setLoadTemplateIdFromAuxId(true);

    templateLoader.load(templateSource);

    final TemplateRegistry registry = templateLoader.getTemplateRegistry();

    return registry;

}

From source file:Main.java

static private Document parseDocument(byte[] bytes)
        throws IOException, ParserConfigurationException, SAXException {
    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    documentBuilderFactory.setNamespaceAware(true);

    DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();

    try (InputStream is = new ByteArrayInputStream(bytes)) {
        return documentBuilder.parse(is);
    }/*from  www  .  j  ava  2 s . co m*/
}

From source file:Main.java

public static Object fromBytes(byte[] arr) throws Exception {
    ByteArrayInputStream bais = new ByteArrayInputStream(arr);
    ObjectInputStream ois = new ObjectInputStream(bais);
    return ois.readObject();
}

From source file:Main.java

public static Document createDoc(String xml, String encoding)
        throws ParserConfigurationException, SAXException, IOException {
    ByteArrayInputStream in = new ByteArrayInputStream(xml.getBytes(encoding));

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);/*from ww w .j av a2  s  .  c o  m*/

    DocumentBuilder builder = factory.newDocumentBuilder();
    Document doc = builder.parse(in);
    return doc;
}

From source file:SerializationUtils.java

public static Object deserialize(byte[] bytes) throws ClassNotFoundException {
    try {/*  w  w  w. j  a  v a  2 s .  c om*/
        ByteArrayInputStream input = new ByteArrayInputStream(bytes);
        ObjectInputStream ois = new ObjectInputStream(input);
        return ois.readObject();
    } catch (IOException e) {
        e.printStackTrace();
        throw new RuntimeException("error reading from byte-array!");
    }
}

From source file:Main.java

public static String getQuery(String id) throws Exception {
    InputStream is = new ByteArrayInputStream(xml.getBytes("UTF8"));
    InputSource inputSource = new InputSource(is);
    XPath xpath = XPathFactory.newInstance().newXPath();
    return xpath.evaluate("/queries/query[@id='" + id + "']", inputSource);
}

From source file:Main.java

public static String decompress(String hexString) throws UnsupportedEncodingException {
    final byte[] byteArray = new byte[hexString.length() / 2];
    int k = 0;//from  w  w w.ja v a2 s  .c  o  m
    for (int i = 0; i < byteArray.length; i++) {
        byte high = (byte) (Character.digit(hexString.charAt(k), 16) & 0xff);
        byte low = (byte) (Character.digit(hexString.charAt(k + 1), 16) & 0xff);
        byteArray[i] = (byte) (high << 4 | low);
        k += 2;
    }

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    ByteArrayInputStream in = new ByteArrayInputStream(byteArray);

    try {
        GZIPInputStream gunzip = new GZIPInputStream(in);
        byte[] buffer = new byte[256];
        int n;
        while ((n = gunzip.read(buffer)) >= 0) {
            out.write(buffer, 0, n);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

    return out.toString();
}

From source file:Main.java

/**
 * Convert string to XML {@link Document}
 *
 * @param string - string to be converted
 * @return - {@link Document}/*from   w ww  .  jav a2s .  co m*/
 * @throws Exception - if {@link DocumentBuilder} is not initialized
 */
public static Document stringToXml(String string) throws Exception {
    if (builder == null) {
        throw new Exception("DocumentBuilder is null.");
    }
    return builder.parse(new InputSource(new ByteArrayInputStream(string.getBytes("UTF-8"))));
}