List of usage examples for java.io ByteArrayOutputStream close
public void close() throws IOException
From source file:Main.java
public static byte[] readStream(InputStream in) throws Exception { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len = -1; while ((len = in.read(buffer)) != -1) { outputStream.write(buffer, 0, len); }//w ww .ja va 2s .c o m outputStream.close(); in.close(); return outputStream.toByteArray(); }
From source file:com.cloud.utils.security.CertificateHelper.java
public static byte[] buildAndSaveKeystore(String alias, String cert, String privateKey, String storePassword) throws KeyStoreException, CertificateException, NoSuchAlgorithmException, InvalidKeySpecException, IOException {//w w w . java2 s .com KeyStore ks = buildKeystore(alias, cert, privateKey, storePassword); ByteArrayOutputStream os = new ByteArrayOutputStream(); ks.store(os, storePassword != null ? storePassword.toCharArray() : null); os.close(); return os.toByteArray(); }
From source file:Main.java
public static byte[] readStream(InputStream inStream) throws Exception { ByteArrayOutputStream outstream = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len = -1; while ((len = inStream.read(buffer)) != -1) { outstream.write(buffer, 0, len); }// ww w.j a v a 2s.co m outstream.close(); inStream.close(); return outstream.toByteArray(); }
From source file:Main.java
public static byte[] bitmap2Bytes(Bitmap bitmap) { ByteArrayOutputStream baos; try {//from w ww. j ava 2 s . c o m baos = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos); byte[] b = baos.toByteArray(); baos.flush(); baos.close(); return b; } catch (Exception e) { e.printStackTrace(); } return null; }
From source file:Main.java
/** * Saves a file from the given URL to the given filename and returns the file * @param link URL to file/* www . j a v a 2s . c o m*/ * @param fileName Name to save the file * @return The file * @throws IOException Thrown if any IOException occurs */ public static File saveFileFromNet(URL link, String fileName) throws IOException { InputStream in = new BufferedInputStream(link.openStream()); ByteArrayOutputStream out = new ByteArrayOutputStream(); byte[] buf = new byte[1024]; int n = 0; while (-1 != (n = in.read(buf))) { out.write(buf, 0, n); } out.close(); in.close(); byte[] response = out.toByteArray(); File file = new File(fileName); if (!file.exists()) { if (file.getParentFile() != null) { file.getParentFile().mkdirs(); } file.createNewFile(); } FileOutputStream fos = new FileOutputStream(file); fos.write(response); fos.close(); return new File(fileName); }
From source file:Main.java
/** * Saves a file from the given URL using HTTPS to the given filename and returns the file * @param link URL to file//from w w w . java2 s . co m * @param fileName Name to save the file * @return The file * @throws IOException Thrown if any IOException occurs */ public static void saveFileFromNetHTTPS(URL link, String fileName) throws IOException { HttpsURLConnection con = (HttpsURLConnection) link.openConnection(); InputStream in = new BufferedInputStream(con.getInputStream()); ByteArrayOutputStream out = new ByteArrayOutputStream(); byte[] buf = new byte[1024]; int n = 0; while (-1 != (n = in.read(buf))) { out.write(buf, 0, n); } out.close(); in.close(); byte[] response = out.toByteArray(); File f = new File(fileName); if (f.getParentFile() != null) { if (f.getParentFile().mkdirs()) { System.out.println("Created Directory Structure"); } } FileOutputStream fos = new FileOutputStream(f); fos.write(response); fos.close(); }
From source file:ca.uhn.fhir.jpa.dao.GZipUtil.java
public static byte[] compress(String theEncoded) { try {//from ww w . j av a 2s .com ByteArrayOutputStream os = new ByteArrayOutputStream(); GZIPOutputStream gos = new GZIPOutputStream(os); IOUtils.write(theEncoded, gos, "UTF-8"); gos.close(); os.close(); byte[] retVal = os.toByteArray(); return retVal; } catch (IOException e) { throw new DataFormatException("Compress contents", e); } }
From source file:Main.java
public static byte[] toBytes(InputStream in) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); int ch;/*from ww w . java 2s. c o m*/ while ((ch = in.read()) != -1) { out.write(ch); } byte buffer[] = out.toByteArray(); out.close(); return buffer; }
From source file:Main.java
public static byte[] serialize(Object obj) throws Exception { //System.err.println(" ser ##"+obj); ByteArrayOutputStream bout = new ByteArrayOutputStream(); ObjectOutputStream oout = new ObjectOutputStream(bout); oout.writeObject(obj);// w w w . j av a 2 s .c om oout.flush(); byte array[] = bout.toByteArray(); oout.close(); bout.close(); //System.err.println(" ser #"+new String(array)); return array; }
From source file:Main.java
public static byte[] readStream(InputStream inputStream) throws Exception { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len = 0;/*from w w w . jav a 2 s . c om*/ while ((len = inputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, len); } outputStream.close(); inputStream.close(); return outputStream.toByteArray(); }