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 buildDoc(String content) throws Exception {

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document dom = db.parse(new ByteArrayInputStream(content.getBytes("UTF-8")));
    Element docEle = dom.getDocumentElement();

    return docEle;
}

From source file:Main.java

@SuppressWarnings("unchecked")
static <T extends Serializable> T fromBlob(Class<T> clazz, byte[] blob)
        throws IOException, ClassNotFoundException {
    T result = null;// w  ww.  ja  va2 s. co  m
    ObjectInputStream ois = null;
    try {
        ByteArrayInputStream bos = new ByteArrayInputStream(blob);
        ois = new ObjectInputStream(bos);
        result = (T) ois.readObject();
    } finally {
        if (ois != null) {
            try {
                ois.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return result;
}

From source file:Main.java

public static ArrayList<Node> getNodeList(String xmlStr, String nodeName, Map<String, String> attributesMap) {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    ArrayList<Node> returnNodeList = new ArrayList<Node>();
    try {// w w  w .ja va 2  s .c om
        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) {
            if (node.getNodeName().equals(nodeName)) {
                if (attributesExist(node, attributesMap)) {
                    returnNodeList.add(node);
                }
            }
            node = i.nextNode();
        }
    } catch (Exception ex) {
        logger.error(ex);
    }
    return returnNodeList;
}

From source file:Main.java

public static Document getAbsoluteXML(Node xml) {
    Document doc = null;//www  .  ja  v  a 2s. c om
    String xmlContent = getString(xml);
    try {
        DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        doc = db.parse(new InputSource(new ByteArrayInputStream(xmlContent.getBytes("utf-8"))));
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (SAXException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return doc;
}

From source file:Main.java

/**
 * unzip some gzip compressed data//from ww w . ja  v  a  2s.  com
 * @param bytes the data to uncompress
 * @return the uncompressed data
 */
public static String gzipDecompress(byte[] bytes) {
    ByteArrayInputStream stream = new ByteArrayInputStream(bytes);
    try {
        GZIPInputStream gs = new GZIPInputStream(stream);
        BufferedInputStream bufis = new BufferedInputStream(gs);
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        byte[] buf = new byte[1024];
        int len;
        while ((len = bufis.read(buf)) > 0) {
            bos.write(buf, 0, len);
        }
        return bos.toString();
    } catch (IOException e) {
        return null;
    }
}

From source file:Main.java

public static String getSpecialString(String context, String encoding) {
    try {//  w w w  .  ja  va 2  s .  co  m
        ByteArrayInputStream in = new ByteArrayInputStream(context.getBytes());
        InputStreamReader isr = new InputStreamReader(in, encoding);
        BufferedReader reader = new BufferedReader(isr);
        StringBuffer buffer = new StringBuffer();
        String result;
        while ((result = reader.readLine()) != null) {
            buffer.append(result);
        }
        return buffer.toString();
    } catch (Exception ex) {
        return context;
    }
}

From source file:com.iflytek.edu.cloud.frame.utils.ErrorMsgParser.java

public static String getErrorCode(MockHttpServletResponse response) {
    try {/*from  w w  w  .  j av a 2 s  .  c  o  m*/
        SAXReader saxReader = new SAXReader();
        Document document = saxReader.read(new ByteArrayInputStream(response.getContentAsByteArray()));
        Element rootElement = document.getRootElement();
        return rootElement.element("code").getText();
    } catch (DocumentException e) {
        return null;
    }
}

From source file:Main.java

/**
 * To buffered image.//from  w  ww  .jav a2 s.  c  om
 *
 * @param imageBytes
 *            the image bytes
 * @return the buffered image
 * @throws IOException
 *             Signals that an I/O exception has occurred.
 */
public static BufferedImage toBufferedImage(final byte[] imageBytes) throws IOException {
    return javax.imageio.ImageIO.read(new ByteArrayInputStream(imageBytes));
}

From source file:Main.java

public static final byte[] uncompress(final byte[] bytes) {
    if (bytes == null) {
        throw new NullPointerException("byte[] is NULL !");
    }//from   w w  w . ja  va  2s .  co  m
    ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
    ByteArrayOutputStream bos = new ByteArrayOutputStream(8192);
    byte[] buffer = new byte[bytes.length];
    int length;
    try {
        GZIPInputStream gis = new GZIPInputStream(bais);
        while ((length = gis.read(buffer)) != -1) {
            bos.write(buffer, 0, length);
        }
        byte[] moreBytes = bos.toByteArray();
        bos.close();
        bais.close();
        gis.close();
        bos = null;
        bais = null;
        gis = null;
        return moreBytes;
    } catch (IOException e) {
        return null;
    }
}

From source file:Main.java

public static Object gzipDecompressObject(byte[] compressedData) throws IOException, ClassNotFoundException {

    ByteArrayInputStream bais = new ByteArrayInputStream(compressedData);
    return readGZipCompressedObject(bais);

}