Skip to content

Commit

Permalink
chore: Optimize readNBytes for ByteBufferInputStream.
Browse files Browse the repository at this point in the history
  • Loading branch information
nstdio committed Mar 18, 2022
1 parent 7cb13e9 commit d00a56f
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 12 deletions.
8 changes: 5 additions & 3 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,18 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
env:
FETCH_DEPTH: ${{ env.RUN_ANALYZE == 'true' && '0' || '1' }}
with:
fetch-depth: ${{ env.RUN_ANALYZE == true && '0' || '1' }}
fetch-depth: ${{ env.FETCH_DEPTH }}
- name: Set up JDK 11
uses: actions/setup-java@v2
with:
distribution: 'temurin'
java-version: 11
cache: 'gradle'
- name: Cache SonarCloud packages
if: ${{ env.RUN_ANALYZE == true }}
if: ${{ env.RUN_ANALYZE == 'true' }}
uses: actions/cache@v1
with:
path: ~/.sonar/cache
Expand All @@ -33,7 +35,7 @@ jobs:
IO_GITHUB_NSTDIO_HTTP_EXT_CLIENT_TEST_CACHE_WRITE_DELAY: PT2.5S
run: ./gradlew build --stacktrace --scan
- name: Analyze
if: ${{ env.RUN_ANALYZE == true }}
if: ${{ env.RUN_ANALYZE == 'true' }}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Needed to get PR information, if any
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ public int read(byte[] b, int off, int len) throws IOException {
return read;
}

@Override
public int read(byte[] b) throws IOException {
return read(b, 0, b.length);
}

private ByteBuffer nextBuffer() {
Deque<ByteBuffer> buffs = buffers;
ByteBuffer buf = buffs.peek();
Expand Down Expand Up @@ -140,6 +145,24 @@ public boolean markSupported() {
return true;
}

@Override
public byte[] readAllBytes() throws IOException {
return readNBytes(Integer.MAX_VALUE);
}

@Override
public byte[] readNBytes(int len) throws IOException {
if (len < 0) {
throw new IllegalArgumentException("len < 0");
}

int available = Math.min(available(), len);
byte[] buf = new byte[available];
read(buf, 0, available);

return buf;
}

@Override
public void close() {
buffers.clear();
Expand All @@ -152,7 +175,7 @@ private void ensureOpen() throws IOException {
throw new IOException("closed");
}

void addBuffer(ByteBuffer b) {
void add(ByteBuffer b) {
if (!closed) {
b = b.duplicate().asReadOnlyBuffer();
if (!b.hasRemaining()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public void onNext(List<ByteBuffer> item) {
return;
}

item.forEach(is::addBuffer);
item.forEach(is::add);

if (decompressingStream == null) {
// trying to buffer at least 10 bytes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import static org.assertj.core.api.Assertions.assertThatIOException;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.apache.commons.io.IOUtils;
Expand Down Expand Up @@ -49,7 +50,7 @@ static Stream<Named<byte[]>> fullReadingData() {
void fullReading(byte[] bytes) throws IOException {
//given
var is = new ByteBufferInputStream();
toBuffers(bytes, false).forEach(is::addBuffer);
toBuffers(bytes, false).forEach(is::add);

//when
var actual = IOUtils.toByteArray(is);
Expand All @@ -63,7 +64,7 @@ void fullReading(byte[] bytes) throws IOException {
void shouldReadAllBytes(byte[] bytes) throws IOException {
//given
var is = new ByteBufferInputStream();
toBuffers(bytes, false).forEach(is::addBuffer);
toBuffers(bytes, false).forEach(is::add);

//when
var actual = is.readAllBytes();
Expand All @@ -77,7 +78,7 @@ void shouldReturnNegativeWhenInputIsEmpty() throws IOException {
//given
var is = new ByteBufferInputStream();
byte[] bytes = nextBytes(8);
is.addBuffer(ByteBuffer.wrap(bytes));
is.add(ByteBuffer.wrap(bytes));

//when
int actual = is.read(new byte[0]);
Expand All @@ -91,7 +92,7 @@ void shouldReadSingleProperly() throws IOException {
//given
var is = new ByteBufferInputStream();
String randomString = RandomStringUtils.randomAlphabetic(64);
toBuffers(randomString.getBytes(), false).forEach(is::addBuffer);
toBuffers(randomString.getBytes(), false).forEach(is::add);
ByteArrayOutputStream out = new ByteArrayOutputStream();

//when
Expand All @@ -113,9 +114,9 @@ void shouldFlipBuffer() throws IOException {
toBuffers(bytes, false)
.stream()
.map(buffer -> buffer.position(buffer.limit()))
.forEach(is::addBuffer);
.forEach(is::add);

is.addBuffer(ByteBuffer.wrap(new byte[0]));
is.add(ByteBuffer.wrap(new byte[0]));

//when
byte[] actual = is.readAllBytes();
Expand Down Expand Up @@ -146,7 +147,7 @@ void shouldReportAvailable() throws Exception {
//given
byte[] bytes = nextBytes(32);
var is = new ByteBufferInputStream();
toBuffers(bytes, false).forEach(is::addBuffer);
toBuffers(bytes, false).forEach(is::add);

//when
int actual = is.available();
Expand All @@ -155,6 +156,44 @@ void shouldReportAvailable() throws Exception {
assertEquals(bytes.length, actual);
}

@Test
void shouldReadAllBytes() throws Exception {
//given
byte[] bytes = nextBytes(32);
var is = new ByteBufferInputStream();
toBuffers(bytes, false).forEach(is::add);

//when
byte[] actual = is.readAllBytes();

//then
assertArrayEquals(bytes, actual);
}

@Test
void shouldThrowWhenRequestedBytesNegative() {
//given
var is = new ByteBufferInputStream();

//when + then
assertThrows(IllegalArgumentException.class, () -> is.readNBytes(-1));
}

@Test
void shouldReadUpToNBytes() throws IOException {
//given
var count = 16;
byte[] bytes = nextBytes(count);
var is = new ByteBufferInputStream();
toBuffers(bytes, false).forEach(is::add);

//when
byte[] actual = is.readNBytes(count + 1);

//then
assertArrayEquals(bytes, actual);
}

@Test
void shouldSupportMark() {
//given + when + then
Expand Down

0 comments on commit d00a56f

Please sign in to comment.