List of usage examples for java.io ByteArrayOutputStream close
public void close() throws IOException
From source file:Main.java
public static byte[] zipCompress(byte[] input, int level) { Deflater compressor = new Deflater(); compressor.setLevel(level);/*from ww w . j av a 2 s. c o m*/ compressor.setInput(input); compressor.finish(); ByteArrayOutputStream bos = new ByteArrayOutputStream(input.length); byte[] buf = new byte[1024]; while (!compressor.finished()) { int count = compressor.deflate(buf); bos.write(buf, 0, count); } try { bos.close(); } catch (IOException e) { } return bos.toByteArray(); }
From source file:Main.java
public static byte[] loadImageDataFromWeb(String url) { byte[] imageData = null; try {//from w w w . j a v a 2 s . c om URLConnection connection = new URL(url).openConnection(); InputStream stream = connection.getInputStream(); //BufferedInputStream in=new BufferedInputStream(stream);//default 8k buffer BufferedInputStream in = new BufferedInputStream(stream, 10240);//YG: 10k=10240, 2x8k=16384 ByteArrayOutputStream out = new ByteArrayOutputStream(10240); int read; byte[] b = new byte[4096]; while ((read = in.read(b)) != -1) { out.write(b, 0, read); } out.flush(); out.close(); imageData = out.toByteArray(); } catch (Exception e) { Log.w(TAG, "Exc=" + e); return null; } return imageData; }
From source file:Main.java
public static byte[] bmpToByteArray(Bitmap bitmap, boolean flag) { ByteArrayOutputStream bytearrayoutputstream = new ByteArrayOutputStream(); bitmap.compress(android.graphics.Bitmap.CompressFormat.JPEG, 100, bytearrayoutputstream); if (flag)// www . j av a 2s .co m bitmap.recycle(); byte abyte0[] = bytearrayoutputstream.toByteArray(); try { bytearrayoutputstream.close(); } catch (Exception exception) { exception.printStackTrace(); } return abyte0; }
From source file:Main.java
public static byte[] base64_decode(String s) { ByteArrayOutputStream bos = new ByteArrayOutputStream(); try {//from www.j a v a 2 s . c om decode(s, bos); } catch (IOException e) { throw new RuntimeException(); } byte[] decodedBytes = bos.toByteArray(); try { bos.close(); bos = null; } catch (IOException ex) { System.err.println("Error while decoding BASE64: " + ex.toString()); } return decodedBytes; }
From source file:com.aqnote.shared.cryptology.cert.io.PKCSTransformer.java
public static String getP12FileString2(KeyStore keyStore, char[] passwd) throws Exception { ByteArrayOutputStream out = new ByteArrayOutputStream(); keyStore.store(out, passwd);/* w w w .j a va 2s . c om*/ out.flush(); String p12File = Base64.encodeBase64String(out.toByteArray()); out.close(); return p12File; }
From source file:Main.java
public static byte[] toByteArray(InputStream is) { byte[] ret = null; try {//from ww w .j a v a 2 s . co m ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); int ch; while ((ch = is.read()) != -1) { byteArrayOutputStream.write(ch); } ret = byteArrayOutputStream.toByteArray(); byteArrayOutputStream.close(); } catch (Exception e) { } finally { try { is.close(); } catch (IOException e) { } } return ret; }
From source file:Main.java
public static String readInStream(InputStream inStream) { try {//from w ww . j a v a2 s. c o m ByteArrayOutputStream outStream = new ByteArrayOutputStream(); byte[] buffer = new byte[512]; int length = -1; while ((length = inStream.read(buffer)) != -1) { outStream.write(buffer, 0, length); } outStream.close(); inStream.close(); return outStream.toString(); } catch (IOException e) { Log.i("FileTest", e.getMessage()); } return null; }
From source file:Main.java
public static byte[] bitmapToByteArray(Bitmap bitmap, int imageQuality) { ByteArrayOutputStream bos = null; try {/*from w ww.j a va 2 s . c o m*/ bos = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.JPEG, imageQuality, bos); return bos.toByteArray(); } finally { if (bos != null) { try { bos.close(); } catch (IOException e) { Log.w(TAG, "Failed to close ByteArrayOutputStream", e); // Ignore exception } } } }
From source file:Main.java
public static byte[] decode(String s) { ByteArrayOutputStream bos = new ByteArrayOutputStream(); try {/*from ww w . j a v a 2s .com*/ decode(s, bos); } catch (IOException e) { throw new RuntimeException(); } byte[] decodedBytes = bos.toByteArray(); try { bos.close(); bos = null; } catch (IOException ex) { System.err.println("Error while decoding BASE64: " + ex.toString()); } return decodedBytes; }
From source file:javafx1.Testpics.java
public static String encodeToString(BufferedImage image, String type) { String imageString = null;/*w w w .j ava 2 s .com*/ ByteArrayOutputStream bos = new ByteArrayOutputStream(); try { ImageIO.write(image, type, bos); byte[] imageBytes = bos.toByteArray(); BASE64Encoder encoder = new BASE64Encoder(); imageString = encoder.encode(imageBytes); bos.close(); } catch (IOException e) { e.printStackTrace(); } return imageString; }