Here you can find the source of readall(InputStream in, byte[] buffer, int offset, int len)
Parameter | Description |
---|---|
in | The stream of data which comes via network. |
buffer | The target buffer. |
offset | The start offset in array buffer at which the data is written. |
len | The maximum number of bytes to read. |
Parameter | Description |
---|---|
IOException | thrown when interrupted during busy waiting |
public static int readall(InputStream in, byte[] buffer, int offset, int len) throws IOException
//package com.java2s; //License from project: Open Source License import java.io.IOException; import java.io.InputStream; public class Main { private static final int POLL_INTERVAL = 50; /**// www.ja v a 2 s .c o m * Receives data. Will read exactly the given number of bytes. Only if the * end-of-file is reached, it might be that we return less bytes. * * @param in The stream of data which comes via network. * @param buffer The target buffer. * @param offset The start offset in array buffer at which the data is written. * @param len The maximum number of bytes to read. * @return The number of bytes actually read is returned as an integer. * @throws IOException thrown when interrupted during busy waiting */ public static int readall(InputStream in, byte[] buffer, int offset, int len) throws IOException { // use polling to read all data int total = 0; for (;;) { int received = in.read(buffer, offset, len); if (received < 0) { return total; } total += received; offset += received; len -= received; if (len == 0) { return total; } try { Thread.sleep(POLL_INTERVAL); } catch (InterruptedException e) { throw new IOException("Interrupted during busy waiting", e); } } } }