List of usage examples for java.io InputStream read
public int read(byte b[]) throws IOException
b
. From source file:Main.java
public static ByteArrayOutputStream toByteArrayOutputStream(InputStream is) throws IOException { final ByteArrayOutputStream baos = new ByteArrayOutputStream(); final byte[] buffer = new byte[512]; int length = 0; while ((length = is.read(buffer)) != -1) { baos.write(buffer, 0, length);// ww w . ja v a 2s . co m } return baos; }
From source file:Main.java
public static long copy(InputStream input, OutputStream output) throws IOException { byte[] buffer = new byte[1024]; long count = 0; int n = 0;/*w w w . j a v a2s. co m*/ while (-1 != (n = input.read(buffer))) { output.write(buffer, 0, n); count += n; } return count; }
From source file:Main.java
/** * Copy the content of the input stream into the output stream, using a * temporary byte array buffer whose size is defined by * {@link #FILE_BUFFER_SIZE}.//from w w w . j a v a 2s . com * * @param in * The input stream to copy from. * @param out * The output stream to copy to. * * @throws java.io.IOException * If any error occurs during the copy. */ public static void copyFile(InputStream in, OutputStream out, long length) throws IOException { long totalRead = 0; byte[] b = new byte[FILE_BUFFER_SIZE]; int read; while ((read = in.read(b)) > 0) { out.write(b, 0, read); out.flush(); totalRead += read; if (totalRead >= length) break; } }
From source file:StreamUtils.java
public static byte[] readInByteArray(InputStream in) throws IOException { byte[] result = new byte[in.available()]; in.read(result); in.close();// w w w .j av a 2 s. co m return result; }
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 va2s . co 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[] 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 w w. j a va 2s .co m*/ return outputStream.toByteArray(); }
From source file:Main.java
/** * Exhaust the content of the representation by reading it and silently * discarding anything read./* ww w. j a v a2 s . c o m*/ * * @param input * The input stream to exhaust. * @return The number of bytes consumed or -1 if unknown. */ public static long exhaust(InputStream input) throws IOException { long result = -1L; if (input != null) { byte[] buf = new byte[2048]; int read = input.read(buf); result = (read == -1) ? -1 : 0; while (read != -1) { result += read; read = input.read(buf); } } return result; }
From source file:Main.java
/** * Copies all bytes from the input stream to the output stream. * Does not close or flush either stream. * * @param from the input stream to read from * @param to the output stream to write to * @return the number of bytes copied/* ww w . ja v a2s .c om*/ * @throws IOException if an I/O error occurs */ private static long copy(InputStream from, OutputStream to) throws IOException { byte[] buf = new byte[8192]; long total = 0; while (true) { int r = from.read(buf); if (r == -1) { break; } to.write(buf, 0, r); total += r; } return total; }
From source file:Main.java
private static String isStreamString(InputStream is) { ByteArrayOutputStream bo = new ByteArrayOutputStream(); byte[] buf = new byte[1024]; int len = -1; try {/*from w ww . j a v a 2 s. c om*/ while ((len = is.read(buf)) != -1) { bo.write(buf, 0, len); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { if (is != null) try { is.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return replaceBr(new String(bo.toByteArray())); }
From source file:Main.java
static long copy(InputStream from, OutputStream to, int bufferSize) throws IOException { byte[] buf = new byte[bufferSize]; long total = 0; while (true) { int read = from.read(buf); if (read < 0) break; to.write(buf, 0, read);/* w w w .j a v a 2 s . c o m*/ total += read; } return total; }