SharedByteArrayStream { @VisibleForTesting int computeResize(int available, int needed, int length) { assert available < needed : "available (" + available + ") >= needed (" + needed + ")"; assert available < length : "available (" + available + ") >= length (" + length + ")"; long additional = 0; for (int i = 1; additional + available <= needed; i++) { additional = (long) length * ((1 << i) - 1); } int newLength = (int) Math.min(length + additional, MAX_BUFFER_SIZE); if (newLength - (length - available) - 1 < needed) { return -1; } else { return newLength; } } SharedByteArrayStream();  SharedByteArrayStream(int windowSize); @VisibleForTesting  SharedByteArrayStream(int windowSize, int bufferSize); byte[] getBufferedData();  }
@Test public void computeResizeExact() { int newLength = stream.computeResize(0, 3, 1); assertEquals("length is incorrect", 8, newLength); }
@Test public void computeResizeGrowsByPowersOf2() { int newLength = stream.computeResize(5, 100, 10); assertEquals("length is incorrect", 160, newLength); }
@Test public void computeResizeCapsAtMaxArraySize() { int newLength = stream.computeResize(0, 64, Integer.MAX_VALUE / 2 + 1); assertEquals("length is incorrect", Integer.MAX_VALUE - 8, newLength); }
@Test public void computeResizeNeededOverflow() { int newLength = stream.computeResize(512, Integer.MAX_VALUE, 1024); assertEquals("length is incorrect", -1, newLength); }
@Test public void computeResizeLengthOverflow() { int newLength = stream.computeResize(0, Integer.MAX_VALUE / 2 + 1, Integer.MAX_VALUE / 2); assertEquals("length is incorrect", -1, newLength); }