Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reuse a buffer in ACDSI.InputAdapter #561

Merged
merged 7 commits into from
Sep 9, 2021
Merged
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ public void close() throws IOException {
}

private static final class InputAdapter implements Input {

private SeekableInput input;
private final SeekableInput input;
private final byte[] readBuffer = new byte[BUFFER_SIZE];

private InputAdapter(SeekableInput input) {
this.input = input;
Expand All @@ -82,14 +82,23 @@ public int read(long position, byte[] buffer, int offset, int length) throws IOE

@Override
public int read(ByteBuffer dst) throws IOException {
byte[] bytes = new byte[dst.remaining()];
int read = input.read(bytes, 0, bytes.length);

if (read != -1) {
dst.put(bytes, 0, read);
int toRead = dst.remaining();
int totalRead = 0;

while (toRead > 0) {
int chunk = Math.min(toRead, readBuffer.length);
int read = input.read(readBuffer, 0, chunk);

if (read == -1) {
return totalRead;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this needs to be:

if (read == -1) {
    if (totalRead == 0) {
        totalRead = -1;
    }
}

there's a reference implementation here: https://github.com/apache/commons-crypto/blob/master/src/main/java/org/apache/commons/crypto/stream/input/StreamInput.java#L58-L75

I think this logic ends up being complex enough to warrant a few tests as well. When I was playing with this myself I pulled the InputAdapter up. I'm still trying to find some common util that we can use here but haven't found anything yet.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was under the impression that DecryptionTests would cover this - but in fact am struggling to have those run locally (tests in intellij die with 'Process finished with exit code 134 (interrupted by signal 6: SIGABRT)') - is there a trick to it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

} else {
dst.put(readBuffer, 0, read);
totalRead += read;
toRead -= read;
}
}

return read;
return totalRead;
}

@Override
Expand All @@ -113,5 +122,4 @@ public void close() throws IOException {
input.close();
}
}

}