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 String readTextFile(InputStream inputStream) { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); byte buf[] = new byte[1024]; int len;//from www. ja va 2 s. com try { while ((len = inputStream.read(buf)) != -1) { outputStream.write(buf, 0, len); } outputStream.close(); inputStream.close(); } catch (IOException e) { } return outputStream.toString(); }
From source file:Main.java
public static byte[] readFully(File file) throws IOException { final InputStream in = new FileInputStream(file); try {//from w w w. j av a2s .c o m ByteArrayOutputStream bytes = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int count; while ((count = in.read(buffer)) != -1) { bytes.write(buffer, 0, count); } return bytes.toByteArray(); } finally { in.close(); } }
From source file:Main.java
public static byte[] fileToByte(String filePath) throws Exception { byte[] data = new byte[0]; File file = new File(filePath); if (file.exists()) { FileInputStream in = new FileInputStream(file); ByteArrayOutputStream out = new ByteArrayOutputStream(2048); byte[] cache = new byte[CACHE_SIZE]; int nRead = 0; while ((nRead = in.read(cache)) != -1) { out.write(cache, 0, nRead); out.flush();/*from w w w. java 2s .com*/ } out.close(); in.close(); data = out.toByteArray(); } return data; }
From source file:Main.java
public static byte[] readInputStreamFully(InputStream is) { ByteArrayOutputStream os = new ByteArrayOutputStream(); byte[] buffer = new byte[32768]; int count;/*w w w . j a va2 s. c o m*/ try { while ((count = is.read(buffer)) != -1) { os.write(buffer, 0, count); } is.close(); } catch (IOException e) { throw new RuntimeException(e); } return os.toByteArray(); }
From source file:Main.java
public static byte[] stream2bytes(InputStream inStream) throws IOException { byte[] buff = new byte[1024]; byte[] data = null; try {/*from w ww . j a v a2 s . c o m*/ ByteArrayOutputStream swapStream = new ByteArrayOutputStream(); int read = 0; while ((read = inStream.read(buff, 0, 100)) > 0) { swapStream.write(buff, 0, read); } data = swapStream.toByteArray(); } catch (Exception e) { e.printStackTrace(); } return data; }
From source file:Main.java
public static byte[] readInputStream(InputStream inStream) throws Exception { ByteArrayOutputStream outSteam = new ByteArrayOutputStream(); byte[] buffer = new byte[4096]; int len = 0;// w w w .j a v a2s . c o m while ((len = inStream.read(buffer)) != -1) { outSteam.write(buffer, 0, len); } outSteam.close(); inStream.close(); return outSteam.toByteArray(); }
From source file:Main.java
public static byte[] readStream(InputStream inStream) { try {//from w w w.ja v a 2 s . c o m ByteArrayOutputStream outStream = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len = 0; while ((len = inStream.read(buffer)) != -1) { outStream.write(buffer, 0, len); } byte[] data = outStream.toByteArray(); outStream.flush(); outStream.close(); inStream.close(); return data; } catch (OutOfMemoryError e) { return null; } catch (Exception e) { return null; } }
From source file:Main.java
public static byte[] getBytesFromStream(InputStream inputStream) { ByteArrayOutputStream os = new ByteArrayOutputStream(1024); byte[] buffer = new byte[1024]; int len;/*from w ww .j a v a 2 s.c o m*/ try { while ((len = inputStream.read(buffer)) >= 0) { os.write(buffer, 0, len); } } catch (IOException e) { e.printStackTrace(); } return os.toByteArray(); }
From source file:io.undertow.testutils.HttpClientUtils.java
public static byte[] readRawResponse(InputStream stream) throws IOException { final ByteArrayOutputStream b = new ByteArrayOutputStream(); byte[] data = new byte[100]; int read;//from w ww . ja v a2 s . com while ((read = stream.read(data)) != -1) { b.write(data, 0, read); } return b.toByteArray(); }
From source file:Main.java
public static byte[] downloadImageFromURL(String strUrl) throws Exception { InputStream in;/*from w ww. jav a 2 s .co m*/ ByteArrayOutputStream out = new ByteArrayOutputStream(); URL url = new URL(strUrl); in = new BufferedInputStream(url.openStream()); byte[] buf = new byte[2048]; int n = 0; while (-1 != (n = in.read(buf))) { out.write(buf, 0, n); } out.close(); in.close(); byte[] response = out.toByteArray(); FileOutputStream fos = new FileOutputStream("/Users/image.jpg"); fos.write(response); fos.close(); return response; }