Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
p2p
go-buffer-pool
Commits
eb6b2a44
Commit
eb6b2a44
authored
Jan 21, 2018
by
Steven Allen
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
implement ReadFrom
parent
ac6cb82b
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
28 additions
and
3 deletions
+28
-3
buffer.go
buffer.go
+28
-1
buffer_test.go
buffer_test.go
+0
-2
No files found.
buffer.go
View file @
eb6b2a44
...
...
@@ -235,7 +235,34 @@ func (b *Buffer) WriteTo(w io.Writer) (int64, error) {
return
0
,
nil
}
// TODO: implement ReadFrom
// MinRead is the minimum slice size passed to a Read call by
// Buffer.ReadFrom. As long as the Buffer has at least MinRead bytes beyond
// what is required to hold the contents of r, ReadFrom will not grow the
// underlying buffer.
const
MinRead
=
512
// ReadFrom reads from the given reader into the buffer.
func
(
b
*
Buffer
)
ReadFrom
(
r
io
.
Reader
)
(
int64
,
error
)
{
n
:=
int64
(
0
)
for
{
wOff
:=
b
.
grow
(
MinRead
)
// Use *entire* buffer.
b
.
buf
=
b
.
buf
[
:
cap
(
b
.
buf
)]
read
,
err
:=
r
.
Read
(
b
.
buf
[
wOff
:
])
b
.
buf
=
b
.
buf
[
:
wOff
+
read
]
n
+=
int64
(
read
)
switch
err
{
case
nil
:
case
io
.
EOF
:
err
=
nil
fallthrough
default
:
b
.
shrink
()
return
n
,
err
}
}
}
// Read reads at most `len(buf)` bytes from the internal buffer into the given
// buffer.
...
...
buffer_test.go
View file @
eb6b2a44
...
...
@@ -239,7 +239,6 @@ func TestNil(t *testing.T) {
}
}
/*
func
TestReadFrom
(
t
*
testing
.
T
)
{
var
buf
Buffer
for
i
:=
3
;
i
<
30
;
i
+=
3
{
...
...
@@ -249,7 +248,6 @@ func TestReadFrom(t *testing.T) {
empty
(
t
,
"TestReadFrom (2)"
,
&
b
,
s
,
make
([]
byte
,
len
(
data
)))
}
}
*/
func
TestWriteTo
(
t
*
testing
.
T
)
{
var
buf
Buffer
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment