CompositeMetadataCodec { public static ByteBuf[] decodeMimeAndContentBuffersSlices( ByteBuf compositeMetadata, int entryIndex, boolean retainSlices) { compositeMetadata.markReaderIndex(); compositeMetadata.readerIndex(entryIndex); if (compositeMetadata.isReadable()) { ByteBuf mime; int ridx = compositeMetadata.readerIndex(); byte mimeIdOrLength = compositeMetadata.readByte(); if ((mimeIdOrLength & STREAM_METADATA_KNOWN_MASK) == STREAM_METADATA_KNOWN_MASK) { mime = retainSlices ? compositeMetadata.retainedSlice(ridx, 1) : compositeMetadata.slice(ridx, 1); } else { int mimeLength = Byte.toUnsignedInt(mimeIdOrLength) + 1; if (compositeMetadata.isReadable( mimeLength)) { mime = retainSlices ? compositeMetadata.retainedSlice(ridx, mimeLength + 1) : compositeMetadata.slice(ridx, mimeLength + 1); compositeMetadata.skipBytes(mimeLength); } else { compositeMetadata.resetReaderIndex(); throw new IllegalStateException("metadata is malformed"); } } if (compositeMetadata.isReadable(3)) { final int metadataLength = compositeMetadata.readUnsignedMedium(); if (compositeMetadata.isReadable(metadataLength)) { ByteBuf metadata = retainSlices ? compositeMetadata.readRetainedSlice(metadataLength) : compositeMetadata.readSlice(metadataLength); compositeMetadata.resetReaderIndex(); return new ByteBuf[] {mime, metadata}; } else { compositeMetadata.resetReaderIndex(); throw new IllegalStateException("metadata is malformed"); } } else { compositeMetadata.resetReaderIndex(); throw new IllegalStateException("metadata is malformed"); } } compositeMetadata.resetReaderIndex(); throw new IllegalArgumentException( String.format("entry index %d is larger than buffer size", entryIndex)); } 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 decodeEntryAtEndOfBuffer() { ByteBuf fakeEntry = Unpooled.buffer(); assertThatIllegalArgumentException() .isThrownBy(() -> decodeMimeAndContentBuffersSlices(fakeEntry, 0, false)); }
@Test void decodeEntryHasNoContentLength() { ByteBuf fakeEntry = Unpooled.buffer(); fakeEntry.writeByte(0); fakeEntry.writeCharSequence("w", CharsetUtil.US_ASCII); assertThatIllegalStateException() .isThrownBy(() -> decodeMimeAndContentBuffersSlices(fakeEntry, 0, false)); }
@Test void decodeEntryTooShortForContentLength() { ByteBuf fakeEntry = Unpooled.buffer(); fakeEntry.writeByte(1); fakeEntry.writeCharSequence("w", CharsetUtil.US_ASCII); NumberUtils.encodeUnsignedMedium(fakeEntry, 456); fakeEntry.writeChar('w'); assertThatIllegalStateException() .isThrownBy(() -> decodeMimeAndContentBuffersSlices(fakeEntry, 0, false)); }
@Test void decodeEntryTooShortForMimeLength() { ByteBuf fakeEntry = Unpooled.buffer(); fakeEntry.writeByte(120); assertThatIllegalStateException() .isThrownBy(() -> decodeMimeAndContentBuffersSlices(fakeEntry, 0, false)); }