diff --git a/io/linereader/linereader.go b/io/linereader/linereader.go index 7243ca9..e60719b 100644 --- a/io/linereader/linereader.go +++ b/io/linereader/linereader.go @@ -2,6 +2,7 @@ package linereader import ( "bytes" + "errors" "io" armath "github.com/asymmetric-research/go-commons/math" @@ -28,9 +29,17 @@ func NewInto(dst *T, reader io.Reader, blockSize uint) { } } -// Read reads as much as possible into p, until the next newline or EOF is reached. +func (lr *T) Read(dst []byte) (n int, err error) { + n, discarded, err := lr.ReadExtra(dst) + if discarded != 0 { + return n + discarded, errors.New("line too long") + } + return n, err +} + +// ReadExtra reads as much as possible into p, until the next newline or EOF is reached. // Every new call to read starts on a new line. The remainder of the previous line will be discarted. -func (lr *T) Read(dst []byte) (nread int, ndiscarted int, err error) { +func (lr *T) ReadExtra(dst []byte) (nread int, ndiscarted int, err error) { // copy as much of read buffer as possible to dst if len(lr.readbuf) > 0 { // fast path: can we get a new line from the read buffer? diff --git a/io/linereader/linereader_test.go b/io/linereader/linereader_test.go index dff02a8..080a189 100644 --- a/io/linereader/linereader_test.go +++ b/io/linereader/linereader_test.go @@ -24,7 +24,7 @@ func TestLineReader(t *testing.T) { var n int for err != io.EOF { - n, _, err = r.Read(linesback[:]) + n, _, err = r.ReadExtra(linesback[:]) line = linesback[:n] require.Equal(t, expectedLines[0], string(line)) expectedLines = expectedLines[1:] @@ -45,7 +45,7 @@ func TestLinesOfReaderTruncation(t *testing.T) { var n int for err != io.EOF { - n, _, err = r.Read(linesback[:]) + n, _, err = r.ReadExtra(linesback[:]) if err != nil { break } @@ -129,7 +129,7 @@ func runOurs(t require.TestingT, r io.Reader) { cnt := 0 for err == nil { - _, _, err = rd.Read(lineBacking[:]) + _, _, err = rd.ReadExtra(lineBacking[:]) cnt += 1 } require.Equal(t, 283, cnt)