-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
io.go
56 lines (48 loc) · 1.16 KB
/
io.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package minequery
import (
"bytes"
"io"
"golang.org/x/text/encoding/unicode"
)
var (
utf16BEEncoder = unicode.UTF16(unicode.BigEndian, unicode.IgnoreBOM).NewEncoder()
utf16BEDecoder = unicode.UTF16(unicode.BigEndian, unicode.IgnoreBOM).NewDecoder()
)
// readAllUntilZero reads all bytes from reader until it hits zero.
// This is a backport from newer Go stdlib for sake of minequery's compatibility with Go 1.13.
func readAllUntilZero(reader io.ByteReader) ([]byte, error) {
buf := &bytes.Buffer{}
for {
b, err := reader.ReadByte()
if err != nil {
if err == io.EOF {
return buf.Bytes(), nil
}
return nil, err
}
if b != 0 {
buf.WriteByte(b)
} else {
return buf.Bytes(), nil
}
}
}
// readAll reads all bytes from reader.
// This is a backport from newer Go stdlib for sake of minequery's compatibility with Go 1.13.
func readAll(r io.Reader) ([]byte, error) {
b := make([]byte, 0, 512)
for {
n, err := r.Read(b[len(b):cap(b)])
b = b[:len(b)+n]
if err != nil {
if err == io.EOF {
err = nil
}
return b, err
}
if len(b) == cap(b) {
// Add more capacity (let append pick how much).
b = append(b, 0)[:len(b)]
}
}
}