override fun consume()

in plot-api/src/commonMain/kotlin/org/jetbrains/letsPlot/util/pngj/ChunkReader.kt [116:174]


    override fun consume(buf: ByteArray, offset: Int, len: Int): Int {
        var off = offset
        @Suppress("NAME_SHADOWING")
        var len = len
        if (len == 0) return 0
        if (len < 0) throw PngjException("negative length??")
        if (read == 0 && crcn == 0 && crcCheck) chunkRaw.updateCrc(
            chunkRaw.idbytes,
            0,
            4
        ) // initializes crc calculation with the Chunk ID
        var bytesForData = chunkRaw.len - read // bytesForData : bytes to be actually read from chunk data
        if (bytesForData > len) bytesForData = len
        // we want to call processData even for empty chunks (IEND:bytesForData=0) at
        // least once
        if (bytesForData > 0 || crcn == 0) {
            // in buffer mode we compute the CRC at the end
            if (crcCheck && mode != ChunkReaderMode.BUFFER && bytesForData > 0) chunkRaw.updateCrc(
                buf,
                off,
                bytesForData
            )
            if (mode == ChunkReaderMode.BUFFER) {
                // just copy the contents to the internal buffer
                if (!chunkRaw.data.contentEquals(buf) && bytesForData > 0) {
                    // If the buffer passed if the same as this one, we don't copy.
                    // The caller should know what he's doing
                    arraycopy(buf, off, chunkRaw.data!!, read, bytesForData)
                }
            } else if (mode == ChunkReaderMode.PROCESS) {
                processData(read, buf, off, bytesForData)
            } else {
                // mode == ChunkReaderMode.SKIP; nothing to do
            }
            read += bytesForData
            off += bytesForData
            len -= bytesForData
        }
        var crcRead = 0
        if (read == chunkRaw.len) { // data done - read crc?
            crcRead = 4 - crcn
            if (crcRead > len) crcRead = len
            if (crcRead > 0) {
                if (!buf.contentEquals(chunkRaw.crcval)) arraycopy(buf, off, chunkRaw.crcval, crcn, crcRead)
                crcn += crcRead
                if (crcn == 4) {
                    if (crcCheck) {
                        if (mode == ChunkReaderMode.BUFFER) { // in buffer mode we compute the CRC on one single call
                            chunkRaw.updateCrc(chunkRaw.data!!, 0, chunkRaw.len)
                        }
                        chunkRaw.checkCrc(errorBehav === ErrorBehaviour.STRICT)
                    }
                    println("Chunk done")
                    chunkDone()
                }
            }
        }
        return if (bytesForData > 0 || crcRead > 0) bytesForData + crcRead else -1 /* should not happen */
    }// has read all 4 bytes from the crc