List of usage examples for java.io ByteArrayInputStream ByteArrayInputStream
public ByteArrayInputStream(byte buf[])
From source file:Main.java
/** * Writes the given byte array as a local file. * * @param array the byte array to read from * @param outputFileName the name of the file to write to * @return the URL for the local file//from w ww .j a v a 2s.c om */ public static String writeFile(byte[] array, String outputFileName) throws IOException { InputStream in = new ByteArrayInputStream(array); try { return writeStreamToFile(in, outputFileName); } finally { in.close(); } }
From source file:Main.java
public static Document fromXML(String xml) throws ParserConfigurationException, IOException, SAXException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true);// w w w . jav a2 s . co m factory.setIgnoringElementContentWhitespace(true); DocumentBuilder builder = factory.newDocumentBuilder(); return builder.parse(new ByteArrayInputStream(xml.getBytes("UTF-8"))); }
From source file:Main.java
public static void compressWithQuality(Bitmap image, String outPath, int maxSize) throws IOException { ByteArrayOutputStream os = new ByteArrayOutputStream(); image.compress(Bitmap.CompressFormat.JPEG, 20, os); while (os.toByteArray().length / 1024 > 1024) { os.reset();/*from w ww .j a va 2 s.c o m*/ image.compress(Bitmap.CompressFormat.JPEG, 20, os); } ByteArrayInputStream bi = new ByteArrayInputStream(os.toByteArray()); BitmapFactory.decodeStream(bi); FileOutputStream fi = new FileOutputStream(outPath); fi.write(os.toByteArray()); fi.flush(); fi.close(); }
From source file:Main.java
public static Node bytesToXml(final byte[] body) throws TransformerException { final Transformer transformer = FACTORY.newTransformer(); final Source source = new StreamSource(new ByteArrayInputStream(body)); final DOMResult result = new DOMResult(); transformer.transform(source, result); return result.getNode(); }
From source file:Main.java
/** * Returns the name of the root element of an XML message. * /*from w ww . j ava 2s . c o m*/ * @param xml XML message * @return the name of the root element * @exception Exception if an error occurs */ public static String getRootElementName(byte[] xml) throws Exception { ByteArrayInputStream is = new ByteArrayInputStream(xml); XMLEventReader reader = inputFactory.createXMLEventReader(is); while (reader.hasNext()) { XMLEvent event = (XMLEvent) reader.next(); if (event.isStartElement()) { StartElement startElement = event.asStartElement(); return startElement.getName().getLocalPart(); } } reader.close(); return ""; }
From source file:Main.java
public static Document loadXMLFrom(String xml) throws Exception { return loadXMLFrom(new ByteArrayInputStream(xml.getBytes())); }
From source file:Main.java
public static void byteArrayToFile(byte[] bytes, String filePath) throws Exception { InputStream in = new ByteArrayInputStream(bytes); File destFile = new File(filePath); if (!destFile.getParentFile().exists()) { destFile.getParentFile().mkdirs(); }//from w ww .j ava 2 s. co m destFile.createNewFile(); OutputStream out = new FileOutputStream(destFile); byte[] cache = new byte[CACHE_SIZE]; int nRead = 0; while ((nRead = in.read(cache)) != -1) { out.write(cache, 0, nRead); out.flush(); } out.close(); in.close(); }
From source file:Main.java
public static InputStream bitmap2InputStream(Bitmap bm, int quality) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); bm.compress(Bitmap.CompressFormat.PNG, quality, baos); InputStream is = new ByteArrayInputStream(baos.toByteArray()); return is;/*from w w w . ja v a 2 s. c om*/ }
From source file:Main.java
/** * Parse the supplied string into an xml document. * //from w w w . j ava 2 s. com * @param content * @return */ public static Document parseString(String content) { try { DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); documentBuilderFactory.setNamespaceAware(true); DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); //parse the stream into a document return documentBuilder.parse(new ByteArrayInputStream(content.getBytes())); } catch (Exception ex) { ex.printStackTrace(); } return null; }
From source file:Main.java
/** * Parses a string into an XMLDocument//from w ww.ja v a2 s. c o m * * @param xmlString * @return * @throws IOException * @throws ParserConfigurationException * @throws SAXException */ public static Document loadXML(String xmlString) throws IOException, ParserConfigurationException, SAXException { InputStream inputStream = new ByteArrayInputStream(xmlString.getBytes()); DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); Document doc = dBuilder.parse(inputStream); // optional, but recommended // read this - // http://stackoverflow.com/questions/13786607/normalization-in-dom-parsing-with-java-how-does-it-work doc.getDocumentElement().normalize(); return doc; }