while (true) {
read = read(buffer);
if (read < 0) {
if (length >= 0 && totalRead < length) {
// We've reached the end of the file, but it's unexpected.
throw new DropboxPartialFileException(totalRead);
}
// TODO check for partial success, if possible
break;
}
bos.write(buffer, 0, read);
totalRead += read;
if (listener != null) {
long now = System.currentTimeMillis();
if (now - lastListened > listener.progressInterval()) {
lastListened = now;
listener.onProgress(totalRead, length);
}
}
}
bos.flush();
os.flush();
// Make sure it's flushed out to disk
try {
if (os instanceof FileOutputStream) {
((FileOutputStream)os).getFD().sync();
}
} catch (SyncFailedException e) {
}
} catch (IOException e) {
String message = e.getMessage();
if (message != null && message.startsWith("No space")) {
// This is a hack, but it seems to be the only way to check
// which exception it is.
throw new DropboxLocalStorageFullException();
} else {
/*
* If the output stream was closed, we notify the caller
* that only part of the file was copied. This could have
* been because this request is being intentionally
* canceled.
*/
throw new DropboxPartialFileException(totalRead);
}
} finally {
if (bos != null) {
try {
bos.close();