Remove the iohelp.ReadNBytes which is better done with ReadFull

This commit is contained in:
Neil Macneale IV
2025-12-15 21:11:33 +00:00
parent f637baa3af
commit 66f6b27183
5 changed files with 21 additions and 33 deletions

View File

@@ -48,26 +48,6 @@ func (r *ErrPreservingReader) Read(p []byte) (int, error) {
return n, r.Err
}
// ReadNBytes will read n bytes from the given reader and return a new slice containing the data. ReadNBytes will always
// return n bytes, or it will return no data and an error (So if you request 100 bytes and there are only 99 left before
// the reader returns io.EOF you won't receive any of the data as this is considered an error as it can't read 100 bytes).
func ReadNBytes(r io.Reader, n int) ([]byte, error) {
bytes := make([]byte, n)
var err error
for totalRead := 0; totalRead < n; {
if err != nil {
return nil, err
}
read := 0
read, err = r.Read(bytes[totalRead:])
totalRead += read
}
return bytes, nil
}
// ReadLine will read a line from an unbuffered io.Reader where it considers lines to be separated by newlines (\n).

View File

@@ -17,6 +17,7 @@ package iohelp
import (
"bufio"
"bytes"
"io"
"reflect"
"testing"
@@ -27,10 +28,14 @@ func TestErrPreservingReader(t *testing.T) {
tr := test.NewTestReader(32, 16)
epr := NewErrPreservingReader(tr)
read1, noErr1 := ReadNBytes(epr, 8)
read2, noErr2 := ReadNBytes(epr, 8)
read3, firstErr := ReadNBytes(epr, 8)
read4, secondErr := ReadNBytes(epr, 8)
read1 := make([]byte, 8)
_, noErr1 := io.ReadFull(epr, read1)
read2 := make([]byte, 8)
_, noErr2 := io.ReadFull(epr, read2)
read3 := make([]byte, 8)
_, firstErr := io.ReadFull(epr, read3)
read4 := make([]byte, 8)
_, secondErr := io.ReadFull(epr, read4)
for i := 0; i < 8; i++ {
if read1[i] != byte(i) || read2[i] != byte(i)+8 {
@@ -38,8 +43,9 @@ func TestErrPreservingReader(t *testing.T) {
}
}
if read3 != nil || read4 != nil {
t.Error("Unexpected read values should be nil.")
// With io.ReadFull, we expect the buffers to exist but error should be set
if len(read3) == 0 || len(read4) == 0 {
t.Error("Expected read buffers to exist.")
}
if noErr1 != nil || noErr2 != nil {

View File

@@ -25,7 +25,6 @@ import (
"runtime/debug"
"sync/atomic"
"github.com/dolthub/dolt/go/libraries/utils/iohelp"
"github.com/dolthub/dolt/go/store/hash"
)
@@ -91,7 +90,8 @@ func ReadTableFooter(rd io.ReadSeeker) (chunkCount uint32, totalUncompressedData
return 0, 0, err
}
footer, err := iohelp.ReadNBytes(rd, int(footerSize))
footer := make([]byte, footerSize)
_, err = io.ReadFull(rd, footer)
if err != nil {
return 0, 0, err

View File

@@ -19,8 +19,6 @@ import (
"io"
"math"
"github.com/dolthub/dolt/go/libraries/utils/iohelp"
"github.com/dolthub/dolt/go/store/chunks"
"github.com/dolthub/dolt/go/store/hash"
)
@@ -100,5 +98,7 @@ func readNFrom(rd io.ReadSeeker, offset uint64, length uint32) ([]byte, error) {
return nil, err
}
return iohelp.ReadNBytes(rd, int(length))
buf := make([]byte, length)
_, err = io.ReadFull(rd, buf)
return buf, err
}

View File

@@ -150,7 +150,8 @@ func NewTupleReader(nbf *NomsBinFormat, vrw ValueReadWriter, rd io.Reader) Tuple
// Read reades the next tuple from the TupleReader
func (trd *tupleReaderImpl) Read() (*Tuple, error) {
sizeBytes, err := iohelp.ReadNBytes(trd.rd, 4)
sizeBytes := make([]byte, 4)
_, err := io.ReadFull(trd.rd, sizeBytes)
if err != nil {
return nil, err
}
@@ -161,7 +162,8 @@ func (trd *tupleReaderImpl) Read() (*Tuple, error) {
return nil, nil
}
data, err := iohelp.ReadNBytes(trd.rd, int(size))
data := make([]byte, size)
_, err = io.ReadFull(trd.rd, data)
if err != nil {
if err == io.EOF {
return nil, errors.New("corrupt tuple stream")