Skip to content

Commit

Permalink
adjust behavior of BigByteArrayOutputStream in a specific situation
Browse files Browse the repository at this point in the history
  • Loading branch information
nck-mlcnv committed Sep 22, 2023
1 parent 696a0b6 commit e2baf7d
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,6 @@ public int readNBytes(byte[] b, int off, int len) throws IOException {
return copyLength1 + copyLength2;
}

// TODO: skip and skipn

@Override
public byte[] readAllBytes() throws IOException {
throw new IOException("Reading all bytes from a BigByteArrayInputStream is prohibited because it might exceed the array capacity");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ public BigByteArrayOutputStream(long bufferSize) {
if (bufferSize <= ARRAY_SIZE_LIMIT) {
baosList = new ArrayList<>(1);
baosList.add(new ByteArrayOutputStream((int) bufferSize));
} else { // TODO: fix the line below
final var requiredBaoss = (int) (bufferSize / ARRAY_SIZE_LIMIT) + 1; // this might create a fully sized, but empty baos at the end if the buffer size is a multiple of ARRAY_SIZE_LIMIT
} else {
final var requiredBaoss = (int) ((bufferSize - 1) / ARRAY_SIZE_LIMIT) + 1; // -1 to prevent creating a fully sized, but empty baos at the end if the buffer size is a multiple of ARRAY_SIZE_LIMIT
baosList = new ArrayList<>(requiredBaoss);
IntStream.range(0, requiredBaoss).forEachOrdered(i -> baosList.add(new ByteArrayOutputStream(ARRAY_SIZE_LIMIT)));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,16 @@ class BigByteArrayOutputStreamTest {
final static Random rng = new Random(0);

public static List<Arguments> data() {
final var maxSize = Integer.MAX_VALUE - 8;
final long maxSize = Integer.MAX_VALUE - 8;

final Supplier<byte[][]> sup1 = () -> getBigRandomBuffer(10, maxSize);
final Supplier<byte[][]> sup2 = () -> getBigRandomBuffer((long) maxSize * 2L, maxSize);
final Supplier<byte[][]> sup1 = () -> getBigRandomBuffer(10L, (int) maxSize);
final Supplier<byte[][]> sup2 = () -> getBigRandomBuffer(maxSize * 2L, (int) maxSize);

return List.of(
Arguments.of(Named.of(String.valueOf(10), sup1), 10, new int[] { 10 }),
Arguments.of(Named.of(String.valueOf(10), sup1), maxSize * 2L, new int[] { maxSize, maxSize, maxSize }), // small data, high initial capacity
Arguments.of(Named.of(String.valueOf(maxSize * 2L), sup2), maxSize * 2L, new int[] { maxSize, maxSize, maxSize }),
Arguments.of(Named.of(String.valueOf(maxSize * 2L), sup2), 0, new int[] { maxSize, maxSize })
Arguments.of(Named.of(String.valueOf(10), sup1), maxSize * 2L, new int[] {(int) maxSize, (int) maxSize}), // small data, high initial capacity
Arguments.of(Named.of(String.valueOf(maxSize * 2L), sup2), maxSize * 2L, new int[] {(int) maxSize, (int) maxSize}),
Arguments.of(Named.of(String.valueOf(maxSize * 2L), sup2), 0, new int[] {(int) maxSize, (int) maxSize})
);
}

Expand Down

0 comments on commit e2baf7d

Please sign in to comment.