List of usage examples for java.io ByteArrayInputStream ByteArrayInputStream
public ByteArrayInputStream(byte buf[])
From source file:Main.java
public static Document string2Document(String xml, String encode) throws ParserConfigurationException, UnsupportedEncodingException, SAXException, IOException { Document document = null;//from w w w . j a va 2 s.c o m DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = builderFactory.newDocumentBuilder(); try { document = builder.parse(new ByteArrayInputStream(xml.getBytes(encode))); } catch (Exception e) { } return document; }
From source file:Main.java
public static String formatXML(String xml) { try {/* w w w .j ava 2 s.c om*/ Transformer serializer = SAXTransformerFactory.newInstance().newTransformer(); serializer.setOutputProperty(OutputKeys.INDENT, "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()); } catch (Exception e) { return xml; } }
From source file:Main.java
private static InputStream openPhoto(final Context context, final Uri contactUri) { Uri photoUri = Uri.withAppendedPath(contactUri, ContactsContract.Contacts.Photo.CONTENT_DIRECTORY); Cursor cursor = context.getContentResolver().query(photoUri, new String[] { ContactsContract.Contacts.Photo.PHOTO }, null, null, null); if (cursor == null) { return null; }/*from w w w . j a v a 2 s .c o m*/ try { if (cursor.moveToFirst()) { byte[] data = cursor.getBlob(0); if (data != null) { return new ByteArrayInputStream(data); } } } finally { cursor.close(); } return null; }
From source file:Main.java
public static Document stringToDocument(final String string) throws ParserConfigurationException, UnsupportedEncodingException, SAXException, IOException { final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true);/*from w ww .ja va2 s. c o m*/ final DocumentBuilder builder = factory.newDocumentBuilder(); return builder.parse(new InputSource(new ByteArrayInputStream(string.getBytes(ENCODING)))); }
From source file:Main.java
public static String getAttribute(String xmlStr, String tagName, String attrName) { DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance(); domFactory.setNamespaceAware(false); // never forget this! Document doc = null;/*www . j a v a 2 s . c om*/ DocumentBuilder builder = null; String value = null; try { builder = domFactory.newDocumentBuilder(); doc = builder.parse(new ByteArrayInputStream(xmlStr.getBytes())); // Get the root element Node rootNode = doc.getFirstChild(); NodeList nodeList = doc.getElementsByTagName(tagName); if ((nodeList.getLength() == 0) || (nodeList.item(0).getAttributes().getNamedItem(attrName) == null)) { logger.error("Either node " + tagName + " or attribute " + attrName + " not found."); } else { value = nodeList.item(0).getAttributes().getNamedItem(attrName).getNodeValue(); logger.debug("value of " + tagName + " attribute: " + attrName + " = " + value); } } catch (Exception ex) { System.out.println(ex.getMessage()); } return value; }
From source file:Main.java
public static InputStream toInputStream(String str) { return new ByteArrayInputStream(str.getBytes()); }
From source file:Main.java
public static boolean xmlStringValidate(String xmlStr, String xsdPath) { boolean flag = false; try {// w w w . ja v a 2 s. c o m SchemaFactory factory = SchemaFactory.newInstance(SCHEMALANG); File schemaLocation = new File(xsdPath); Schema schema = factory.newSchema(schemaLocation); Validator validator = schema.newValidator(); InputStream is = new ByteArrayInputStream(xmlStr.getBytes()); Source source = new StreamSource(is); try { validator.validate(source); flag = true; } catch (SAXException ex) { System.out.println(ex.getMessage()); } } catch (Exception e) { e.printStackTrace(); } return flag; }
From source file:Main.java
static public byte[] gzip(byte src[], byte default_value[]) { try {// w w w . j av a2s .c om if (src == null) return default_value; ByteArrayOutputStream out_raw = new ByteArrayOutputStream(); GZIPOutputStream out = new GZIPOutputStream(out_raw); ByteArrayInputStream in = new ByteArrayInputStream(src); IOUtils.copy(in, out); in.close(); out.close(); return out_raw.toByteArray(); } catch (Exception e) { return default_value; } }
From source file:Main.java
public static Document stringToDocument(final String source) { String tmp = source.trim();//w ww .j a v a2 s .co m if (!tmp.startsWith("<?xml")) { tmp = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + tmp; } String encode = "utf-8"; Pattern p = Pattern.compile("<?.*encoding=\"([^ ]*)\".*?>"); Matcher m = p.matcher(tmp); if (m.find()) { encode = m.group(1); } try { return DocumentBuilderFactory.newInstance().newDocumentBuilder() .parse(new ByteArrayInputStream(tmp.getBytes(encode))); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (SAXException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (ParserConfigurationException e) { e.printStackTrace(); } return null; }
From source file:Main.java
/** * Print an XML string in "pretty" format to an output stream. * //from ww w .ja v a 2s .c o m * @param xmlString * @param out * @param indent the number of characters to indent */ public static void prettyPrintXmlString(String xmlString, OutputStream out, int indent) { prettyPrintXml(new ByteArrayInputStream(xmlString.getBytes()), out, indent); }