List of usage examples for java.io ByteArrayOutputStream close
public void close() throws IOException
From source file:main.java.refinement_class.Useful.java
static public byte[] readFileAsBytes(Class c, String fileName) throws IOException { InputStream inStream = new java.io.BufferedInputStream(c.getClassLoader().getResourceAsStream(fileName)); ByteArrayOutputStream out = new ByteArrayOutputStream(); int nbytes = 0; byte[] buffer = new byte[100000]; try {/*w w w .j ava2 s . co m*/ while ((nbytes = inStream.read(buffer)) != -1) { out.write(buffer, 0, nbytes); } return out.toByteArray(); } finally { if (inStream != null) { inStream.close(); } if (out != null) { out.close(); } } }
From source file:com.spstudio.common.image.ImageUtils.java
/** * ,?byte//www. j a va2s. c o m * * @return */ public static byte[] loadFile() { File file = new File("d:/touxiang.jpg"); FileInputStream fis = null; ByteArrayOutputStream baos = null; byte[] data = null; try { fis = new FileInputStream(file); baos = new ByteArrayOutputStream((int) file.length()); byte[] buffer = new byte[1024]; int len = -1; while ((len = fis.read(buffer)) != -1) { baos.write(buffer, 0, len); } data = baos.toByteArray(); } catch (IOException e) { e.printStackTrace(); } finally { try { if (fis != null) { fis.close(); fis = null; } baos.close(); } catch (IOException e) { e.printStackTrace(); } } return data; }
From source file:com.clustercontrol.plugin.impl.AsyncTask.java
/** * SerializableXML???//from w ww. j av a 2s . c o m * @param obj Serializable * @return ??XML * @throws IOException */ public static String encodeXML(Serializable obj) throws IOException { ByteArrayOutputStream baos = null; XMLEncoder enc = null; String xml = null; try { baos = new ByteArrayOutputStream(); enc = new XMLEncoder(baos); enc.writeObject(obj); xml = baos.toString("UTF-8"); } catch (UnsupportedEncodingException e) { log.warn(e); } finally { if (enc != null) { enc.close(); } if (baos != null) { baos.close(); } } return xml; }
From source file:gov.niem.ws.util.SecurityUtil.java
/** * Decode data by Base64 decoding, then decompressing with the DEFLATE * algorithm; reverses encodeHeader./*from w w w.j a v a2 s . c om*/ * * @param encoded * @return the decoded, decompressed data * @throws DataFormatException */ public static String decodeHeader(byte[] encoded) throws DataFormatException { // TODO: length limit on encoded? byte[] compressedBytes = Base64.decodeBase64(encoded); ByteArrayOutputStream out = new ByteArrayOutputStream(compressedBytes.length); Inflater inflater = new Inflater(); inflater.setInput(compressedBytes); byte[] buffer = new byte[1024]; while (!inflater.finished()) { int count = inflater.inflate(buffer); if (count == 0) break; out.write(buffer, 0, count); } try { inflater.end(); out.close(); } catch (IOException e) { } return new String(out.toByteArray()); }
From source file:Main.java
public static String getContent(Node node, boolean omitXMLDeclaration) { try {//from w ww. j ava 2s .com ByteArrayOutputStream baos = new ByteArrayOutputStream(); // Use a Transformer for output TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer transformer = tFactory.newTransformer(); transformer.setOutputProperty("indent", "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); transformer.setOutputProperty("encoding", "UTF-8"); if (omitXMLDeclaration) { transformer.setOutputProperty("omit-xml-declaration", "yes"); } DOMSource source = new DOMSource(node); StreamResult result = new StreamResult(baos); transformer.transform(source, result); String cont = baos.toString("UTF8"); baos.close(); return cont; } catch (Exception ex) { return ""; } }
From source file:gov.niem.ws.util.SecurityUtil.java
/** * Encode data with DEFLATE and then Base64 without chunking, so it can * safely be placed in an HTTP header.//w w w . j av a2 s . co m * * @param data * @return the compressed, b64 encoded data * @throws DataFormatException */ public static String encodeHeader(byte[] data) throws DataFormatException { // TODO: length limit on encoded? ByteArrayOutputStream out = new ByteArrayOutputStream(data.length); Deflater deflater = new Deflater(); deflater.setInput(data); deflater.finish(); byte[] buffer = new byte[1024]; while (!deflater.finished()) { int count = deflater.deflate(buffer); if (count == 0) break; out.write(buffer, 0, count); } try { deflater.end(); out.close(); } catch (IOException e) { } return new String(Base64.encodeBase64(out.toByteArray(), false)); }
From source file:Main.java
public static boolean saveBitmap(Bitmap aBmp, String aPath) { if (aBmp == null || aPath == null) { return false; }//from w w w. ja v a 2 s.co m FileOutputStream fos = null; ByteArrayOutputStream baos = null; boolean result; try { File file = new File(aPath); if (!file.exists()) { file.createNewFile(); } fos = new FileOutputStream(file); baos = new ByteArrayOutputStream(); aBmp.compress(Bitmap.CompressFormat.PNG, 100, baos); //SUPPRESS CHECKSTYLE fos.write(baos.toByteArray()); baos.flush(); fos.flush(); result = true; } catch (OutOfMemoryError e) { e.printStackTrace(); result = false; } catch (Exception e) { e.printStackTrace(); result = false; } finally { try { if (baos != null) { baos.close(); } if (fos != null) { fos.close(); } } catch (IOException e) { e.printStackTrace(); } } return result; }
From source file:com.granule.json.utils.XML.java
/** * Method to take an input stream to an XML document and return a String of the JSON format. Note that the xmlStream is not closed when read is complete. This is left up to the caller, who may wish to do more with it. * @param xmlStream The InputStream to an XML document to transform to JSON. * @param verbose Boolean flag denoting whther or not to write the JSON in verbose (formatted), or compact form (no whitespace) * @return A string of the JSON representation of the XML file * /* ww w . j av a2 s. c o m*/ * @throws SAXException Thrown if an error occurs during parse. * @throws IOException Thrown if an IOError occurs. */ public static String toJson(InputStream xmlStream, boolean verbose) throws SAXException, IOException { if (logger.isLoggable(Level.FINER)) { logger.exiting(className, "toJson(InputStream, boolean)"); } ByteArrayOutputStream baos = new ByteArrayOutputStream(); String result = null; try { toJson(xmlStream, baos, verbose); result = baos.toString("UTF-8"); baos.close(); } catch (UnsupportedEncodingException uec) { IOException iox = new IOException(uec.toString()); iox.initCause(uec); throw iox; } if (logger.isLoggable(Level.FINER)) { logger.exiting(className, "toJson(InputStream, boolean)"); } return result; }
From source file:com.granule.json.utils.XML.java
/** * Method to take an input stream to an JSON document and return a String of the XML format. Note that the JSONStream is not closed when read is complete. This is left up to the caller, who may wish to do more with it. * @param xmlStream The InputStream to an JSON document to transform to XML. * @param verbose Boolean flag denoting whther or not to write the XML in verbose (formatted), or compact form (no whitespace) * @return A string of the JSON representation of the XML file * /*from ww w . ja v a 2 s . c o m*/ * @throws IOException Thrown if an IOError occurs. */ public static String toXml(InputStream JSONStream, boolean verbose) throws IOException { if (logger.isLoggable(Level.FINER)) { logger.exiting(className, "toXml(InputStream, boolean)"); } ByteArrayOutputStream baos = new ByteArrayOutputStream(); String result = null; try { toXml(JSONStream, baos, verbose); result = baos.toString("UTF-8"); baos.close(); } catch (UnsupportedEncodingException uec) { IOException iox = new IOException(uec.toString()); iox.initCause(uec); throw iox; } if (logger.isLoggable(Level.FINER)) { logger.exiting(className, "toXml(InputStream, boolean)"); } return result; }
From source file:tr.com.turkcellteknoloji.turkcellupdater.Utilities.java
/** * Copies contents of the stream to a byte array and closes stream. * * @param is/*w ww . j a v a 2 s . co m*/ * source input stream * @return created byte array * @throws IOException */ static byte[] getAsByteArray(InputStream is) throws IOException { if (is == null) { return null; } final ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { Utilities.pipe(is, baos); } finally { try { is.close(); } catch (IOException e) { e.printStackTrace(); } baos.close(); } return baos.toByteArray(); }