mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
make NewBufRead etc. idempotent
R=rsc DELTA=63 (59 added, 0 deleted, 4 changed) OCL=27143 CL=27143
This commit is contained in:
parent
ac6ebfdea9
commit
ee19695cfc
2 changed files with 63 additions and 4 deletions
|
|
@ -54,12 +54,18 @@ type BufRead struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewBufReadSize creates a new BufRead whose buffer has the specified size,
|
// NewBufReadSize creates a new BufRead whose buffer has the specified size,
|
||||||
// which must be greater than zero.
|
// which must be greater than zero. If the argument io.Read is already a
|
||||||
|
// BufRead with large enough size, it returns the underlying BufRead.
|
||||||
// It returns the BufRead and any error.
|
// It returns the BufRead and any error.
|
||||||
func NewBufReadSize(rd io.Read, size int) (b *BufRead, err *os.Error) {
|
func NewBufReadSize(rd io.Read, size int) (*BufRead, *os.Error) {
|
||||||
if size <= 0 {
|
if size <= 0 {
|
||||||
return nil, BadBufSize
|
return nil, BadBufSize
|
||||||
}
|
}
|
||||||
|
// Is it already a BufRead?
|
||||||
|
b, ok := rd.(*BufRead);
|
||||||
|
if ok && len(b.buf) >= size {
|
||||||
|
return b, nil
|
||||||
|
}
|
||||||
b = new(BufRead);
|
b = new(BufRead);
|
||||||
b.buf = make([]byte, size);
|
b.buf = make([]byte, size);
|
||||||
b.rd = rd;
|
b.rd = rd;
|
||||||
|
|
@ -381,12 +387,18 @@ type BufWrite struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewBufWriteSize creates a new BufWrite whose buffer has the specified size,
|
// NewBufWriteSize creates a new BufWrite whose buffer has the specified size,
|
||||||
// which must be greater than zero.
|
// which must be greater than zero. If the argument io.Write is already a
|
||||||
|
// BufWrite with large enough size, it returns the underlying BufWrite.
|
||||||
// It returns the BufWrite and any error.
|
// It returns the BufWrite and any error.
|
||||||
func NewBufWriteSize(wr io.Write, size int) (b *BufWrite, err *os.Error) {
|
func NewBufWriteSize(wr io.Write, size int) (*BufWrite, *os.Error) {
|
||||||
if size <= 0 {
|
if size <= 0 {
|
||||||
return nil, BadBufSize
|
return nil, BadBufSize
|
||||||
}
|
}
|
||||||
|
// Is it already a BufWrite?
|
||||||
|
b, ok := wr.(*BufWrite);
|
||||||
|
if ok && len(b.buf) >= size {
|
||||||
|
return b, nil
|
||||||
|
}
|
||||||
b = new(BufWrite);
|
b = new(BufWrite);
|
||||||
b.buf = make([]byte, size);
|
b.buf = make([]byte, size);
|
||||||
b.wr = wr;
|
b.wr = wr;
|
||||||
|
|
|
||||||
|
|
@ -330,3 +330,50 @@ func TestBufWrite(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestNewBufReadSizeIdempotent(t *testing.T) {
|
||||||
|
const BufSize = 1000;
|
||||||
|
b, err := NewBufReadSize(newByteReader(io.StringBytes("hello world")), BufSize);
|
||||||
|
if err != nil {
|
||||||
|
t.Error("NewBufReadSize create fail", err);
|
||||||
|
}
|
||||||
|
// Does it recognize itself?
|
||||||
|
b1, err2 := NewBufReadSize(b, BufSize);
|
||||||
|
if err2 != nil {
|
||||||
|
t.Error("NewBufReadSize #2 create fail", err2);
|
||||||
|
}
|
||||||
|
if b1 != b {
|
||||||
|
t.Error("NewBufReadSize did not detect underlying BufRead");
|
||||||
|
}
|
||||||
|
// Does it wrap if existing buffer is too small?
|
||||||
|
b2, err3 := NewBufReadSize(b, 2*BufSize);
|
||||||
|
if err3 != nil {
|
||||||
|
t.Error("NewBufReadSize #3 create fail", err3);
|
||||||
|
}
|
||||||
|
if b2 == b {
|
||||||
|
t.Error("NewBufReadSize did not enlarge buffer");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestNewBufWriteSizeIdempotent(t *testing.T) {
|
||||||
|
const BufSize = 1000;
|
||||||
|
b, err := NewBufWriteSize(newByteWriter(), BufSize);
|
||||||
|
if err != nil {
|
||||||
|
t.Error("NewBufWriteSize create fail", err);
|
||||||
|
}
|
||||||
|
// Does it recognize itself?
|
||||||
|
b1, err2 := NewBufWriteSize(b, BufSize);
|
||||||
|
if err2 != nil {
|
||||||
|
t.Error("NewBufWriteSize #2 create fail", err2);
|
||||||
|
}
|
||||||
|
if b1 != b {
|
||||||
|
t.Error("NewBufWriteSize did not detect underlying BufWrite");
|
||||||
|
}
|
||||||
|
// Does it wrap if existing buffer is too small?
|
||||||
|
b2, err3 := NewBufWriteSize(b, 2*BufSize);
|
||||||
|
if err3 != nil {
|
||||||
|
t.Error("NewBufWriteSize #3 create fail", err3);
|
||||||
|
}
|
||||||
|
if b2 == b {
|
||||||
|
t.Error("NewBufWriteSize did not enlarge buffer");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue