ByteBufferUtil { public static int slice(final ByteBuffer data, final int maxSliceSize, final Slice<ByteBuffer> slice) { ByteBuffer d = data; int remaining = 0; int sliceCount = 0; while ((remaining = d.remaining()) > 0) { sliceCount++; final byte[] bytes = new byte[maxSliceSize < remaining ? maxSliceSize : remaining]; final boolean goOn = slice.each(ByteBuffer.wrap(bytes), d.get(bytes).remaining() == 0); if (!goOn) { break; } d = d.slice(); } return sliceCount; } private  ByteBufferUtil(); static int slice(final ByteBuffer data, final int maxSliceSize,
            final Slice<ByteBuffer> slice); static int sliceIfRequired(final ByteBuffer data,
            final int maxSliceSize, final Slice<ByteBuffer> slice); static ByteBuffer merge(final ByteBuffer... dataArray);  }
@Test(expected = PushFailedException.class) public void testName() throws Exception { final String s = "1234567890abc"; final ByteBuffer inputData = ByteBuffer .wrap(s.getBytes(StandardCharsets.UTF_8)); ByteBufferUtil.slice(inputData, 1, (part, last) -> { if (last) { throw new PushFailedException(); } return !last; }); }
@Test public void testSlice() { { final String s = "1234567890abc"; final ByteBuffer inputData = ByteBuffer .wrap(s.getBytes(StandardCharsets.UTF_8)); StringBuilder builder = new StringBuilder(); final int totalSlices = ByteBufferUtil.slice(inputData, 1, (part, last) -> { builder.append( new String(part.array(), StandardCharsets.UTF_8)); return !last; }); assertEquals(s, builder.toString()); assertEquals(13, totalSlices); } { final String s = "1234567890abc"; final ByteBuffer inputData = ByteBuffer .wrap(s.getBytes(StandardCharsets.UTF_8)); StringBuilder builder = new StringBuilder(); final int totalSlices = ByteBufferUtil.slice(inputData, 2, (part, last) -> { builder.append( new String(part.array(), StandardCharsets.UTF_8)); return !last; }); assertEquals(s, builder.toString()); assertEquals(7, totalSlices); } { final String s = "1234567890abc"; final ByteBuffer inputData = ByteBuffer .wrap(s.getBytes(StandardCharsets.UTF_8)); StringBuilder builder = new StringBuilder(); final int totalSlices = ByteBufferUtil.slice(inputData, 3, (part, last) -> { builder.append( new String(part.array(), StandardCharsets.UTF_8)); return !last; }); assertEquals(s, builder.toString()); assertEquals(5, totalSlices); } { final String s = "1234567890abc"; final ByteBuffer inputData = ByteBuffer .wrap(s.getBytes(StandardCharsets.UTF_8)); StringBuilder builder = new StringBuilder(); final int totalSlices = ByteBufferUtil.slice(inputData, 4, (part, last) -> { builder.append( new String(part.array(), StandardCharsets.UTF_8)); return !last; }); assertEquals(s, builder.toString()); assertEquals(4, totalSlices); } { final String s = "1234567890abc"; final ByteBuffer inputData = ByteBuffer .wrap(s.getBytes(StandardCharsets.UTF_8)); StringBuilder builder = new StringBuilder(); final int totalSlices = ByteBufferUtil.slice(inputData, 5, (part, last) -> { builder.append( new String(part.array(), StandardCharsets.UTF_8)); return !last; }); assertEquals(s, builder.toString()); assertEquals(3, totalSlices); } { final String s = "1234567890abc"; final ByteBuffer inputData = ByteBuffer .wrap(s.getBytes(StandardCharsets.UTF_8)); StringBuilder builder = new StringBuilder(); final int totalSlices = ByteBufferUtil.slice(inputData, 6, (part, last) -> { builder.append( new String(part.array(), StandardCharsets.UTF_8)); return !last; }); assertEquals(s, builder.toString()); assertEquals(3, totalSlices); } { final String s = "1234567890abc"; final ByteBuffer inputData = ByteBuffer .wrap(s.getBytes(StandardCharsets.UTF_8)); StringBuilder builder = new StringBuilder(); final int totalSlices = ByteBufferUtil.slice(inputData, 7, (part, last) -> { builder.append( new String(part.array(), StandardCharsets.UTF_8)); return !last; }); assertEquals(s, builder.toString()); assertEquals(2, totalSlices); } { final String s = "1234567890abc"; final ByteBuffer inputData = ByteBuffer .wrap(s.getBytes(StandardCharsets.UTF_8)); StringBuilder builder = new StringBuilder(); final int totalSlices = ByteBufferUtil.slice(inputData, 20, (part, last) -> { builder.append( new String(part.array(), StandardCharsets.UTF_8)); return !last; }); assertEquals(s, builder.toString()); assertEquals(1, totalSlices); } }