zbytes.go 1.02 KB
Newer Older
Vladimir Barsukov's avatar
zbytes  
Vladimir Barsukov committed
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
package zbytes

import (
	"bytes"
	"sync"
)

type ZBuffer struct {
	Buff *bytes.Buffer
	Mu   *sync.Mutex
}

func NewBuffer() *ZBuffer {
	return &ZBuffer{
		Buff: bytes.NewBuffer(nil),
		Mu:   new(sync.Mutex),
	}
}

func (z *ZBuffer) Write(p []byte) (n int, err error) {
	z.Mu.Lock()
	defer z.Mu.Unlock()
	return z.Buff.Write(p)
}
func (z *ZBuffer) WriteRune(r rune) (n int, err error) {
	z.Mu.Lock()
	defer z.Mu.Unlock()
	return z.Buff.WriteRune(r)
}
func (z *ZBuffer) WriteByte(c byte) error {
	z.Mu.Lock()
	z.Mu.Unlock()
	return z.Buff.WriteByte(c)
}
func (z *ZBuffer) WriteString(s string) (n int, err error) {
	z.Mu.Lock()
	defer z.Mu.Unlock()
	return z.Buff.WriteString(s)
}

func (z *ZBuffer) Bytes() []byte {
	z.Mu.Lock()
	defer z.Mu.Unlock()
	return z.Buff.Bytes()
}

func (z *ZBuffer) String() string {
	z.Mu.Lock()
	defer z.Mu.Unlock()
	return z.Buff.String()
}

func (z *ZBuffer) Reset() {
	z.Mu.Lock()
	defer z.Mu.Unlock()
	z.Buff.Reset()
}
func (z *ZBuffer) Len() int {
	z.Mu.Lock()
	defer z.Mu.Unlock()
	return z.Buff.Len()
}