-
Notifications
You must be signed in to change notification settings - Fork 0
/
ByteBuf.go
104 lines (88 loc) · 1.77 KB
/
ByteBuf.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 codec
import (
"bufio"
)
type ByteBuf struct {
*bufio.Reader
*bufio.Writer
}
func (b *ByteBuf) ReadBuffer() *bufio.Reader {
return b.Reader
}
func (b *ByteBuf) ReadInt() int {
readInt, err := ReadInt(b.Reader)
if err != nil {
panic(err)
}
return readInt
}
func (b *ByteBuf) ReadBytea() byte {
readByte, err := b.Reader.ReadByte()
if err != nil {
panic(err)
}
return readByte
}
func (b *ByteBuf) ReadBytes(size int) []byte {
bytes, err := ReadBytes(b.Reader, size)
if err != nil {
panic(err)
}
return bytes
}
func (b *ByteBuf) ReadLine() string {
var readData []byte
for {
line, more, err := b.Reader.ReadLine()
if err != nil {
panic(err)
}
if line != nil {
readData = append(readData, line...)
}
if more {
continue
}
break
}
return string(readData)
}
func (b *ByteBuf) WriteBuffer() *bufio.Writer {
return b.Writer
}
func (b *ByteBuf) WriteInt(data int) {
if n, err := b.Write(IntToByteBigEndian(data)); n != 4 || err != nil {
panic(err)
}
}
func (b *ByteBuf) WriteBytea(data byte) {
if err := b.Writer.WriteByte(data); err != nil {
panic(err)
}
}
func (b *ByteBuf) WriteBytes(data []byte) {
if n, err := b.Writer.Write(data); err != nil || n != len(data) {
panic(err)
}
}
func (b *ByteBuf) WriteString(data string) {
if _, err := b.Writer.WriteString(data); err != nil {
panic(err)
}
}
func (b *ByteBuf) Flush() {
if b.Writer != nil {
if err := b.Writer.Flush(); err != nil {
panic(err)
}
}
}
func NewReadByteBuf(reader *bufio.Reader) *ByteBuf {
return &ByteBuf{Reader: reader}
}
func NewWriteByteBuf(writeBuffer *bufio.Writer) *ByteBuf {
return &ByteBuf{Writer: writeBuffer}
}
func NewReadWriteByteBuf(reader *bufio.Reader, writer *bufio.Writer) *ByteBuf {
return &ByteBuf{Reader: reader, Writer: writer}
}