Commit 6128ff84 authored by Brad Fitzpatrick's avatar Brad Fitzpatrick

bufio: add Reader.Size and Writer.Size accessors

Fixes #21343

Change-Id: I3582fced902592fe12bfa29acf7b40b6e5e554a7
Reviewed-on: https://go-review.googlesource.com/75150
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: 's avatarRobert Griesemer <gri@golang.org>
parent 2da1446b
...@@ -62,6 +62,9 @@ func NewReader(rd io.Reader) *Reader { ...@@ -62,6 +62,9 @@ func NewReader(rd io.Reader) *Reader {
return NewReaderSize(rd, defaultBufSize) return NewReaderSize(rd, defaultBufSize)
} }
// Size returns the size of the underlying buffer in bytes.
func (r *Reader) Size() int { return len(r.buf) }
// Reset discards any buffered data, resets all state, and switches // Reset discards any buffered data, resets all state, and switches
// the buffered reader to read from r. // the buffered reader to read from r.
func (b *Reader) Reset(r io.Reader) { func (b *Reader) Reset(r io.Reader) {
...@@ -548,6 +551,9 @@ func NewWriter(w io.Writer) *Writer { ...@@ -548,6 +551,9 @@ func NewWriter(w io.Writer) *Writer {
return NewWriterSize(w, defaultBufSize) return NewWriterSize(w, defaultBufSize)
} }
// Size returns the size of the underlying buffer in bytes.
func (b *Writer) Size() int { return len(b.buf) }
// Reset discards any unflushed buffered data, clears any error, and // Reset discards any unflushed buffered data, clears any error, and
// resets b to write its output to w. // resets b to write its output to w.
func (b *Writer) Reset(w io.Writer) { func (b *Writer) Reset(w io.Writer) {
......
...@@ -1418,6 +1418,24 @@ func TestReaderDiscard(t *testing.T) { ...@@ -1418,6 +1418,24 @@ func TestReaderDiscard(t *testing.T) {
} }
func TestReaderSize(t *testing.T) {
if got, want := NewReader(nil).Size(), DefaultBufSize; got != want {
t.Errorf("NewReader's Reader.Size = %d; want %d", got, want)
}
if got, want := NewReaderSize(nil, 1234).Size(), 1234; got != want {
t.Errorf("NewReaderSize's Reader.Size = %d; want %d", got, want)
}
}
func TestWriterSize(t *testing.T) {
if got, want := NewWriter(nil).Size(), DefaultBufSize; got != want {
t.Errorf("NewWriter's Writer.Size = %d; want %d", got, want)
}
if got, want := NewWriterSize(nil, 1234).Size(), 1234; got != want {
t.Errorf("NewWriterSize's Writer.Size = %d; want %d", got, want)
}
}
// An onlyReader only implements io.Reader, no matter what other methods the underlying implementation may have. // An onlyReader only implements io.Reader, no matter what other methods the underlying implementation may have.
type onlyReader struct { type onlyReader struct {
io.Reader io.Reader
......
...@@ -11,6 +11,8 @@ import ( ...@@ -11,6 +11,8 @@ import (
var IsSpace = isSpace var IsSpace = isSpace
const DefaultBufSize = defaultBufSize
func (s *Scanner) MaxTokenSize(n int) { func (s *Scanner) MaxTokenSize(n int) {
if n < utf8.UTFMax || n > 1e9 { if n < utf8.UTFMax || n > 1e9 {
panic("bad max token size") panic("bad max token size")
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment