BinaryAbstractOutputStream extends BinaryAbstractStream implements BinaryOutputStream { protected static int capacity(int curCap, int reqCap) { int newCap; if (reqCap < MIN_CAP) newCap = MIN_CAP; else if (reqCap > MAX_ARRAY_SIZE) throw new IllegalArgumentException("Required capacity exceeds allowed. Required:" + reqCap); else { newCap = Math.max(curCap, MIN_CAP); while (newCap < reqCap) { newCap = newCap << 1; if (newCap < 0) newCap = MAX_ARRAY_SIZE; } } return newCap; }  @Override void writeByte(byte val); @Override void writeByteArray(byte[] val); @Override void writeBoolean(boolean val); @Override void writeBooleanArray(boolean[] val); @Override void writeShort(short val); @Override void writeShortArray(short[] val); @Override void writeChar(char val); @Override void writeCharArray(char[] val); @Override void writeInt(int val); @Override void writeShort(int pos, short val); @Override void writeInt(int pos, int val); @Override void writeIntArray(int[] val); @Override void writeFloat(float val); @Override void writeFloatArray(float[] val); @Override void writeLong(long val); @Override void writeLongArray(long[] val); @Override void writeDouble(double val); @Override void writeDoubleArray(double[] val); @Override void write(byte[] arr, int off, int len); @Override void write(long addr, int cnt); @Override void position(int pos); @Override long offheapPointer(); @Override long rawOffheapPointer(); @Override void unsafeEnsure(int cap); @Override void unsafePosition(int pos); @Override void unsafeWriteBoolean(boolean val); @Override void unsafeWriteFloat(float val); @Override void unsafeWriteDouble(double val);  }
@Test public void testCapacity() { assertEquals(256, BinaryAbstractOutputStream.capacity(0, 1)); assertEquals(256, BinaryAbstractOutputStream.capacity(256, 1)); assertEquals(256, BinaryAbstractOutputStream.capacity(256, 256)); assertEquals(512, BinaryAbstractOutputStream.capacity(256, 257)); assertEquals(512, BinaryAbstractOutputStream.capacity(512, 256)); assertEquals(1024, BinaryAbstractOutputStream.capacity(512, 513)); assertEquals(2048, BinaryAbstractOutputStream.capacity(1024, 1025)); assertEquals(4096, BinaryAbstractOutputStream.capacity(2048, 2049)); assertEquals(8192, BinaryAbstractOutputStream.capacity(4096, 4097)); assertEquals(16384, BinaryAbstractOutputStream.capacity(8192, 8193)); assertEquals(32768, BinaryAbstractOutputStream.capacity(16384, 16385)); assertEquals(65536, BinaryAbstractOutputStream.capacity(32768, 32769)); assertEquals(131072, BinaryAbstractOutputStream.capacity(65536, 65537)); assertEquals(262144, BinaryAbstractOutputStream.capacity(131072, 131073)); assertEquals(524288, BinaryAbstractOutputStream.capacity(262144, 262145)); assertEquals(1048576, BinaryAbstractOutputStream.capacity(524288, 524289)); assertEquals(2097152, BinaryAbstractOutputStream.capacity(1048576, 1048577)); assertEquals(4194304, BinaryAbstractOutputStream.capacity(2097152, 2097153)); assertEquals(8388608, BinaryAbstractOutputStream.capacity(4194304, 4194305)); assertEquals(16777216, BinaryAbstractOutputStream.capacity(8388608, 8388609)); assertEquals(33554432, BinaryAbstractOutputStream.capacity(16777216, 16777217)); assertEquals(67108864, BinaryAbstractOutputStream.capacity(33554432, 33554433)); assertEquals(134217728, BinaryAbstractOutputStream.capacity(67108864, 67108865)); assertEquals(268435456, BinaryAbstractOutputStream.capacity(134217728, 134217729)); assertEquals(536870912, BinaryAbstractOutputStream.capacity(268435456, 268435457)); assertEquals(1073741824, BinaryAbstractOutputStream.capacity(536870912, 536870913)); final int MAX_SIZE = BinaryAbstractOutputStream.MAX_ARRAY_SIZE; assertEquals(MAX_SIZE, BinaryAbstractOutputStream.capacity(1073741824, 1073741825)); assertEquals(MAX_SIZE, BinaryAbstractOutputStream.capacity(0, 1073741825)); assertEquals(MAX_SIZE, BinaryAbstractOutputStream.capacity(1073741824, 1500000000)); assertEquals(MAX_SIZE, BinaryAbstractOutputStream.capacity(1073741824, 2000000000)); assertEquals(MAX_SIZE, BinaryAbstractOutputStream.capacity(1073741824, Integer.MAX_VALUE - 9)); assertEquals(MAX_SIZE, BinaryAbstractOutputStream.capacity(1073741824, Integer.MAX_VALUE - 8)); try { assertEquals(MAX_SIZE, BinaryAbstractOutputStream.capacity(0, Integer.MAX_VALUE - 7)); fail(); } catch (IllegalArgumentException ignored) { } }