Skip to content

Commit

Permalink
bufiox.NewReader on *bufiox.Reader
Browse files Browse the repository at this point in the history
  • Loading branch information
xushiwei committed Apr 12, 2020
1 parent 679d5a8 commit b4635b1
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
5 changes: 5 additions & 0 deletions bufiox/bufio_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ func TestSeeker(t *testing.T) {
if err != nil || string(data) != "Hello, china!!!" {
t.Fatal("ReadAll failed:", err, data)
}

b3 := NewReaderSize(b2, 32)
if b2 != b3 {
t.Fatal("NewReader on *bufiox.Reader")
}
}

// -------------------------------------------------------------------------------------
11 changes: 9 additions & 2 deletions bufiox/seek.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,24 @@ type Reader struct {
bufio.Reader
}

const (
defaultBufSize = 4096
)

// NewReader returns a new Reader whose buffer has the default size.
func NewReader(rd io.ReadSeeker) *Reader {
r := bufio.NewReader(rd)
return &Reader{Reader: *r}
return NewReaderSize(rd, defaultBufSize)
}

// NewReaderSize returns a new Reader whose buffer has at least the specified
// size. If the argument io.Reader is already a Reader with large enough size,
// it returns the underlying Reader.
//
func NewReaderSize(rd io.ReadSeeker, size int) *Reader {
b, ok := rd.(*Reader)
if ok && b.Size() >= size {
return b
}
r := bufio.NewReaderSize(rd, size)
return &Reader{Reader: *r}
}
Expand Down

0 comments on commit b4635b1

Please sign in to comment.