int total = 0;
while (true) {
int got = channel.read(b);
if (got < 0) {
if (got <= 0) {
return (total == 0) ? -1 : total;
}
total += got;