Do not run busy cycles. You probably prefer to use streams. The read()stream method is blocked until the data arrives, so your code will be simple without busy cycles and will work exactly the way you want:
while ( in.read() != -1) {
}
or even better to use buffers:
byte[] buf = new buf[MAX_SIZE]
while ( in.read(buf) != -1) {
}
source
share