@Override
        public RecoverResult next() {
            // If there is no more data to read, return null.
            if (!hasNext()) {
                return null;
            }

            if (!dataBuffer.isReadable()) {
                loadNextBuffer(true);
            }

            // Try to read next object.
            tryReadAhead();

            ByteBuf recordHeaderBuf = dataBuffer.readBytes(RECORD_HEADER_SIZE);
            RecordHeader header = RecordHeader.unmarshal(recordHeaderBuf);

            if (header.getRecordHeaderCRC() != WALUtil.crc32(recordHeaderBuf, RECORD_HEADER_WITHOUT_CRC_SIZE)) {
                recordHeaderBuf.release();
                throw new IllegalStateException("Record header crc check failed.");
            }
            recordHeaderBuf.release();

            if (header.getMagicCode() != RecordHeader.RECORD_HEADER_MAGIC_CODE) {
                throw new IllegalStateException("Invalid magic code in record header.");
            }

            int length = header.getRecordBodyLength();

            ByteBuf recordBuf = ByteBufAlloc.byteBuffer(length);
            if (dataBuffer.readableBytes() < length) {
                // Read the remain data and release the buffer.
                dataBuffer.readBytes(recordBuf, dataBuffer.readableBytes());
                dataBuffer.release();

                // Read from next buffer.
                if (!hasNext()) {
                    throw new IllegalStateException("[Bug] There is a record part but no more data to read.");
                }
                loadNextBuffer(false);
                dataBuffer.readBytes(recordBuf, length - recordBuf.readableBytes());
            } else {
                dataBuffer.readBytes(recordBuf, length);
            }

            if (!dataBuffer.isReadable()) {
                dataBuffer.release();
            }

            if (header.getRecordBodyCRC() != WALUtil.crc32(recordBuf)) {
                recordBuf.release();
                throw new IllegalStateException("Record body crc check failed.");
            }

            return new RecoverResultImpl(recordBuf, header.getRecordBodyOffset() - RECORD_HEADER_SIZE);
        }
Get the latest trim offset from the newest object.
s3stream/src/main/java/com/automq/stream/s3/wal/impl/object/ObjectWALService.java