zbytes.go 1 KB
Newer Older
Vladimir Barsukov's avatar
zbytes  
Vladimir Barsukov committed
1
2
3
4
5
6
7
package zbytes

import (
	"bytes"
	"sync"
)

Vladimir Barsukov's avatar
fix    
Vladimir Barsukov committed
8
type Buffer struct {
Vladimir Barsukov's avatar
zbytes  
Vladimir Barsukov committed
9
10
11
12
	Buff *bytes.Buffer
	Mu   *sync.Mutex
}

Vladimir Barsukov's avatar
fix    
Vladimir Barsukov committed
13
14
func NewBuffer() *Buffer {
	return &Buffer{
Vladimir Barsukov's avatar
zbytes  
Vladimir Barsukov committed
15
16
17
18
19
		Buff: bytes.NewBuffer(nil),
		Mu:   new(sync.Mutex),
	}
}

Vladimir Barsukov's avatar
fix    
Vladimir Barsukov committed
20
func (z *Buffer) Write(p []byte) (n int, err error) {
Vladimir Barsukov's avatar
zbytes  
Vladimir Barsukov committed
21
22
23
24
	z.Mu.Lock()
	defer z.Mu.Unlock()
	return z.Buff.Write(p)
}
Vladimir Barsukov's avatar
fix    
Vladimir Barsukov committed
25
func (z *Buffer) WriteRune(r rune) (n int, err error) {
Vladimir Barsukov's avatar
zbytes  
Vladimir Barsukov committed
26
27
28
29
	z.Mu.Lock()
	defer z.Mu.Unlock()
	return z.Buff.WriteRune(r)
}
Vladimir Barsukov's avatar
fix    
Vladimir Barsukov committed
30
func (z *Buffer) WriteByte(c byte) error {
Vladimir Barsukov's avatar
zbytes  
Vladimir Barsukov committed
31
32
33
34
	z.Mu.Lock()
	z.Mu.Unlock()
	return z.Buff.WriteByte(c)
}
Vladimir Barsukov's avatar
fix    
Vladimir Barsukov committed
35
func (z *Buffer) WriteString(s string) (n int, err error) {
Vladimir Barsukov's avatar
zbytes  
Vladimir Barsukov committed
36
37
38
39
40
	z.Mu.Lock()
	defer z.Mu.Unlock()
	return z.Buff.WriteString(s)
}

Vladimir Barsukov's avatar
fix    
Vladimir Barsukov committed
41
func (z *Buffer) Bytes() []byte {
Vladimir Barsukov's avatar
zbytes  
Vladimir Barsukov committed
42
43
44
45
46
	z.Mu.Lock()
	defer z.Mu.Unlock()
	return z.Buff.Bytes()
}

Vladimir Barsukov's avatar
fix    
Vladimir Barsukov committed
47
func (z *Buffer) String() string {
Vladimir Barsukov's avatar
zbytes  
Vladimir Barsukov committed
48
49
50
51
52
	z.Mu.Lock()
	defer z.Mu.Unlock()
	return z.Buff.String()
}

Vladimir Barsukov's avatar
fix    
Vladimir Barsukov committed
53
func (z *Buffer) Reset() {
Vladimir Barsukov's avatar
zbytes  
Vladimir Barsukov committed
54
55
56
57
	z.Mu.Lock()
	defer z.Mu.Unlock()
	z.Buff.Reset()
}
Vladimir Barsukov's avatar
fix    
Vladimir Barsukov committed
58
func (z *Buffer) Len() int {
Vladimir Barsukov's avatar
zbytes  
Vladimir Barsukov committed
59
60
61
62
	z.Mu.Lock()
	defer z.Mu.Unlock()
	return z.Buff.Len()
}