List of usage examples for java.io ByteArrayOutputStream write
public synchronized void write(byte b[], int off, int len)
From source file:Main.java
public static byte[] readFileToByteArray2(File file) { try {/*from w ww .j a v a 2 s . c om*/ FileInputStream fis = new FileInputStream(file); ByteArrayOutputStream bos = new ByteArrayOutputStream(); byte[] buf = new byte[1024]; for (int readNum; (readNum = fis.read(buf)) != -1;) { bos.write(buf, 0, readNum); } bos.flush(); byte[] bytes = bos.toByteArray(); fis.close(); bos.close(); return bytes; } catch (IOException e) { return null; } }
From source file:com.eucalyptus.auth.crypto.StringCryptoTest.java
private static byte[] readfile(String filename) throws Exception { FileInputStream fis = new FileInputStream(filename); ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] block = new byte[512]; int n;//from w ww .j a v a 2 s . c o m while ((n = fis.read(block)) > 0) { baos.write(block, 0, n); } byte[] bytes = baos.toByteArray(); baos.close(); return bytes; }
From source file:Main.java
public static byte[] slurpToByteArray(InputStream inputStream) throws IOException { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); byte[] buffer = new byte[4096]; for (int numRead; (numRead = inputStream.read(buffer)) != -1;) { outputStream.write(buffer, 0, numRead); }/*from w ww . ja v a 2 s . c o m*/ return outputStream.toByteArray(); }
From source file:Main.java
public static byte[] getBytesFromFile(File f) { if (f == null) { return null; }// w w w . j ava 2 s . c o m try { FileInputStream stream = new FileInputStream(f); ByteArrayOutputStream out = new ByteArrayOutputStream(1000); byte[] b = new byte[1000]; for (int n; (n = stream.read(b)) != -1;) { out.write(b, 0, n); } stream.close(); out.close(); return out.toByteArray(); } catch (IOException e) { } return null; }
From source file:Main.java
public static byte[] readFromInputStream(InputStream inputStream) throws IOException { if (inputStream == null) return null; BufferedInputStream bin = new BufferedInputStream(inputStream); ByteArrayOutputStream bos = new ByteArrayOutputStream(); byte[] buffer = new byte[1024 * 8]; int len = 0;/*from www . j av a2 s.c o m*/ while ((len = bin.read(buffer)) != -1) { bos.write(buffer, 0, len); } bin.close(); return bos.toByteArray(); }
From source file:Main.java
public static String getStringFromInput(InputStream in) { ByteArrayOutputStream bos = new ByteArrayOutputStream(); int c = 0;/* w w w.j a v a 2 s . co m*/ byte[] buffer = new byte[1024]; try { while ((c = in.read(buffer)) != -1) { bos.write(buffer, 0, c); } in.close(); } catch (IOException e) { e.printStackTrace(); } return new String(bos.toByteArray()); }
From source file:Main.java
public static byte[] decompressInGzip(byte[] compressData, int offset, int length) throws Exception { ByteArrayInputStream bis = new ByteArrayInputStream(compressData, offset, length); GZIPInputStream gzipInStream = new GZIPInputStream(bis); ByteArrayOutputStream bos = new ByteArrayOutputStream(); int count;// w w w. j a v a 2 s. c o m byte[] buf = new byte[1024]; while ((count = gzipInStream.read(buf)) > 0) { bos.write(buf, 0, count); } gzipInStream.close(); byte[] originalData = bos.toByteArray(); bos.close(); return originalData; }
From source file:Main.java
public static byte[] File2byte(String filePath) { byte[] buffer = null; try {//ww w .j a v a 2s . c o m File file = new File(filePath); FileInputStream fis = new FileInputStream(file); ByteArrayOutputStream bos = new ByteArrayOutputStream(); byte[] b = new byte[1024]; int n; while ((n = fis.read(b)) != -1) { bos.write(b, 0, n); } fis.close(); bos.close(); buffer = bos.toByteArray(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return buffer; }
From source file:Main.java
public static byte[] inputStreamToBytes(InputStream inputStream) throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] buffer = new byte[2048]; int read = 0; while ((read = inputStream.read(buffer, 0, buffer.length)) != -1) baos.write(buffer, 0, read); baos.flush();//from w ww . j a v a 2s. c o m return baos.toByteArray(); }
From source file:Main.java
private static byte[] getFileBytes(String filePath) { byte[] buffer = null; try {/*from w ww.j a va 2s. c o m*/ File file = new File(filePath); FileInputStream fis = new FileInputStream(file); ByteArrayOutputStream bos = new ByteArrayOutputStream(1024); byte[] b = new byte[1024]; int n; while ((n = fis.read(b)) != -1) { bos.write(b, 0, n); } fis.close(); bos.close(); buffer = bos.toByteArray(); } catch (FileNotFoundException e) { } catch (IOException e) { } return buffer; }