Skip to content

Commit

Permalink
Fix split reader
Browse files Browse the repository at this point in the history
  • Loading branch information
gagliardetto committed Oct 8, 2023
1 parent 71ad5b5 commit f864752
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions readahead/readahead.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,14 @@ func (cr *CachingReader) Read(p []byte) (int, error) {
}

if len(p) > cr.chunkSize {
cr.buffer.Reset() // clear buffer
// If the read is larger than the chunk size, read directly from the file
return cr.file.Read(p)
// read what we can from the buffer
n := copy(p, cr.buffer.Next(cr.chunkSize))
// read the rest directly from the file
n2, err := cr.file.Read(p[n:])
if err != nil && err != io.EOF {
return 0, fmt.Errorf("failed to read from file: %w", err)
}
return n + n2, nil
}

// Refill the buffer if needed
Expand Down

0 comments on commit f864752

Please sign in to comment.