Skip to content

Commit

Permalink
Reduce byte[] allocations during reads via ApacheCtrDecryptingSeekabl…
Browse files Browse the repository at this point in the history
…eInput (#561)
  • Loading branch information
JacekLach authored and dtobin committed Oct 7, 2021
1 parent 81ca23e commit 9b2b7ac
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 11 deletions.
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));
}
}

0 comments on commit 9b2b7ac

Please sign in to comment.