@Test public void testWriteArgsSecondArgWriteFails() { ByteBuf allocatedByteBuf1 = Unpooled.buffer(TFrame.FRAME_SIZE_LENGTH); ByteBuf allocatedByteBuf2 = Mockito.mock(ByteBuf.class); when(allocatedByteBuf2.release()).thenThrow(new RuntimeException("Can't release")); ByteBuf allocatedByteBuf3 = Unpooled.buffer(TFrame.FRAME_SIZE_LENGTH); ByteBufAllocator allocator = Mockito.mock(ByteBufAllocator.class); when(allocator.buffer(TFrame.FRAME_SIZE_LENGTH)) .thenReturn(allocatedByteBuf1) .thenReturn(allocatedByteBuf2) .thenReturn(allocatedByteBuf3); ByteBuf arg1 = Unpooled.wrappedBuffer("arg1".getBytes()); ByteBuf arg2 = Unpooled.wrappedBuffer("arg3".getBytes()); ByteBuf arg3 = Mockito.mock(ByteBuf.class); when(arg3.readableBytes()).thenReturn(10); when(arg3.readSlice(anyInt())).thenThrow(new RuntimeException("Can't read")); List<ByteBuf> args = new ArrayList<>(); args.add(arg1); args.add(arg2); args.add(arg3); try { CodecUtils.writeArgs(allocator, Unpooled.wrappedBuffer("header".getBytes()),args); fail(); } catch (Exception e) { assertEquals("Can't read", e.getMessage()); } verify(allocator, times(3)).buffer(TFrame.FRAME_SIZE_LENGTH); assertEquals(0, allocatedByteBuf1.refCnt()); assertEquals(0, allocatedByteBuf2.refCnt()); assertEquals(0, allocatedByteBuf3.refCnt()); }
public static @NotNull ByteBuf writeArgs( @NotNull ByteBufAllocator allocator, @NotNull ByteBuf header, @NotNull List<ByteBuf> args ) { int writableBytes = TFrame.MAX_FRAME_PAYLOAD_LENGTH - header.readableBytes(); List<ByteBuf> allocatedBufs = new ArrayList<>(7); List<ByteBuf> bufs = new ArrayList<>(7); bufs.add(header); boolean release = true; try { while (!args.isEmpty()) { ByteBuf arg = args.get(0); int len = writeArg(allocator, arg, writableBytes, bufs, allocatedBufs); writableBytes -= len; if (writableBytes <= TFrame.FRAME_SIZE_LENGTH) { break; } if (arg.readableBytes() == 0) { args.remove(0); } } CompositeByteBuf comp = allocator.compositeBuffer(); comp.addComponents(bufs); comp.writerIndex(TFrame.MAX_FRAME_PAYLOAD_LENGTH - writableBytes); release = false; return comp; } finally { if (release) { for (ByteBuf buf : allocatedBufs) { if (buf != null) { try { buf.release(); } catch (Exception e) { logger.warn("Failed to release", e); } } } } } }
CodecUtils { public static @NotNull ByteBuf writeArgs( @NotNull ByteBufAllocator allocator, @NotNull ByteBuf header, @NotNull List<ByteBuf> args ) { int writableBytes = TFrame.MAX_FRAME_PAYLOAD_LENGTH - header.readableBytes(); List<ByteBuf> allocatedBufs = new ArrayList<>(7); List<ByteBuf> bufs = new ArrayList<>(7); bufs.add(header); boolean release = true; try { while (!args.isEmpty()) { ByteBuf arg = args.get(0); int len = writeArg(allocator, arg, writableBytes, bufs, allocatedBufs); writableBytes -= len; if (writableBytes <= TFrame.FRAME_SIZE_LENGTH) { break; } if (arg.readableBytes() == 0) { args.remove(0); } } CompositeByteBuf comp = allocator.compositeBuffer(); comp.addComponents(bufs); comp.writerIndex(TFrame.MAX_FRAME_PAYLOAD_LENGTH - writableBytes); release = false; return comp; } finally { if (release) { for (ByteBuf buf : allocatedBufs) { if (buf != null) { try { buf.release(); } catch (Exception e) { logger.warn("Failed to release", e); } } } } } } }
CodecUtils { public static @NotNull ByteBuf writeArgs( @NotNull ByteBufAllocator allocator, @NotNull ByteBuf header, @NotNull List<ByteBuf> args ) { int writableBytes = TFrame.MAX_FRAME_PAYLOAD_LENGTH - header.readableBytes(); List<ByteBuf> allocatedBufs = new ArrayList<>(7); List<ByteBuf> bufs = new ArrayList<>(7); bufs.add(header); boolean release = true; try { while (!args.isEmpty()) { ByteBuf arg = args.get(0); int len = writeArg(allocator, arg, writableBytes, bufs, allocatedBufs); writableBytes -= len; if (writableBytes <= TFrame.FRAME_SIZE_LENGTH) { break; } if (arg.readableBytes() == 0) { args.remove(0); } } CompositeByteBuf comp = allocator.compositeBuffer(); comp.addComponents(bufs); comp.writerIndex(TFrame.MAX_FRAME_PAYLOAD_LENGTH - writableBytes); release = false; return comp; } finally { if (release) { for (ByteBuf buf : allocatedBufs) { if (buf != null) { try { buf.release(); } catch (Exception e) { logger.warn("Failed to release", e); } } } } } } private  CodecUtils(); }
CodecUtils { public static @NotNull ByteBuf writeArgs( @NotNull ByteBufAllocator allocator, @NotNull ByteBuf header, @NotNull List<ByteBuf> args ) { int writableBytes = TFrame.MAX_FRAME_PAYLOAD_LENGTH - header.readableBytes(); List<ByteBuf> allocatedBufs = new ArrayList<>(7); List<ByteBuf> bufs = new ArrayList<>(7); bufs.add(header); boolean release = true; try { while (!args.isEmpty()) { ByteBuf arg = args.get(0); int len = writeArg(allocator, arg, writableBytes, bufs, allocatedBufs); writableBytes -= len; if (writableBytes <= TFrame.FRAME_SIZE_LENGTH) { break; } if (arg.readableBytes() == 0) { args.remove(0); } } CompositeByteBuf comp = allocator.compositeBuffer(); comp.addComponents(bufs); comp.writerIndex(TFrame.MAX_FRAME_PAYLOAD_LENGTH - writableBytes); release = false; return comp; } finally { if (release) { for (ByteBuf buf : allocatedBufs) { if (buf != null) { try { buf.release(); } catch (Exception e) { logger.warn("Failed to release", e); } } } } } } private  CodecUtils(); static int decodeChecksum(@NotNull ChecksumType checksumType, ByteBuf buffer); static void encodeChecksum(int checksum, @NotNull ChecksumType checksumType, ByteBuf buffer); static @NotNull String decodeString(@NotNull ByteBuf buffer); static void encodeString(@NotNull String value, @NotNull ByteBuf buffer); static @NotNull String decodeSmallString(@NotNull ByteBuf buffer); static void encodeSmallString(@NotNull String value, @NotNull ByteBuf buffer); static @NotNull Map<String, String> decodeHeaders(@NotNull ByteBuf buffer); static void encodeHeaders(@NotNull Map<String, String> headers, @NotNull ByteBuf buffer); static @NotNull Map<String, String> decodeSmallHeaders(@NotNull ByteBuf buffer); static void encodeSmallHeaders(@NotNull Map<String, String> headers, @NotNull ByteBuf buffer); static @NotNull Trace decodeTrace(@NotNull ByteBuf buffer); static void encodeTrace(@NotNull Trace trace, @NotNull ByteBuf buffer); @Deprecated static int writeArg(
        @NotNull ByteBufAllocator allocator,
        @NotNull ByteBuf arg,
        int writableBytes,
        @NotNull List<ByteBuf> bufs
    ); static int writeArg(
        @NotNull ByteBufAllocator allocator,
        @NotNull ByteBuf arg,
        int writableBytes,
        @NotNull List<ByteBuf> bufs,
        @NotNull List<ByteBuf> allocatedBufs
    ); static @NotNull ByteBuf writeArgs(
        @NotNull ByteBufAllocator allocator,
        @NotNull ByteBuf header,
        @NotNull List<ByteBuf> args
    ); static @NotNull ByteBuf writeArgCopy(
        ByteBufAllocator allocator,
        @NotNull ByteBuf payload,
        @NotNull ByteBuf arg,
        int writableBytes
    ); static @NotNull ByteBuf writeArgsCopy(
        @NotNull ByteBufAllocator allocator,
        @NotNull ByteBuf header,
        @NotNull List<ByteBuf> args
    ); @SuppressWarnings("ObjectEquality") static @NotNull ByteBuf compose(@NotNull ByteBuf first, @NotNull ByteBuf second); static @Nullable ByteBuf readArg(@NotNull ByteBuf buffer); static void readArgs(@NotNull List<ByteBuf> args, @NotNull ByteBuf buffer); }
CodecUtils { public static @NotNull ByteBuf writeArgs( @NotNull ByteBufAllocator allocator, @NotNull ByteBuf header, @NotNull List<ByteBuf> args ) { int writableBytes = TFrame.MAX_FRAME_PAYLOAD_LENGTH - header.readableBytes(); List<ByteBuf> allocatedBufs = new ArrayList<>(7); List<ByteBuf> bufs = new ArrayList<>(7); bufs.add(header); boolean release = true; try { while (!args.isEmpty()) { ByteBuf arg = args.get(0); int len = writeArg(allocator, arg, writableBytes, bufs, allocatedBufs); writableBytes -= len; if (writableBytes <= TFrame.FRAME_SIZE_LENGTH) { break; } if (arg.readableBytes() == 0) { args.remove(0); } } CompositeByteBuf comp = allocator.compositeBuffer(); comp.addComponents(bufs); comp.writerIndex(TFrame.MAX_FRAME_PAYLOAD_LENGTH - writableBytes); release = false; return comp; } finally { if (release) { for (ByteBuf buf : allocatedBufs) { if (buf != null) { try { buf.release(); } catch (Exception e) { logger.warn("Failed to release", e); } } } } } } private  CodecUtils(); static int decodeChecksum(@NotNull ChecksumType checksumType, ByteBuf buffer); static void encodeChecksum(int checksum, @NotNull ChecksumType checksumType, ByteBuf buffer); static @NotNull String decodeString(@NotNull ByteBuf buffer); static void encodeString(@NotNull String value, @NotNull ByteBuf buffer); static @NotNull String decodeSmallString(@NotNull ByteBuf buffer); static void encodeSmallString(@NotNull String value, @NotNull ByteBuf buffer); static @NotNull Map<String, String> decodeHeaders(@NotNull ByteBuf buffer); static void encodeHeaders(@NotNull Map<String, String> headers, @NotNull ByteBuf buffer); static @NotNull Map<String, String> decodeSmallHeaders(@NotNull ByteBuf buffer); static void encodeSmallHeaders(@NotNull Map<String, String> headers, @NotNull ByteBuf buffer); static @NotNull Trace decodeTrace(@NotNull ByteBuf buffer); static void encodeTrace(@NotNull Trace trace, @NotNull ByteBuf buffer); @Deprecated static int writeArg(
        @NotNull ByteBufAllocator allocator,
        @NotNull ByteBuf arg,
        int writableBytes,
        @NotNull List<ByteBuf> bufs
    ); static int writeArg(
        @NotNull ByteBufAllocator allocator,
        @NotNull ByteBuf arg,
        int writableBytes,
        @NotNull List<ByteBuf> bufs,
        @NotNull List<ByteBuf> allocatedBufs
    ); static @NotNull ByteBuf writeArgs(
        @NotNull ByteBufAllocator allocator,
        @NotNull ByteBuf header,
        @NotNull List<ByteBuf> args
    ); static @NotNull ByteBuf writeArgCopy(
        ByteBufAllocator allocator,
        @NotNull ByteBuf payload,
        @NotNull ByteBuf arg,
        int writableBytes
    ); static @NotNull ByteBuf writeArgsCopy(
        @NotNull ByteBufAllocator allocator,
        @NotNull ByteBuf header,
        @NotNull List<ByteBuf> args
    ); @SuppressWarnings("ObjectEquality") static @NotNull ByteBuf compose(@NotNull ByteBuf first, @NotNull ByteBuf second); static @Nullable ByteBuf readArg(@NotNull ByteBuf buffer); static void readArgs(@NotNull List<ByteBuf> args, @NotNull ByteBuf buffer);  }