forked from linuxdeepin/go-x11-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
writer.go
75 lines (61 loc) · 1.28 KB
/
writer.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
// SPDX-FileCopyrightText: 2022 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later
package x
import (
"bytes"
)
type Writer struct {
buf bytes.Buffer
}
func NewWriter() *Writer {
return &Writer{}
}
func (w *Writer) Reset() {
w.buf.Reset()
}
func (w *Writer) WritePad(n int) {
for i := 0; i < n; i++ {
w.buf.WriteByte(0)
}
}
func (w *Writer) Write1b(v uint8) {
w.buf.WriteByte(v)
}
func (w *Writer) Write2b(v uint16) {
w.buf.WriteByte(byte(v))
w.buf.WriteByte(byte(v >> 8))
}
func (w *Writer) Write4b(v uint32) {
w.buf.WriteByte(byte(v))
w.buf.WriteByte(byte(v >> 8))
w.buf.WriteByte(byte(v >> 16))
w.buf.WriteByte(byte(v >> 24))
}
func (w *Writer) Write8b(v uint64) {
w.buf.WriteByte(byte(v))
w.buf.WriteByte(byte(v >> 8))
w.buf.WriteByte(byte(v >> 16))
w.buf.WriteByte(byte(v >> 24))
w.buf.WriteByte(byte(v >> 32))
w.buf.WriteByte(byte(v >> 40))
w.buf.WriteByte(byte(v >> 48))
w.buf.WriteByte(byte(v >> 56))
}
func (w *Writer) WriteBytes(p []byte) {
w.buf.Write(p)
}
func (w *Writer) WriteNBytes(n int, p []byte) {
if len(p) < n {
w.buf.Write(p)
w.WritePad(n - len(p))
} else {
w.buf.Write(p[:n])
}
}
func (w *Writer) WriteString(s string) {
w.buf.WriteString(s)
}
func (w *Writer) Bytes() []byte {
return w.buf.Bytes()
}