trufflehog/pkg/common/chunker.go
Bill Rich a473b9aa99
Use re-readable reader and common chunker (#703)
* Use re-readable reader and common chunker

* Linter feedback

* Break on error
2022-08-10 15:32:49 -07:00

36 lines
675 B
Go

package common
import (
"bufio"
"errors"
"io"
log "github.com/sirupsen/logrus"
)
const (
ChunkSize = 10 * 1024
PeekSize = 3 * 1024
)
func ChunkReader(r io.Reader) chan []byte {
chunkChan := make(chan []byte)
go func() {
defer close(chunkChan)
reader := bufio.NewReaderSize(bufio.NewReader(r), ChunkSize)
for {
chunk := make([]byte, ChunkSize)
n, err := reader.Read(chunk)
if err != nil && !errors.Is(err, io.EOF) {
log.WithError(err).Error("Error chunking reader.")
break
}
peekData, _ := reader.Peek(PeekSize)
chunkChan <- append(chunk[:n], peekData...)
if errors.Is(err, io.EOF) {
break
}
}
}()
return chunkChan
}