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 createDocumentFromString(String xmlString)
        throws ParserConfigurationException, SAXException, IOException {
    DocumentBuilder parser = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    // Create the Input Stream
    ByteArrayInputStream byteIs = new ByteArrayInputStream(xmlString.getBytes());
    // parse the XML purely as XML and get a DOM tree representation.         
    Document doc = parser.parse(byteIs);
    return doc;//from   ww w .ja va2 s. c om
}

From source file:Main.java

/**
 * Check if the file provide is PKCS12/*from   w  ww  .  j a v  a2  s .  c o  m*/
 * @param cert certificate to be validated
 * @param pass password to be provided
 * @throws Exception to indicate an invalid certificate
 */
public static void validate(byte[] cert, String pass) throws Exception {

    try {
        KeyStore keyStore = KeyStore.getInstance(ALGORITHM);
        keyStore.load(new ByteArrayInputStream(cert), pass.toCharArray());
    } catch (Exception e) {
        throw new Exception("Certificate is not valid!", e);
    }
}

From source file:Main.java

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

From source file:Main.java

/**
 * Creates from the given xml string an java object.
 * /*from  w ww  .  j  a  va  2s  .c om*/
 * @param <T>
 *            the generic type
 * @param xmlString
 *            the xml string to transform to an java object.
 * @return the xml string
 */
@SuppressWarnings("unchecked")
public static <T> T toObjectWithXMLDecoder(final String xmlString) {

    XMLDecoder dec = null;
    T obj = null;
    try {
        InputStream is = new ByteArrayInputStream(xmlString.getBytes());
        dec = new XMLDecoder(is);

        obj = (T) dec.readObject();

    } finally {
        if (dec != null) {
            dec.close();
        }
    }
    return obj;
}

From source file:com.mirth.connect.connectors.http.HttpUtil.java

public static String uncompressGzip(byte[] content, String charset) throws IOException {
    GZIPInputStream gzis = new GZIPInputStream(new ByteArrayInputStream(content));
    StringWriter writer = new StringWriter();
    IOUtils.copy(gzis, writer, charset);
    return writer.toString();
}

From source file:Main.java

public static String formatXMLStr(String xml) {
    String output = null;/* w  w w.j  a  va 2 s.  co m*/
    try {
        Node document = DocumentBuilderFactory.newInstance().newDocumentBuilder()
                .parse(new ByteArrayInputStream(xml.getBytes())).getDocumentElement();

        TransformerFactory tFactory = TransformerFactory.newInstance();
        Transformer transformer = tFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
        StringWriter writer = new StringWriter();
        transformer.transform(new DOMSource(document), new StreamResult(writer));

        // output = writer.getBuffer().toString().replaceAll("\n|\r", "");
        output = writer.getBuffer().toString();
    } catch (TransformerConfigurationException e) {
        e.printStackTrace();
    } catch (TransformerException e) {
        e.printStackTrace();
    } catch (SAXException e) {
        e.printStackTrace();
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return output;
}

From source file:Main.java

/**
 * Parses a {@link Document} from an {@link InputStream} containing one.
 * This is quite usual start {@link PersistenceHandler #load(InputStream)
 * load()}-method of an {@link PersistenceHandler}-implementor.
 * //from w ww .j a v a  2  s.c om
 * @param inStream
 *            {@link InputStream} from which to parse
 * @return {@link Document} parsed from the input-stream
 * @throws ParserConfigurationException
 * @throws SAXException
 * @throws IOException
 */
public static Document parseFromBytes(byte[] dataPres)
        throws ParserConfigurationException, SAXException, IOException {
    DocumentBuilder dBuilder = null;
    Document doc = null;
    dBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();

    // no external resources are tied with ByteArray-streams;
    // no need to worry about closing
    doc = dBuilder.parse(new ByteArrayInputStream(dataPres));

    doc.getDocumentElement().normalize();
    return doc;
}

From source file:Main.java

/**
 * Deserialize provided string into Object
 * @param serialized String//from ww  w  .  j a  v a  2 s  .  c  om
 * @return Object
 * @throws IOException If unable to access Stream
 * @throws ClassNotFoundException If unable to find class
 */
public static Object deserialize(String serialized) throws IOException, ClassNotFoundException {
    if (serialized == null)
        return null;
    return new ObjectInputStream(new Base64InputStream(new ByteArrayInputStream(serialized.getBytes()), 0))
            .readObject();
}

From source file:com.mirth.connect.model.converters.DICOMConverter.java

public static DicomObject byteArrayToDicomObject(byte[] bytes, boolean decodeBase64) throws IOException {
    DicomObject basicDicomObject = new BasicDicomObject();
    DicomInputStream dis = null;/*  w  w  w .  j  a  v  a 2  s  .c om*/

    try {
        ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
        InputStream inputStream;
        if (decodeBase64) {
            inputStream = new BufferedInputStream(new Base64InputStream(bais));
        } else {
            inputStream = bais;
        }
        dis = new DicomInputStream(inputStream);
        /*
         * This parameter was added in dcm4che 2.0.28. We use it to retain the memory allocation
         * behavior from 2.0.25.
         * http://www.mirthcorp.com/community/issues/browse/MIRTH-2166
         * http://www.dcm4che.org/jira/browse/DCM-554
         */
        dis.setAllocateLimit(-1);
        dis.readDicomObject(basicDicomObject, -1);
    } catch (IOException e) {
        throw e;
    } finally {
        IOUtils.closeQuietly(dis);
    }

    return basicDicomObject;
}

From source file:Main.java

public static Document getDocument(String xmlStr) {
    docFactory = getInstance();/*w  w w  . j av a2 s .c  om*/
    Document document = null;
    try {
        DocumentBuilder builder = docFactory.newDocumentBuilder();
        document = builder.parse(new ByteArrayInputStream(xmlStr.getBytes()));
        document.getDocumentElement().normalize();
    } catch (SAXException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    }
    return document;
}