CompositeMetadataCodec { static ByteBuf encodeMetadataHeader( ByteBufAllocator allocator, String customMime, int metadataLength) { ByteBuf metadataHeader = allocator.buffer(4 + customMime.length()); int writerIndexInitial = metadataHeader.writerIndex(); metadataHeader.writerIndex(writerIndexInitial + 1); int customMimeLength = ByteBufUtil.writeUtf8(metadataHeader, customMime); if (!ByteBufUtil.isText( metadataHeader, metadataHeader.readerIndex() + 1, customMimeLength, CharsetUtil.US_ASCII)) { metadataHeader.release(); throw new IllegalArgumentException("custom mime type must be US_ASCII characters only"); } if (customMimeLength < 1 || customMimeLength > 128) { metadataHeader.release(); throw new IllegalArgumentException( "custom mime type must have a strictly positive length that fits on 7 unsigned bits, ie 1-128"); } metadataHeader.markWriterIndex(); metadataHeader.writerIndex(writerIndexInitial); metadataHeader.writeByte(customMimeLength - 1); metadataHeader.resetWriterIndex(); NumberUtils.encodeUnsignedMedium(metadataHeader, metadataLength); return metadataHeader; } private  CompositeMetadataCodec(); static int computeNextEntryIndex(
      int currentEntryIndex, ByteBuf headerSlice, ByteBuf contentSlice); static ByteBuf[] decodeMimeAndContentBuffersSlices(
      ByteBuf compositeMetadata, int entryIndex, boolean retainSlices); static byte decodeMimeIdFromMimeBuffer(ByteBuf mimeBuffer); @Nullable static CharSequence decodeMimeTypeFromMimeBuffer(ByteBuf flyweightMimeBuffer); static void encodeAndAddMetadata(
      CompositeByteBuf compositeMetaData,
      ByteBufAllocator allocator,
      String customMimeType,
      ByteBuf metadata); static void encodeAndAddMetadata(
      CompositeByteBuf compositeMetaData,
      ByteBufAllocator allocator,
      WellKnownMimeType knownMimeType,
      ByteBuf metadata); static void encodeAndAddMetadataWithCompression(
      CompositeByteBuf compositeMetaData,
      ByteBufAllocator allocator,
      String mimeType,
      ByteBuf metadata); static boolean hasEntry(ByteBuf compositeMetadata, int entryIndex); static boolean isWellKnownMimeType(ByteBuf header);  }
@Test void customMimeHeaderLatin1_encodingFails() { String mimeNotAscii = "mime/typé"; assertThatIllegalArgumentException() .isThrownBy( () -> CompositeMetadataCodec.encodeMetadataHeader( ByteBufAllocator.DEFAULT, mimeNotAscii, 0)) .withMessage("custom mime type must be US_ASCII characters only"); }
@Test void customMimeHeaderLength0_encodingFails() { assertThatIllegalArgumentException() .isThrownBy( () -> CompositeMetadataCodec.encodeMetadataHeader(ByteBufAllocator.DEFAULT, "", 0)) .withMessage( "custom mime type must have a strictly positive length that fits on 7 unsigned bits, ie 1-128"); }
@Test void customMimeHeaderLength129_encodingFails() { StringBuilder builder = new StringBuilder(129); for (int i = 0; i < 129; i++) { builder.append('a'); } assertThatIllegalArgumentException() .isThrownBy( () -> CompositeMetadataCodec.encodeMetadataHeader( ByteBufAllocator.DEFAULT, builder.toString(), 0)) .withMessage( "custom mime type must have a strictly positive length that fits on 7 unsigned bits, ie 1-128"); }
@Test void customMimeHeaderUtf8_encodingFails() { String mimeNotAscii = "mime/tyࠒe"; assertThatIllegalArgumentException() .isThrownBy( () -> CompositeMetadataCodec.encodeMetadataHeader( ByteBufAllocator.DEFAULT, mimeNotAscii, 0)) .withMessage("custom mime type must be US_ASCII characters only"); }
@Test void encodeCustomHeaderAsciiCheckSkipsFirstByte() { final ByteBuf badBuf = Unpooled.copiedBuffer("é00000000000", CharsetUtil.UTF_8); badBuf.writerIndex(0); assertThat(badBuf.readerIndex()).isZero(); ByteBufAllocator allocator = new AbstractByteBufAllocator() { @Override public boolean isDirectBufferPooled() { return false; } @Override protected ByteBuf newHeapBuffer(int initialCapacity, int maxCapacity) { return badBuf; } @Override protected ByteBuf newDirectBuffer(int initialCapacity, int maxCapacity) { return badBuf; } }; assertThatCode(() -> CompositeMetadataCodec.encodeMetadataHeader(allocator, "custom/type", 0)) .doesNotThrowAnyException(); assertThat(badBuf.readByte()).isEqualTo((byte) 10); assertThat(badBuf.readCharSequence(11, CharsetUtil.UTF_8)).hasToString("custom/type"); assertThat(badBuf.readUnsignedMedium()).isEqualTo(0); }