File: writer.go

package info (click to toggle)
golang-github-linuxdeepin-go-x11-client 0.6.3-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 1,656 kB
  • sloc: python: 944; sh: 38; makefile: 17
file content (71 lines) | stat: -rw-r--r-- 1,188 bytes parent folder | download | duplicates (2)
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
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()
}