forked from go-pg/pg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
buffer.go
104 lines (84 loc) · 2.24 KB
/
buffer.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package pg
import (
"encoding/binary"
"io"
)
var nullParamLength = int32(-1)
type buffer struct {
Bytes []byte
start []int // Message start position.
}
func newBuffer() *buffer {
return &buffer{
Bytes: make([]byte, 0, 8192),
}
}
func (buf *buffer) StartMessage(c msgType) {
if c == 0 {
buf.start = append(buf.start, len(buf.Bytes))
buf.Bytes = append(buf.Bytes, 0, 0, 0, 0)
} else {
buf.start = append(buf.start, len(buf.Bytes)+1)
buf.Bytes = append(buf.Bytes, byte(c), 0, 0, 0, 0)
}
}
func (buf *buffer) StartParam() {
buf.StartMessage(0)
}
func (buf *buffer) popStart() int {
start := buf.start[len(buf.start)-1]
buf.start = buf.start[:len(buf.start)-1]
return start
}
func (buf *buffer) FinishMessage() {
start := buf.popStart()
binary.BigEndian.PutUint32(buf.Bytes[start:], uint32(len(buf.Bytes)-start))
}
func (buf *buffer) FinishParam() {
start := buf.popStart()
binary.BigEndian.PutUint32(buf.Bytes[start:], uint32(len(buf.Bytes)-start-4))
}
func (buf *buffer) FinishNullParam() {
start := buf.popStart()
binary.BigEndian.PutUint32(buf.Bytes[start:], uint32(nullParamLength))
}
func (buf *buffer) Write(b []byte) (int, error) {
buf.Bytes = append(buf.Bytes, b...)
return len(b), nil
}
func (buf *buffer) WriteInt16(num int16) {
buf.Bytes = append(buf.Bytes, 0, 0)
binary.BigEndian.PutUint16(buf.Bytes[len(buf.Bytes)-2:], uint16(num))
}
func (buf *buffer) WriteInt32(num int32) {
buf.Bytes = append(buf.Bytes, 0, 0, 0, 0)
binary.BigEndian.PutUint32(buf.Bytes[len(buf.Bytes)-4:], uint32(num))
}
func (buf *buffer) WriteString(s string) {
buf.Bytes = append(buf.Bytes, s...)
buf.Bytes = append(buf.Bytes, 0)
}
func (buf *buffer) WriteBytes(b []byte) {
buf.Bytes = append(buf.Bytes, b...)
buf.Bytes = append(buf.Bytes, 0)
}
func (buf *buffer) WriteByte(c byte) {
buf.Bytes = append(buf.Bytes, c)
}
func (buf *buffer) Flush() []byte {
if len(buf.start) != 0 {
panic("message was not finished")
}
b := buf.Bytes[:]
buf.Bytes = buf.Bytes[:0]
return b
}
func (buf *buffer) Reset() {
buf.start = buf.start[:0]
buf.Bytes = buf.Bytes[:0]
}
func (buf *buffer) ReadFrom(r io.Reader) (int64, error) {
n, err := r.Read(buf.Bytes[len(buf.Bytes):cap(buf.Bytes)])
buf.Bytes = buf.Bytes[:len(buf.Bytes)+int(n)]
return int64(n), err
}