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 <T> T unMarshal(String xml, Class<T> clazz, String encoding) {
    T result = null;//w ww.ja  v  a  2s. c o m
    try {
        ByteArrayInputStream is = new ByteArrayInputStream(xml.getBytes());
        JAXBContext context = JAXBContext.newInstance(clazz);
        result = (T) context.createUnmarshaller()
                .unmarshal(new InputSource(new InputStreamReader(is, encoding)));
    } catch (JAXBException e) {
        e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    return result;

}

From source file:Main.java

public static Document domObject(StringBuffer data)
        throws SAXException, IOException, ParserConfigurationException {
    Document xmlDoc = null;//from   w  ww.ja  va2s .com
    xmlDoc = DocumentBuilderFactory.newInstance().newDocumentBuilder()
            .parse(new ByteArrayInputStream(data.toString().getBytes()));
    return xmlDoc;
}

From source file:Main.java

public static byte[] decompress(byte[] bytes) throws IOException {

    GZIPInputStream gzip = null;/*  ww w  . j  a  v a  2s  .  c  om*/
    ByteArrayOutputStream baos = null;
    try {
        baos = new ByteArrayOutputStream();

        gzip = new GZIPInputStream(new ByteArrayInputStream(bytes));

        int len = 0;
        byte data[] = new byte[BUFFER_SIZE];

        while ((len = gzip.read(data, 0, BUFFER_SIZE)) != -1) {
            baos.write(data, 0, len);
        }

        gzip.close();
        baos.flush();

        return baos.toByteArray();

    } finally {
        if (gzip != null) {
            gzip.close();
        }
        if (baos != null) {
            baos.close();
        }
    }
}

From source file:Main.java

public static Element GetXmlDocFromStr(String xmlStr) {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = null;/*from  w  w  w.  j av  a  2s.co  m*/
    try {
        db = dbf.newDocumentBuilder();
    } catch (ParserConfigurationException pce) {
        pce.printStackTrace();
    }
    Document doc = null;
    try {
        InputStream inputStream = new ByteArrayInputStream(xmlStr.getBytes());
        doc = db.parse(inputStream);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return (Element) doc.getDocumentElement();
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static <T> T unmarshall(String xml, Class<T> cl) throws JAXBException, UnsupportedEncodingException {

    JAXBContext jaxbCtx = JAXBContext.newInstance(cl);
    Unmarshaller unmarshaller = jaxbCtx.createUnmarshaller();
    return (T) unmarshaller.unmarshal(new ByteArrayInputStream(xml.getBytes("UTF-8")));

}

From source file:Main.java

public static Object xml2Bean(Class<?> zClass, String xml) {
    Object obj = null;/*from  w w w  .  ja  va2s  . co  m*/
    JAXBContext context = null;
    if (null == xml || "".equals(xml) || "null".equalsIgnoreCase(xml) || xml.length() < 1)
        return obj;
    try {
        context = JAXBContext.newInstance(zClass);
        InputStream iStream = new ByteArrayInputStream(xml.getBytes());
        Unmarshaller um = context.createUnmarshaller();
        obj = (Object) um.unmarshal(iStream);
        return obj;
    } catch (JAXBException e) {
        e.printStackTrace();
    }
    return obj;
}

From source file:Main.java

/**
 * Decompresses a array of bytes back into a string.
 *
 * @param bytes The compressed list of bytes.
 *
 * @return The string uncompress./*from ww  w .j a v  a2  s.co  m*/
 *
 * @throws Exception If failed to uncompress.
 */
public static String decompress(byte[] bytes) throws Exception {
    if (bytes == null || bytes.length == 0) {
        return null;
    }

    GZIPInputStream gis = new GZIPInputStream(new ByteArrayInputStream(bytes));
    BufferedReader bf = new BufferedReader(new InputStreamReader(gis, "UTF-8"));
    StringBuilder result = new StringBuilder();
    String line;

    while ((line = bf.readLine()) != null) {
        result.append(line);
    }

    return result.toString();
}

From source file:Main.java

public static Document createDocument(String xmlString)
        throws SAXException, IOException, ParserConfigurationException, UnsupportedEncodingException {
    Document xmlDocument = DocumentBuilderFactory.newInstance().newDocumentBuilder()
            .parse(new ByteArrayInputStream(xmlString.getBytes("UTF8")));
    return xmlDocument;
}

From source file:mx.com.pendulum.carga.util.Md5Converter.java

public static String calculaMD5(byte[] data) {
    InputStream is = new ByteArrayInputStream(data);
    try {/*from ww  w .ja v a 2s . c om*/
        byte[] buffer = new byte[1024];
        MessageDigest digest = MessageDigest.getInstance("MD5");
        int numRead = 0;
        while (numRead != -1) {
            numRead = is.read(buffer);
            if (numRead > 0) {
                digest.update(buffer, 0, numRead);
            }
        }
        byte[] md5Bytes = digest.digest();
        return convertHashToString(md5Bytes);
    } catch (Exception e) {
        return null;
    } finally {
        if (is != null) {
            try {
                is.close();
            } catch (Exception e) {
            }
        }
    }

}

From source file:Main.java

public static Document loadXML(String xml) {
    if (null == xml) {
        return null;
    }/*ww  w  . j  a v  a2  s  .  com*/
    Document dom = null;
    InputStream stream = null;
    try {
        stream = new ByteArrayInputStream(xml.getBytes("utf-8"));
        return load(stream);
    } catch (Exception e) {
    } finally {
        try {
            stream.close();
        } catch (Exception e) {

        }
    }
    return dom;
}