List of usage examples for java.io ByteArrayInputStream ByteArrayInputStream
public ByteArrayInputStream(byte buf[])
From source file:Main.java
private static String bytesToString(byte[] bytes) { if (bytes == null) { return null; }//from w w w .ja va 2s.c om ByteArrayInputStream bais = new ByteArrayInputStream(bytes); DataInputStream dis = new DataInputStream(bais); String res = ""; try { res = dis.readUTF(); } catch (IOException ex) { } finally { try { dis.close(); bais.close(); } catch (IOException ex1) { } dis = null; bais = null; } return res; }
From source file:Main.java
/** * Build an XML Document from the data contained in the given String * @param data A String containing an XML document * @return A Document object equivalent to the XML document in the given String *///from www .j a v a2 s. com public static Document xml(String data) { return xml(new ByteArrayInputStream(data.getBytes())); }
From source file:Main.java
public static X509Certificate fromString(String certificate) throws CertificateException { CertificateFactory cf = CertificateFactory.getInstance("X.509"); return (X509Certificate) cf.generateCertificate(new ByteArrayInputStream(certificate.getBytes())); }
From source file:Main.java
/** * * @param xmlContent// ww w. j av a 2 s . com * @param charset * @return * @throws SAXException * @throws IOException * @throws ParserConfigurationException */ private static Document parse(String xmlContent, Charset charset) throws SAXException, IOException, ParserConfigurationException { DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); documentBuilderFactory.setNamespaceAware(false); documentBuilderFactory.setValidating(false); DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); Document document = documentBuilder.parse(new ByteArrayInputStream(xmlContent.getBytes(charset))); return document; }
From source file:Main.java
public static void parseDocumentByString(String result, DefaultHandler defaultHandler) { try {/*from w w w.j a v a2 s . c o m*/ SAXParserFactory sf = SAXParserFactory.newInstance(); SAXParser sp = sf.newSAXParser(); sp.parse(new ByteArrayInputStream(result.getBytes("UTF-8")), defaultHandler); } catch (Exception ex) { ex.printStackTrace(); } }
From source file:Main.java
public static String getProcessIdFromBpmn(final String bpmn) { String processId = null;//from w ww. j a va 2s . c om try { final DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance(); domFactory.setNamespaceAware(true); final Document doc = domFactory.newDocumentBuilder() .parse(new ByteArrayInputStream(bpmn.getBytes("UTF-8"))); final XPath xpath = XPathFactory.newInstance().newXPath(); xpath.setNamespaceContext(new NamespaceContext() { @Override public Iterator<?> getPrefixes(final String namespaceURI) { // Not used in this context. return null; } @Override public String getPrefix(final String namespaceURI) { // Not used in this context. return null; } @Override public String getNamespaceURI(final String prefix) { // Only require the URI for the bpmn2 NS. return BPMN2_NAMESPACE_URI; } }); final XPathExpression expr = xpath.compile(BPMN_PROCESS_ID_XPATH_EXPR); final Node node = (Node) expr.evaluate(doc, XPathConstants.NODE); processId = node.getAttributes().getNamedItem(BPMN_PROCESS_ID_ATTR).getNodeValue(); } catch (final Exception e) { e.printStackTrace(); } return processId; }
From source file:Main.java
public static String prettyFormat(String xml) { if (xml == null || xml.isEmpty() || !xml.contains("<")) { // System.out.println("Why?"+xml.startsWith("<", 0)); return xml; }//from w ww . j av a 2 s . co m try { Transformer serializer = SAXTransformerFactory.newInstance().newTransformer(); serializer.setOutputProperty(OutputKeys.INDENT, "yes"); //serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); Source xmlSource = new SAXSource(new InputSource(new ByteArrayInputStream(xml.getBytes()))); StreamResult res = new StreamResult(new ByteArrayOutputStream()); serializer.transform(xmlSource, res); return new String(((ByteArrayOutputStream) res.getOutputStream()).toByteArray()).replace("><", ">\n<"); } catch (Exception e) { System.out.println("prettyFormat: Error.." + e.getMessage()); //TODO log error return xml.replace("<", "\n<"); // return xml.replace("><", ">\n<"); } }
From source file:Main.java
static byte[] decompress(byte[] compressed) throws IOException { int nRead;/*from www . j a v a 2 s. c om*/ byte[] data = new byte[2048]; ByteArrayInputStream bis = new ByteArrayInputStream(compressed); GZIPInputStream gzip = new GZIPInputStream(bis); ByteArrayOutputStream buffer = new ByteArrayOutputStream(); while ((nRead = gzip.read(data, 0, data.length)) != -1) { buffer.write(data, 0, nRead); } bis.close(); gzip.close(); return buffer.toByteArray(); }
From source file:Main.java
public static ArrayList<Node> getNodesXPath(String srcXmlString, String xPath) { DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance(); domFactory.setNamespaceAware(false); // never forget this! Document doc = null;/* w w w .ja v a 2s .co m*/ DocumentBuilder builder = null; ArrayList<Node> nodesList = new ArrayList<Node>(); try { builder = domFactory.newDocumentBuilder(); doc = builder.parse(new ByteArrayInputStream(srcXmlString.getBytes())); XPathFactory factory = XPathFactory.newInstance(); XPath xpath = factory.newXPath(); XPathExpression expr = xpath.compile(xPath); Object result = expr.evaluate(doc, XPathConstants.NODESET); NodeList xPathNodes = (NodeList) result; logger.debug("xpath result count: " + xPathNodes.getLength()); // iterate through all the nodes for (int i = 0; i < xPathNodes.getLength(); i++) { nodesList.add(xPathNodes.item(i)); } } catch (Exception ex) { logger.error(ex.getMessage()); } return nodesList; }
From source file:Main.java
static byte[] decompress(byte[] compressed) throws IOException { int nRead;/*from www . jav a 2 s . c o m*/ byte[] data = new byte[2048]; ByteArrayInputStream bis = new ByteArrayInputStream(compressed); GZIPInputStream gzip = new GZIPInputStream(bis); ByteArrayOutputStream buffer = new ByteArrayOutputStream(); while ((nRead = gzip.read(data, 0, data.length)) != -1) { buffer.write(data, 0, nRead); } gzip.close(); bis.close(); return buffer.toByteArray(); }