}
// Loop, downloading chunks until the transfer is complete.
while (true) {
try {
Extent extent = null;
// Determine how much data to download. If we're at the last
// request in the transfer, only request as many bytes as needed
// to get to the end of the file.
if (currentBytes + buffer.getBuffer().length > totalBytes) {
// Would go past end of file. Request less bytes.
extent = new Extent(this.currentBytes, totalBytes
- currentBytes);
} else {
extent = new Extent(this.currentBytes,
buffer.getBuffer().length);
}
buffer.setSize((int) extent.getSize());
// Read data from the server.
byte[] obuffer = this.esu.readObject(id, extent, buffer.getBuffer(), checksum);
// See if they returned the buffer we're using. In some
// cases, this doesn't happen (when content length is not
// defined in the response).
if( obuffer != buffer.getBuffer() ) {
if( obuffer.length != extent.getSize() ) {
throw new EsuException( "Read size mismatch. " +
"Requested " + extent.getSize() +
" bytes but received " +
obuffer.length + " bytes" );
}
stream.write( obuffer, 0, obuffer.length );
} else {