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 Document getDocument(String xml) throws ParserConfigurationException, SAXException, IOException {
    DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
    domFactory.setNamespaceAware(true);//from  ww w  .j  av a  2s  . c om
    DocumentBuilder builder = domFactory.newDocumentBuilder();
    return builder.parse(new ByteArrayInputStream(xml.getBytes("UTF-8")));
}

From source file:Main.java

/**
 * Converts a String representing an XML snippet into an {@link org.w3c.dom.Element}.
 *
 * @param xml snippet as a string//from w w w  .  j av  a 2  s  .c  om
 * @return a DOM Element
 * @throws Exception if unable to parse the String or if it doesn't contain valid XML.
 */
public static Element stringToElement(String xml) throws Exception {
    ByteArrayInputStream bais = new ByteArrayInputStream(xml.getBytes("utf8"));
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document d = builder.parse(bais);
    bais.close();
    return d.getDocumentElement();
}

From source file:Main.java

public static Object getObject(ByteBuffer byteBuffer) throws ClassNotFoundException, IOException {
    InputStream input = new ByteArrayInputStream(byteBuffer.array());
    ObjectInputStream oi = new ObjectInputStream(input);
    Object obj = oi.readObject();
    input.close();//from   ww  w . j a va2  s.  c  o  m
    oi.close();
    byteBuffer.clear();
    return obj;
}

From source file:Main.java

/**
 * Degzips <strong>all</strong> of the datain the specified {@link ByteBuffer}.
 *
 * @param compressed The compressed buffer.
 * @return The decompressed array./*  w w  w  .  j  a v  a  2 s  .c  om*/
 * @throws IOException If there is an error decompressing the buffer.
 */
public static byte[] degzip(byte[] compressed) throws IOException {
    try (InputStream is = new GZIPInputStream(new ByteArrayInputStream(compressed));
            ByteArrayOutputStream out = new ByteArrayOutputStream()) {
        byte[] buffer = new byte[1024];

        while (true) {
            int read = is.read(buffer, 0, buffer.length);
            if (read == -1) {
                break;
            }

            out.write(buffer, 0, read);
        }

        return out.toByteArray();
    }
}

From source file:Main.java

public static InputStream docToInputStream(Document doc) throws 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

public static Document parse(String document) throws SAXException, IOException {
    return parse(new ByteArrayInputStream(document.getBytes()));
}

From source file:Main.java

public static Document parse(String xml) {
    try {/*from w ww.  j  a va 2s .  c  o m*/
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        ByteArrayInputStream stream = new ByteArrayInputStream(xml.getBytes("UTF-8"));
        Document document = db.parse(stream);
        return document;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static void writeFile(File file, byte[] content) throws IOException {
    if (!file.exists()) {
        try {/*w  w w.  j  a  v a2  s. c o  m*/
            file.createNewFile();
        } catch (IOException e) {
            throw new IOException("not crete file=" + file.getAbsolutePath());
        }
    }
    FileOutputStream fileOutputStream = null;
    ByteArrayInputStream bis = null;
    try {
        bis = new ByteArrayInputStream(content);
        fileOutputStream = new FileOutputStream(file, false);
        byte[] buffer = new byte[1024];
        int length = 0;
        while ((length = bis.read(buffer)) != -1) {
            fileOutputStream.write(buffer, 0, length);
        }
        fileOutputStream.flush();
    } finally {
        if (fileOutputStream != null) {
            fileOutputStream.close();
        }
        if (bis != null) {
            bis.close();
        }
    }
}

From source file:Main.java

public static Document returnDocFromString(String str)
        throws ParserConfigurationException, UnsupportedEncodingException, SAXException, IOException {
    Document document = null;//from  w  ww.  j  a v a2  s . c  om
    DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    document = db.parse(new ByteArrayInputStream(str.getBytes("UTF-8")));
    return document;
}

From source file:Main.java

public static Document returnDocFromStringUTF16(String str)
        throws ParserConfigurationException, UnsupportedEncodingException, SAXException, IOException {
    Document document = null;//  w w  w .j av  a2  s  .  co  m
    DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    document = db.parse(new ByteArrayInputStream(str.getBytes("utf-16")));
    return document;
}