FastByteArrayProvider { public byte[] getArray (final int length) { for (int i = 0; i < size; ++i) { if (length == arrays[i].length) { if (i > 0) { tmp = arrays[i]; arrays[i] = arrays[i - 1]; arrays[i - 1] = tmp; return tmp; } return arrays[0]; } } tmp = new byte[length]; if (size == capacity) --size; arrays[size] = tmp; ++size; return tmp; } FastByteArrayProvider(final int capacity); byte[] getArray(final int length); byte[][] getAll();  }
@Test public void testGetArray () { PROVIDER.getArray(1); PROVIDER.getArray(2); PROVIDER.getArray(3); PROVIDER.getArray(4); final byte[][] arrays = PROVIDER.getAll(); for (int i = 0; i < CAPACITY; ++i) assertEquals(arrays[i].length, i + 1); for (int i = 0; i < 3; ++i) PROVIDER.getArray(4); assert (arrays[0].length == 4); for (int i = 1; i < CAPACITY; ++i) assertEquals(arrays[i].length, i); PROVIDER.getArray(5); assertEquals(4, arrays[0].length); assertEquals(1, arrays[1].length); assertEquals(2, arrays[2].length); assertEquals(5, arrays[3].length); }