List of usage examples for java.io ByteArrayInputStream ByteArrayInputStream
public ByteArrayInputStream(byte buf[])
From source file:Main.java
/** * Descomprime uma string utilizando o GZIP * /*www . j av a 2 s. co m*/ * @param str * @param encoding * @return */ public static String gzipDecompress(byte[] bytes, String encoding) { String decompressedString = ""; try { ByteArrayInputStream bais = new ByteArrayInputStream(bytes); GZIPInputStream gzip = new GZIPInputStream(bais); Reader reader = new InputStreamReader(gzip, encoding); StringBuffer sbuf = new StringBuffer(); char[] buffer = new char[32 * 1024]; int nread; while ((nread = reader.read(buffer)) >= 0) { sbuf.append(buffer, 0, nread); } decompressedString = sbuf.toString(); reader.close(); } catch (Exception e) { e.printStackTrace(); } return decompressedString; }
From source file:Main.java
public static Node stringToNode(String string) { try {//from w w w. jav a2s . c om DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder(); return db.parse(new ByteArrayInputStream(string.getBytes())).getFirstChild(); } catch (SAXException ex) { throw new RuntimeException(ex); } catch (IOException ex) { throw new RuntimeException(ex); } catch (ParserConfigurationException ex) { throw new RuntimeException(ex); } }
From source file:Main.java
public static byte[] unzip(byte[] output) { if (output == null || output.length == 0) { return output; }//from w ww . ja va 2 s .co m try { ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayInputStream in = new ByteArrayInputStream(output); GZIPInputStream gunzip = new GZIPInputStream(in); byte[] buffer = new byte[BUFFER_SIZE]; int ret; while ((ret = gunzip.read(buffer)) >= 0) { out.write(buffer, 0, ret); } gunzip.close(); return out.toByteArray(); } catch (IOException e) { throw new RuntimeException(e); } }
From source file:Main.java
public static Bitmap byteToBitmap(byte[] imgByte) { InputStream input = null;//from ww w . j av a2 s .com Bitmap bitmap = null; BitmapFactory.Options options = new BitmapFactory.Options(); options.inSampleSize = 1; input = new ByteArrayInputStream(imgByte); SoftReference softRef = new SoftReference(BitmapFactory.decodeStream(input, null, options)); bitmap = (Bitmap) softRef.get(); if (imgByte != null) { imgByte = null; } try { if (input != null) { input.close(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return bitmap; }
From source file:Main.java
/** * Convert bytes array to object./*from w w w . j a v a 2 s.co m*/ * * @param bytes object bytes * @param <T> object class * @return object */ public static <T> T fromBytes(byte[] bytes) { Object result = null; ByteArrayInputStream bis = new ByteArrayInputStream(bytes); ObjectInput in = null; try { in = new ObjectInputStream(bis); result = in.readObject(); } catch (IOException | ClassNotFoundException e) { throw new RuntimeException(e); } finally { if (in != null) { try { in.close(); } catch (Exception e) { } } if (bis != null) { try { bis.close(); } catch (Exception e) { } } } return (T) result; }
From source file:Main.java
/** * Bitmap into compressed PNG as InputStream object * * @param image the Bitmap//from w w w. j a va2s . c om * @return compressed PNG as InputStream object */ public static ByteArrayInputStream bitmapToPngInputStream(final Bitmap image) { return new ByteArrayInputStream(bitmapToPng(image)); }
From source file:Main.java
public static String[] xpathOnString(String q, String stringdoc) { try {//from ww w .j a va 2 s .c o m DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance(); // domFactory.setNamespaceAware(true); DocumentBuilder builder = domFactory.newDocumentBuilder(); Document doc = builder.parse(new ByteArrayInputStream(stringdoc.getBytes())); XPathFactory factory = XPathFactory.newInstance(); XPath xpath = factory.newXPath(); XPathExpression expr = xpath.compile(q); Object result = expr.evaluate(doc, XPathConstants.NODESET); NodeList nodes = (NodeList) result; String res[] = new String[nodes.getLength()]; for (int i = 0; i < nodes.getLength(); i++) { // System.out.println(nodes.item(i).toString()); res[i] = nodes.item(i).getNodeValue(); } return res; } catch (Exception e) { System.out.println("XPathUtils.xpathOnString:caught:" + e); return null; } }
From source file:Main.java
static public byte[] gunzip(byte src[], byte default_value[]) { try {/*from w ww.j a v a2 s .c o m*/ if (src == null) return default_value; ByteArrayOutputStream out = new ByteArrayOutputStream(); GZIPInputStream in = new GZIPInputStream(new ByteArrayInputStream(src)); IOUtils.copy(in, out); in.close(); out.close(); return out.toByteArray(); } catch (Exception e) { return default_value; } }
From source file:Main.java
public static Object parseXmlStringToBeanByJAXB(String xml, Class clase) throws Exception { JAXBContext context = JAXBContext.newInstance(clase); Unmarshaller unmarshaller = context.createUnmarshaller(); InputStream is = null;//from w w w. j a va 2s. c o m try { is = new ByteArrayInputStream(xml.getBytes("UTF-8")); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } if (is == null) { return null; } JAXBElement elm = unmarshaller.unmarshal(new StreamSource(is), clase); return elm.getValue(); }
From source file:Main.java
public static Document getDocument(String xml) { if (xml == null || xml.indexOf("<?xml") < 0) return null; try {/* w w w .j a v a 2s . c o m*/ DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); Document doc = db.parse(new ByteArrayInputStream(xml.getBytes())); return doc; } catch (Exception e) { e.printStackTrace(); return null; } }