readChunkAt copies data from the chunk at the given absolute offset to the provided slice. Chunk must be available when this method is called. Returns the number of bytes copied to the slice or 0 if chunk has no data (eg. offset is beyond the end of the stream).
(p []byte, off int64)
| 474 | // Returns the number of bytes copied to the slice or 0 if chunk has no data |
| 475 | // (eg. offset is beyond the end of the stream). |
| 476 | func (ab *AsyncBuffer) readChunkAt(p []byte, off int64) int { |
| 477 | // If the chunk is not available, we return 0 |
| 478 | if off >= ab.bytesRead.Load() { |
| 479 | return 0 |
| 480 | } |
| 481 | |
| 482 | ind := off / ChunkSize // chunk index |
| 483 | chunk := ab.chunks[ind] |
| 484 | |
| 485 | startOffset := off % ChunkSize // starting offset in the chunk |
| 486 | |
| 487 | // If the offset in current chunk is greater than the data |
| 488 | // it has, we return 0 |
| 489 | if startOffset >= int64(len(chunk.data)) { |
| 490 | return 0 |
| 491 | } |
| 492 | |
| 493 | // Copy data to the target slice. The number of bytes to copy is limited by the |
| 494 | // size of the target slice and the size of the data in the chunk. |
| 495 | return copy(p, chunk.data[startOffset:]) |
| 496 | } |