in firebase-storage/src/testUtil/java/com/google/firebase/storage/TestDownloadHelper.java [41:164]
public static Task<StreamDownloadResponse> streamDownload(
@Nullable final ProcessImage imageCallback,
@Nullable final ProcessBytes byteCallback,
String filename,
final int cancelAfter) {
StreamDownloadResponse response = new StreamDownloadResponse();
StorageReference storage = FirebaseStorage.getInstance().getReference(filename);
ControllableSchedulerHelper.getInstance().pause();
final StreamDownloadTask task =
storage.getStream(
(state, stream) -> {
try {
long totalByteCountBeginning = state.getTotalByteCount();
String statusMessage = "\ndoInBackground:\n" + streamTaskToString(state);
Log.i(TAG, statusMessage);
response.backgroundTask.append(statusMessage);
try {
bytes = IOUtils.toByteArray(stream);
icon = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
} catch (OutOfMemoryError e) {
Log.w(TAG, "Can't persist download due to low memory", e);
}
while (stream.read() != -1) {
// do nothing
}
if (state.getTotalByteCount() != -1) {
Preconditions.checkState(totalByteCountBeginning == state.getTotalByteCount());
Preconditions.checkState(bytes.length == state.getTotalByteCount());
}
} finally {
// Closing stream
stream.close();
}
});
try {
task.pause();
throw new RuntimeException(
"StreamDownload.pause() did not throw UnsupportedOperationException");
} catch (UnsupportedOperationException ignore) {
// Expected.
}
try {
task.resume();
throw new RuntimeException(
"StreamDownload.resume() did not throw UnsupportedOperationException");
} catch (UnsupportedOperationException ignore) {
// Expected.
}
if (cancelAfter == 0) {
task.cancel();
}
task.addOnSuccessListener(
state -> {
ControllableSchedulerHelper.getInstance().verifyCallbackThread();
String statusMessage = "\nonSuccess:\n" + streamTaskToString(state);
Log.i(TAG, statusMessage);
response.mainTask.append(statusMessage);
if (imageCallback != null) {
imageCallback.run(icon);
}
if (byteCallback != null) {
byteCallback.run(bytes);
}
})
.addOnFailureListener(
e -> {
ControllableSchedulerHelper.getInstance().verifyCallbackThread();
String statusMessage = "\nonFailure:\n" + e;
Log.i(TAG, statusMessage);
response.mainTask.append(statusMessage);
})
.addOnProgressListener(
state -> {
ControllableSchedulerHelper.getInstance().verifyCallbackThread();
String statusMessage = "\nonProgressUpdate:\n" + streamTaskToString(state);
Log.i(TAG, statusMessage);
response.mainTask.append(statusMessage);
if (cancelAfter != -1 && state.getBytesTransferred() >= cancelAfter) {
task.cancel();
}
})
.addOnCanceledListener(
() -> {
ControllableSchedulerHelper.getInstance().verifyCallbackThread();
String statusMessage = "\nonCanceled:";
Log.i(TAG, statusMessage);
response.mainTask.append(statusMessage);
})
.addOnCompleteListener(
completedTask -> {
ControllableSchedulerHelper.getInstance().verifyCallbackThread();
String statusMessage = "\nonComplete:Success=\n" + completedTask.isSuccessful();
Log.i(TAG, statusMessage);
response.mainTask.append(statusMessage);
long expectedTotalByteCount = completedTask.getResult().getTotalByteCount();
if (expectedTotalByteCount != -1) {
long bytesTransferred = completedTask.getResult().getBytesTransferred();
Preconditions.checkState(
expectedTotalByteCount == bytesTransferred,
"Expected transfer of %s bytes, but only received %s",
expectedTotalByteCount,
bytesTransferred);
}
});
ControllableSchedulerHelper.getInstance().resume();
return task.continueWithTask(
continuedTask -> {
TaskCompletionSource<StreamDownloadResponse> source = new TaskCompletionSource<>();
source.setResult(response);
return source.getTask();
});
}