buf.clear();
while (in.read(buf) >= 0 || buf.position != 0) {
buf.flip();
out.write(buf);
buf.compact();
}
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);
offset += write;
if ( offset < length ) {
System.arraycopy(buf, offset, buf, 0, length - offset);
offset = length - offset;
length = buf.length;
} else {
offset = 0;
length = 0;
}
}