-
Notifications
You must be signed in to change notification settings - Fork 0
/
buff.go
55 lines (46 loc) · 963 Bytes
/
buff.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
package proxy
import (
"bytes"
"encoding/binary"
"log"
"github.com/lib/pq/oid"
)
// ReadBuf byte array helper
type ReadBuf []byte
// Int32 get 4 bytes as int32
func (b *ReadBuf) Int32() (n int) {
n = int(int32(binary.BigEndian.Uint32(*b)))
*b = (*b)[4:]
return
}
// Oid get 4 bytes as oid
func (b *ReadBuf) Oid() (n oid.Oid) {
n = oid.Oid(binary.BigEndian.Uint32(*b))
*b = (*b)[4:]
return
}
// Int16 N.B: this is actually an unsigned 16-bit integer, unlike int32
func (b *ReadBuf) Int16() (n int) {
n = int(binary.BigEndian.Uint16(*b))
*b = (*b)[2:]
return
}
func (b *ReadBuf) String() string {
i := bytes.IndexByte(*b, 0)
if i < 0 {
log.Fatalf("invalid message format; expected string terminator")
}
s := (*b)[:i]
*b = (*b)[i+1:]
return string(s)
}
// Next get next N bytes
func (b *ReadBuf) Next(n int) (v []byte) {
v = (*b)[:n]
*b = (*b)[n:]
return
}
// Byte get one byte
func (b *ReadBuf) Byte() byte {
return b.Next(1)[0]
}