Add ReadFrom and WriteTo methods to bytes.Buffer, to enable i/o without buffer allocation.

Use them in Copy and Copyn.
Speed up ReadFile by using ReadFrom and avoiding Copy altogether (a minor win).

R=rsc, gri
CC=golang-dev
https://golang.org/cl/166041
This commit is contained in:
Rob Pike 2009-12-03 12:56:16 -08:00
parent 7e7008fa5e
commit bc3e34759c
5 changed files with 194 additions and 5 deletions

View file

@ -240,3 +240,25 @@ func TestNil(t *testing.T) {
t.Error("expcted <nil>; got %q", b.String())
}
}
func TestReadFrom(t *testing.T) {
var buf Buffer;
for i := 3; i < 30; i += 3 {
s := fillBytes(t, "TestReadFrom (1)", &buf, "", 5, bytes[0:len(bytes)/i]);
var b Buffer;
b.ReadFrom(&buf);
empty(t, "TestReadFrom (2)", &b, s, make([]byte, len(data)));
}
}
func TestWriteTo(t *testing.T) {
var buf Buffer;
for i := 3; i < 30; i += 3 {
s := fillBytes(t, "TestReadFrom (1)", &buf, "", 5, bytes[0:len(bytes)/i]);
var b Buffer;
buf.WriteTo(&b);
empty(t, "TestReadFrom (2)", &b, s, make([]byte, len(data)));
}
}