Skip to content

Commit

Permalink
Simplify CountingInputstreamWithCallback and remove unnecessary const…
Browse files Browse the repository at this point in the history
…ructs (#86)

## Goal

Simplified implementation of the wrapped stream to not return the bytes in the callback. This could probably be furthered simplified to not count bytes, but there's some logic there involving stream marking and reseting that I don't fully grok yet, so I'd rather not touch it.

I've also removed a couple of backwards compatible constructs that aren no longer necessary.

## Testing

Existing tests pass.
  • Loading branch information
bidetofevil committed Nov 17, 2023
2 parents 69354c9 + 22b981f commit a2d22e3
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
import java.io.InputStream;
import java.util.concurrent.atomic.AtomicLong;

import io.embrace.android.embracesdk.utils.Consumer;
import kotlin.Unit;
import kotlin.jvm.functions.Function1;

/**
* Counts the bytes read from an input stream and invokes a callback once the stream has reached
Expand All @@ -20,9 +21,9 @@ final class CountingInputStreamWithCallback extends FilterInputStream {
*/
private volatile long streamMark = -1;
/**
* The callback to be invoked with num of bytes after reaching the end of the stream.
* The callback to be invoked with raw bytes after reaching the end of the stream.
*/
private final Consumer<Long, byte[]> callback;
private final Function1<byte[], Unit> callback;

/**
* true if the callback has been invoked, false otherwise.
Expand All @@ -38,15 +39,14 @@ final class CountingInputStreamWithCallback extends FilterInputStream {

ByteArrayOutputStream os = new ByteArrayOutputStream();


/**
* Wraps another input stream, counting the number of bytes read.
*
* @param in the input stream to be wrapped
*/
CountingInputStreamWithCallback(InputStream in,
boolean shouldCaptureBody,
@NonNull Consumer<Long, byte[]> callback) {
@NonNull Function1<byte[], Unit> callback) {
super(in);
this.callback = callback;
this.shouldCaptureBody = shouldCaptureBody;
Expand Down Expand Up @@ -124,6 +124,6 @@ private void conditionallyCaptureBody(byte[] b, int off, int len) {

private void notifyCallback() {
callbackCompleted = true;
callback.accept(count.longValue(), os.toByteArray());
callback.invoke(os.toByteArray());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -659,11 +659,12 @@ private CountingInputStreamWithCallback countingInputStream(InputStream inputStr
return new CountingInputStreamWithCallback(
inputStream,
hasNetworkCaptureRules(),
(bytesCount, responseBody) -> {
(responseBody) -> {
if (startTime != null && endTime != null) {
cacheNetworkCallData(responseBody);
internalLogNetworkCall(startTime, endTime,true);
internalLogNetworkCall(startTime, endTime, true);
}
return null;
});
}

Expand Down

This file was deleted.

This file was deleted.

0 comments on commit a2d22e3

Please sign in to comment.