Report abuse

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
buf.clear();
while (in.read(buf) >= 0 || buf.position != 0) {
	buf.flip();
	out.write(buf);
	buf.compact();
}

// vs.

int offset = 0;
int length = 0;
do {
	int read = in.read(buf, offset, length);
	if ( read == -1 && offset == 0 )
		break;
	else if ( read != -1 )
		offset += read;
	length = offset;
	offset = 0;
	int write = out.write(buf, offset, length); // Normal java.io doesn't actually provide a partial write, so we're just pretending it does
	offset += write;
	if ( offset < length ) {
		System.arraycopy(buf, offset, buf, 0, length - offset);
		offset = length - offset;
		length = buf.length;
	} else {
		offset = 0;
		length = 0;
	}
}