-
Notifications
You must be signed in to change notification settings - Fork 6
/
readall.go
38 lines (33 loc) · 931 Bytes
/
readall.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// Copyright 2022 The incite Authors. All rights reserved.
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
package incite
import (
"io"
)
// ReadAll reads from s until an error or EOF and returns the data it
// read. A successful call returns err == nil, not err == EOF. Because
// ReadAll is defined to read from s until EOF, it does not treat an EOF
// from Read as an error to be reported.
func ReadAll(s Stream) ([]Result, error) {
if s == nil {
panic(nilStreamMsg)
}
// For interests' sake, this implementation is borrowed almost
// char-for-char from io.ReadAll.
x := make([]Result, 0, 512)
for {
if len(x) == cap(x) {
// Add more capacity (let append pick now much).
x = append(x, Result{})[:len(x)]
}
n, err := s.Read(x[len(x):cap(x)])
x = x[:len(x)+n]
if err != nil {
if err == io.EOF {
err = nil
}
return x, err
}
}
}