@Test public void testCalculateNewCapacity() { testCalculateNewCapacity(true); testCalculateNewCapacity(false); }
@Override public int calculateNewCapacity(int minNewCapacity, int maxCapacity) { if (minNewCapacity < 0) { throw new IllegalArgumentException("minNewCapacity: " + minNewCapacity + " (expected: 0+)"); } if (minNewCapacity > maxCapacity) { throw new IllegalArgumentException(String.format( "minNewCapacity: %d (expected: not greater than maxCapacity(%d)", minNewCapacity, maxCapacity)); } final int threshold = CALCULATE_THRESHOLD; if (minNewCapacity == threshold) { return threshold; } if (minNewCapacity > threshold) { int newCapacity = minNewCapacity / threshold * threshold; if (newCapacity > maxCapacity - threshold) { newCapacity = maxCapacity; } else { newCapacity += threshold; } return newCapacity; } int newCapacity = 64; while (newCapacity < minNewCapacity) { newCapacity <<= 1; } return Math.min(newCapacity, maxCapacity); }
AbstractByteBufAllocator implements ByteBufAllocator { @Override public int calculateNewCapacity(int minNewCapacity, int maxCapacity) { if (minNewCapacity < 0) { throw new IllegalArgumentException("minNewCapacity: " + minNewCapacity + " (expected: 0+)"); } if (minNewCapacity > maxCapacity) { throw new IllegalArgumentException(String.format( "minNewCapacity: %d (expected: not greater than maxCapacity(%d)", minNewCapacity, maxCapacity)); } final int threshold = CALCULATE_THRESHOLD; if (minNewCapacity == threshold) { return threshold; } if (minNewCapacity > threshold) { int newCapacity = minNewCapacity / threshold * threshold; if (newCapacity > maxCapacity - threshold) { newCapacity = maxCapacity; } else { newCapacity += threshold; } return newCapacity; } int newCapacity = 64; while (newCapacity < minNewCapacity) { newCapacity <<= 1; } return Math.min(newCapacity, maxCapacity); } }
AbstractByteBufAllocator implements ByteBufAllocator { @Override public int calculateNewCapacity(int minNewCapacity, int maxCapacity) { if (minNewCapacity < 0) { throw new IllegalArgumentException("minNewCapacity: " + minNewCapacity + " (expected: 0+)"); } if (minNewCapacity > maxCapacity) { throw new IllegalArgumentException(String.format( "minNewCapacity: %d (expected: not greater than maxCapacity(%d)", minNewCapacity, maxCapacity)); } final int threshold = CALCULATE_THRESHOLD; if (minNewCapacity == threshold) { return threshold; } if (minNewCapacity > threshold) { int newCapacity = minNewCapacity / threshold * threshold; if (newCapacity > maxCapacity - threshold) { newCapacity = maxCapacity; } else { newCapacity += threshold; } return newCapacity; } int newCapacity = 64; while (newCapacity < minNewCapacity) { newCapacity <<= 1; } return Math.min(newCapacity, maxCapacity); } protected  AbstractByteBufAllocator(); protected  AbstractByteBufAllocator(boolean preferDirect); }
AbstractByteBufAllocator implements ByteBufAllocator { @Override public int calculateNewCapacity(int minNewCapacity, int maxCapacity) { if (minNewCapacity < 0) { throw new IllegalArgumentException("minNewCapacity: " + minNewCapacity + " (expected: 0+)"); } if (minNewCapacity > maxCapacity) { throw new IllegalArgumentException(String.format( "minNewCapacity: %d (expected: not greater than maxCapacity(%d)", minNewCapacity, maxCapacity)); } final int threshold = CALCULATE_THRESHOLD; if (minNewCapacity == threshold) { return threshold; } if (minNewCapacity > threshold) { int newCapacity = minNewCapacity / threshold * threshold; if (newCapacity > maxCapacity - threshold) { newCapacity = maxCapacity; } else { newCapacity += threshold; } return newCapacity; } int newCapacity = 64; while (newCapacity < minNewCapacity) { newCapacity <<= 1; } return Math.min(newCapacity, maxCapacity); } protected  AbstractByteBufAllocator(); protected  AbstractByteBufAllocator(boolean preferDirect); @Override ByteBuf buffer(); @Override ByteBuf buffer(int initialCapacity); @Override ByteBuf buffer(int initialCapacity, int maxCapacity); @Override ByteBuf ioBuffer(); @Override ByteBuf ioBuffer(int initialCapacity); @Override ByteBuf ioBuffer(int initialCapacity, int maxCapacity); @Override ByteBuf heapBuffer(); @Override ByteBuf heapBuffer(int initialCapacity); @Override ByteBuf heapBuffer(int initialCapacity, int maxCapacity); @Override ByteBuf directBuffer(); @Override ByteBuf directBuffer(int initialCapacity); @Override ByteBuf directBuffer(int initialCapacity, int maxCapacity); @Override CompositeByteBuf compositeBuffer(); @Override CompositeByteBuf compositeBuffer(int maxNumComponents); @Override CompositeByteBuf compositeHeapBuffer(); @Override CompositeByteBuf compositeHeapBuffer(int maxNumComponents); @Override CompositeByteBuf compositeDirectBuffer(); @Override CompositeByteBuf compositeDirectBuffer(int maxNumComponents); @Override String toString(); @Override int calculateNewCapacity(int minNewCapacity, int maxCapacity); }
AbstractByteBufAllocator implements ByteBufAllocator { @Override public int calculateNewCapacity(int minNewCapacity, int maxCapacity) { if (minNewCapacity < 0) { throw new IllegalArgumentException("minNewCapacity: " + minNewCapacity + " (expected: 0+)"); } if (minNewCapacity > maxCapacity) { throw new IllegalArgumentException(String.format( "minNewCapacity: %d (expected: not greater than maxCapacity(%d)", minNewCapacity, maxCapacity)); } final int threshold = CALCULATE_THRESHOLD; if (minNewCapacity == threshold) { return threshold; } if (minNewCapacity > threshold) { int newCapacity = minNewCapacity / threshold * threshold; if (newCapacity > maxCapacity - threshold) { newCapacity = maxCapacity; } else { newCapacity += threshold; } return newCapacity; } int newCapacity = 64; while (newCapacity < minNewCapacity) { newCapacity <<= 1; } return Math.min(newCapacity, maxCapacity); } protected  AbstractByteBufAllocator(); protected  AbstractByteBufAllocator(boolean preferDirect); @Override ByteBuf buffer(); @Override ByteBuf buffer(int initialCapacity); @Override ByteBuf buffer(int initialCapacity, int maxCapacity); @Override ByteBuf ioBuffer(); @Override ByteBuf ioBuffer(int initialCapacity); @Override ByteBuf ioBuffer(int initialCapacity, int maxCapacity); @Override ByteBuf heapBuffer(); @Override ByteBuf heapBuffer(int initialCapacity); @Override ByteBuf heapBuffer(int initialCapacity, int maxCapacity); @Override ByteBuf directBuffer(); @Override ByteBuf directBuffer(int initialCapacity); @Override ByteBuf directBuffer(int initialCapacity, int maxCapacity); @Override CompositeByteBuf compositeBuffer(); @Override CompositeByteBuf compositeBuffer(int maxNumComponents); @Override CompositeByteBuf compositeHeapBuffer(); @Override CompositeByteBuf compositeHeapBuffer(int maxNumComponents); @Override CompositeByteBuf compositeDirectBuffer(); @Override CompositeByteBuf compositeDirectBuffer(int maxNumComponents); @Override String toString(); @Override int calculateNewCapacity(int minNewCapacity, int maxCapacity);  }