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

Backport buffer re-use (#561) to 2.x #576

Merged
merged 1 commit into from
Oct 20, 2021
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -16,6 +16,7 @@

package com.palantir.crypto2.io;

import com.google.common.annotations.VisibleForTesting;
import com.palantir.crypto2.cipher.ApacheCiphers;
import com.palantir.crypto2.keys.KeyMaterial;
import com.palantir.seekio.SeekableInput;
Expand Down Expand Up @@ -66,11 +67,12 @@ public void close() throws IOException {
super.close();
}

private static final class InputAdapter implements Input {
@VisibleForTesting
static final class InputAdapter implements Input {
private final SeekableInput input;
private final byte[] readBuffer = new byte[BUFFER_SIZE];

private SeekableInput input;

private InputAdapter(SeekableInput input) {
InputAdapter(SeekableInput input) {
this.input = input;
}

Expand All @@ -82,14 +84,28 @@ 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) {
if (totalRead == 0) {
// first read hit EOF
return -1;
} else {
return totalRead;
}
} else {
dst.put(readBuffer, 0, read);
totalRead += read;
toRead -= read;
}
}

return read;
return totalRead;
}

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

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* (c) Copyright 2021 Palantir Technologies Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.palantir.crypto2.io;

import static org.assertj.core.api.Assertions.assertThat;

import com.palantir.seekio.InMemorySeekableDataInput;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.Random;
import org.junit.BeforeClass;
import org.junit.Test;

public final class ApacheCtrDecryptingSeekableInputTests {
private static final int NUM_BYTES = 1024 * 1024;
private static final Random random = new Random(0);
private static byte[] data;

@BeforeClass
public static void beforeClass() throws IOException {
data = new byte[NUM_BYTES];
random.nextBytes(data);
}

@Test
public void testEmptyRead() throws IOException {
ByteBuffer dst = ByteBuffer.allocate(1024);
byte[] emptyData = new byte[] {};

ApacheCtrDecryptingSeekableInput.InputAdapter adapter = inputAdapter(emptyData);
assertThat(adapter.read(dst)).isEqualTo(-1);
assertThat(dst.position()).isEqualTo(0);
}

@Test
public void testFullRead() throws IOException {
ByteBuffer dst = ByteBuffer.allocate(2 * NUM_BYTES);

ApacheCtrDecryptingSeekableInput.InputAdapter adapter = inputAdapter(data);
assertThat(adapter.read(dst)).isEqualTo(NUM_BYTES);
assertThat(dst.position()).isEqualTo(NUM_BYTES);
}

@Test
public void testPartialRead() throws IOException {
int toRead = NUM_BYTES / 2;
ByteBuffer dst = ByteBuffer.allocate(toRead);

ApacheCtrDecryptingSeekableInput.InputAdapter adapter = inputAdapter(data);
assertThat(adapter.read(dst)).isEqualTo(toRead);
assertThat(dst.position()).isEqualTo(toRead);
}

private ApacheCtrDecryptingSeekableInput.InputAdapter inputAdapter(byte[] inputData) {
return new ApacheCtrDecryptingSeekableInput.InputAdapter(new InMemorySeekableDataInput(inputData));
}
}