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:mx.com.pendulum.carga.util.Md5Converter.java

public static String getMD5Checksum(MultipartFile multipartFile) {
    InputStream is = null;/* w w  w . j  a v  a 2 s  . co  m*/
    try {
        is = new ByteArrayInputStream(multipartFile.getBytes());
        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 String xPathSearch(String input, String expression) throws Exception {
    DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    XPath xPath = XPathFactory.newInstance().newXPath();
    XPathExpression xPathExpression = xPath.compile(expression);
    Document document = documentBuilder.parse(new ByteArrayInputStream(input.getBytes("UTF-8")));
    String output = (String) xPathExpression.evaluate(document, XPathConstants.STRING);
    return output == null ? "" : output.trim();
}

From source file:Main.java

public static void save(Document document, IFile file, IProgressMonitor monitor)
        throws TransformerException, CoreException, UnsupportedEncodingException {
    TransformerFactory transfac = TransformerFactory.newInstance();
    Transformer trans = transfac.newTransformer();
    trans.setOutputProperty(OutputKeys.ENCODING, "utf-8");
    trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
    trans.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
    trans.setOutputProperty(OutputKeys.INDENT, "yes");

    StringWriter sw = new StringWriter();
    StreamResult sr = new StreamResult(sw);
    DOMSource ds = new DOMSource(document);
    trans.transform(ds, sr);/*from  w  ww  . ja v  a 2s  .c  o  m*/
    String xmlString = sw.toString();

    InputStream is = new ByteArrayInputStream(xmlString.getBytes("UTF-8"));

    if (file.exists()) {
        file.setContents(is, 0, monitor);
    } else {
        file.create(is, true, monitor);
    }
}

From source file:com.evolveum.midpoint.util.SerializationUtil.java

public static <T> T fromString(String string) throws IOException, ClassNotFoundException {
    byte[] data = Base64.decodeBase64(string);
    ObjectInputStream objectInputStream = new ObjectInputStream(new ByteArrayInputStream(data));
    Object object = objectInputStream.readObject();
    objectInputStream.close();// w ww  . j  a  v  a 2 s . c om
    return (T) object;
}

From source file:Main.java

public static Document getDocumentForString(String str)
        throws IOException, SAXException, ParserConfigurationException {
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    return dbFactory.newDocumentBuilder().parse(new ByteArrayInputStream(str.getBytes()));
}

From source file:Main.java

public static InputStream toInputStream(CharSequence input, String encoding)
        throws UnsupportedEncodingException {
    byte[] bytes = input.toString().getBytes(encoding);
    return new ByteArrayInputStream(bytes);
}

From source file:Main.java

public static Document parseXmlText(String xml) {
    ByteArrayInputStream bais = new ByteArrayInputStream(xml.getBytes());
    return parseXmlInputSource(new InputSource(bais));
}

From source file:Main.java

/**
 * Applies an XSL transformation to <code>xmlString</code> using
 * <code>xsltStr</code> as the stylesheet and returns the transformed
 * string./*from ww  w.ja v a2s . co m*/
 * 
 * @param xmlString
 *            The XML to transform.
 * @param xsltStr
 *            The stylesheet as an XML string (not a file).
 * @return Transformed string.
 * @throws Exception
 */
public static String applyStylesheetAsString(String xmlString, String xsltStr) throws Exception {
    // Use the static TransformerFactory.newInstance() method to instantiate
    // a TransformerFactory. The javax.xml.transform.TransformerFactory
    // system property setting determines the actual class to instantiate --
    // org.apache.xalan.transformer.TransformerImpl.

    TransformerFactory tFactory = TransformerFactory.newInstance();

    // Use the TransformerFactory to instantiate a Transformer that will work with
    // the stylesheet you specify. This method call also processes the
    // stylesheet into a compiled Templates object.

    Transformer transformer = tFactory
            .newTransformer(new StreamSource(new ByteArrayInputStream(xsltStr.getBytes())));

    // Use the Transformer to apply the associated Templates object to an
    // XML document (foo.xml) and write the output to a file (foo.out).

    StringWriter swriter = new StringWriter();
    StreamResult sresult = new StreamResult(swriter);
    transformer.transform(new StreamSource(new ByteArrayInputStream(xmlString.getBytes("UTF-8"))), sresult);
    return swriter.toString();
}

From source file:Main.java

static boolean isJPEG(byte[] data) throws IOException {
    return internalIsJPEG(new DataInputStream(new ByteArrayInputStream(data)));
}

From source file:org.energyos.espi.common.test.FixtureFactory.java

public static InputStream newFeedInputStream(UUID uuid) throws IOException {
    return new ByteArrayInputStream(newFeedXML(uuid).getBytes());
}