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 List<Certificate> loadCertificates(String[] pemEncodedCerts) throws Exception {

    List<Certificate> certList = new ArrayList<>();
    CertificateFactory certFactory = CertificateFactory.getInstance("X.509");

    for (String certPem : pemEncodedCerts) {
        InputStream certIn = new ByteArrayInputStream(certPem.getBytes());
        Certificate cert = certFactory.generateCertificate(certIn);
        certList.add(cert);/*from   www.  j  a  v  a2 s  .  c o m*/
    }

    return certList;
}

From source file:Main.java

public static String getSingleValueFromString(String xmlStr, String xpathStr)
        throws XPathExpressionException, SAXException, IOException {
    InputStream is = new ByteArrayInputStream(xmlStr.getBytes("utf-8"));
    return getSingleValue(is, xpathStr);
}

From source file:Main.java

/**
 * Compresses a GZIP file.//ww  w  . j  a  va  2s. co m
 * 
 * @param bytes
 *            The uncompressed bytes.
 * @return The compressed bytes.
 * @throws IOException
 *             if an I/O error occurs.
 */
public static byte[] gzip(byte[] bytes) throws IOException {
    /* create the streams */
    InputStream is = new ByteArrayInputStream(bytes);
    try {
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        OutputStream os = new GZIPOutputStream(bout);
        try {
            /* copy data between the streams */
            byte[] buf = new byte[4096];
            int len = 0;
            while ((len = is.read(buf, 0, buf.length)) != -1) {
                os.write(buf, 0, len);
            }
        } finally {
            os.close();
        }

        /* return the compressed bytes */
        return bout.toByteArray();
    } finally {
        is.close();
    }
}

From source file:Main.java

/**
 * Liefert das {@link Document} aus dem Byte-Array.
 * /*w  w  w.  j  a v a  2s . co m*/
 * @param bytes byte[]
 * @return {@link Document}
 * @throws Exception Falls was schief geht.
 */
public static Document getDocument(final byte[] bytes) throws Exception {
    return getDocument(new InputSource(new ByteArrayInputStream(bytes)));
}

From source file:Main.java

/**
 * Descomprime uma string utilizando o GZIP
 * //from  w w  w .j  ava 2 s.c  o  m
 * @param str
 * @param encoding
 * @return
 */
public static String gzipDecompressString(String str, String encoding) {
    String decompressedString = "";

    try {
        byte[] bytes = Base64.decodeBase64(str.getBytes(encoding));
        ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
        GZIPInputStream gzip = new GZIPInputStream(bais);
        Reader reader = new InputStreamReader(gzip, encoding);
        StringBuffer sbuf = new StringBuffer();
        char[] buffer = new char[32 * 1024];
        int nread;
        while ((nread = reader.read(buffer)) >= 0) {
            sbuf.append(buffer, 0, nread);
        }
        decompressedString = sbuf.toString();
        reader.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return decompressedString;
}

From source file:Main.java

public static Object toObject(byte[] bytes) {
    Object obj = null;//w w  w .  ja v  a 2 s.c  om
    try {
        ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
        ObjectInputStream ois = new ObjectInputStream(bis);
        obj = ois.readObject();
        ois.close();
        bis.close();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return obj;
}

From source file:Main.java

public static Document fromXML(String xml) throws ParserConfigurationException, IOException, SAXException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);//w  ww.  j a  v a2  s . c  om
    factory.setIgnoringElementContentWhitespace(true);
    DocumentBuilder builder = factory.newDocumentBuilder();
    return builder.parse(new ByteArrayInputStream(xml.getBytes("UTF-8")));
}

From source file:Main.java

public static Document createDocument(byte[] data) throws RuntimeException {

    DocumentBuilderFactory f = DocumentBuilderFactory.newInstance();
    InputStream input = null;/*from  w  ww .  j a  v a2 s  .c  o  m*/
    try {
        DocumentBuilder p = f.newDocumentBuilder();
        input = new ByteArrayInputStream(data);
        return p.parse(input);
    } catch (ParserConfigurationException e) {
        throw new RuntimeException(e);
    } catch (SAXException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static Document getXMLDoc(String response)
        throws ParserConfigurationException, SAXException, IOException {
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    InputStream is = new ByteArrayInputStream(response.getBytes());
    Document doc = dBuilder.parse(is);
    return doc;//from w  w w  .jav  a2 s.c o  m
}

From source file:Main.java

public static Document parseDocumentByString(String result) {
    try {//from w  w  w  .ja  v  a 2  s  . c  om
        factory.setValidating(false);
        DocumentBuilder builder = factory.newDocumentBuilder();
        return builder.parse(new ByteArrayInputStream(result.getBytes("UTF-8")));
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return null;
}