For many years, I read from InputStreams in a loop as follows:
final byte[] buffer = new byte[65536];
InputStream is = ...;
int r;
while ((r = is.read(buffer)) > 0) {
...
}
But I wonder if there is a way to avoid this assignment in the loop (without introducing the second condition) - for example. I find this code even less elegant since there are two read statements and two conditions:
r = is.read(buffer);
if (r > 0) {
do {
...
r = is.read(buffer);
} while (r > 0);
}
Any ideas for a more elegant (compact, without conditional) design?
source
share