override fun consume()

in plot-api/src/commonMain/kotlin/org/jetbrains/letsPlot/util/pngj/ChunkSeqReader.kt [101:143]


    override fun consume(buf: ByteArray, offset: Int, len: Int): Int {
        if (isClosed) return -1
        if (len == 0) return 0 // nothing to do (should not happen)
        if (len < 0) throw PngjInputException("This should not happen. Bad length: $len")
        var processed = 0
        if (isSignatureDone) {
            if (curChunkReader == null || curChunkReader!!.isDone) { // new chunk: read first 8 bytes
                var read0 = 8 - buf0len
                if (read0 > len) read0 = len
                arraycopy(buf, offset, buf0, buf0len, read0)
                buf0len += read0
                processed += read0
                bytesCount += read0.toLong()
                // len -= read0;
                // offset += read0;
                if (buf0len == 8) { // end reading chunk length and id
                    chunkCount++
                    val clen: Int = PngHelperInternal.readInt4fromBytes(buf0, 0)
                    val cid: String = ChunkHelper.idFromBytes(buf0, 4)
                    startNewChunk(clen, cid, bytesCount - 8)
                    buf0len = 0
                }
            } else { // reading chunk, delegates to curChunkReader
                val read1: Int = curChunkReader!!.consume(buf, offset, len)
                if (read1 < 0) return -1 // should not happen
                processed += read1
                bytesCount += read1.toLong()
            }
        } else { // reading signature
            var read = signatureLength - buf0len
            if (read > len) read = len
            arraycopy(buf, offset, buf0, buf0len, read)
            buf0len += read
            if (buf0len == signatureLength) {
                checkSignature(buf0)
                buf0len = 0
                isSignatureDone = true
            }
            processed += read
            bytesCount += read.toLong()
        }
        return processed
    }