-
Notifications
You must be signed in to change notification settings - Fork 47
/
rbuf.go
72 lines (62 loc) · 1.18 KB
/
rbuf.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// Copyright ©2021 The go-pdf Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package fpdf
import (
"bytes"
"encoding/binary"
"io"
)
type rbuffer struct {
p []byte
c int
}
// newRBuffer returns a new buffer populated with the contents of the specified Reader
func newRBuffer(r io.Reader) (b *rbuffer, err error) {
buf := new(bytes.Buffer)
_, err = buf.ReadFrom(r)
b = &rbuffer{p: buf.Bytes()}
return
}
func (r *rbuffer) Read(p []byte) (int, error) {
if r.c >= len(r.p) {
return 0, io.EOF
}
n := copy(p, r.p[r.c:])
r.c += n
return n, nil
}
func (r *rbuffer) ReadByte() (byte, error) {
if r.c >= len(r.p) {
return 0, io.EOF
}
v := r.p[r.c]
r.c++
return v, nil
}
func (r *rbuffer) u8() uint8 {
if r.c >= len(r.p) {
panic(io.ErrShortBuffer)
}
v := r.p[r.c]
r.c++
return v
}
func (r *rbuffer) u32() uint32 {
const n = 4
if r.c+n >= len(r.p) {
panic(io.ErrShortBuffer)
}
beg := r.c
r.c += n
v := binary.BigEndian.Uint32(r.p[beg:])
return v
}
func (r *rbuffer) i32() int32 {
return int32(r.u32())
}
func (r *rbuffer) Next(n int) []byte {
c := r.c
r.c += n
return r.p[c:r.c]
}