override fun read()

in jvm/src/main/kotlin/com/jetbrains/util/SubrangeSeekableByteChannel.kt [29:53]


  override fun read(dst: ByteBuffer): Int {
    if (pos >= length) return -1
    if (!dst.hasRemaining()) return 0

    val allowed = Math.min(length - pos, dst.remaining().toLong()).toInt()

    // Save original limit and restrict to allowed bytes
    val originalLimit = dst.limit()
    val limitedLimit = dst.position() + allowed
    if (limitedLimit < originalLimit) {
      dst.limit(limitedLimit)
    }

    // Position base to absolute offset and read
    base.position(startOffset + pos)
    val read = try {
      base.read(dst)
    } finally {
      // Restore original limit
      dst.limit(originalLimit)
    }

    if (read > 0) pos += read.toLong()
    return if (read == 0 && allowed == 0) -1 else read
  }