in java/src/main/java/com/epam/deltix/zstd/ZstdFrameDecompressor.java [795:841]
private int decodeRawLiterals(final ByteBuffer inputBase, final int inputAddress, final int inputLimit) {
int input = inputAddress;
final int type = (inputBase.get(input) >> 2) & 0b11;
final int literalSize;
switch (type) {
case 0:
case 2:
literalSize = (inputBase.get(input) & 0xFF) >>> 3;
input++;
break;
case 1:
literalSize = (inputBase.getShort(input) & 0xFFFF) >>> 4;
input += 2;
break;
case 3:
// read 3 little-endian bytes
final int header = ((inputBase.get(input) & 0xFF) |
((inputBase.getShort(input + 1) & 0xFFFF) << 8));
literalSize = header >>> 4;
input += 3;
break;
default:
throw fail(input, "Invalid raw literals header encoding type");
}
verify(input + literalSize <= inputLimit, input, "Not enough input bytes");
// Set literals pointer to [input, literalSize], but only if we can copy 8 bytes at a time during sequence decoding
// Otherwise, copy literals into buffer that's big enough to guarantee that
if (literalSize > (inputLimit - input) - SIZE_OF_LONG) {
literalsBase = ByteBufferWrap(literals);
literalsAddress = 0;
literalsLimit = literalSize;
System.arraycopy(inputBase.array(), input, literalsBase.array(), literalsAddress, literalSize);
Arrays.fill(literals, literalSize, literalSize + SIZE_OF_LONG, (byte) 0);
} else {
literalsBase = inputBase;
literalsAddress = input;
literalsLimit = literalsAddress + literalSize;
}
input += literalSize;
return (int) (input - inputAddress);
}