amuz.es/src/infra/goutils@v0.1.3/buf/atomic_buf.go (about) 1 package buf 2 3 import ( 4 "bytes" 5 "sync" 6 "io" 7 ) 8 9 type AtomicBuffers interface { 10 Bytes() []byte 11 Cap() int 12 Grow(int) 13 Len() int 14 Next(int) []byte 15 ReadByte() (byte, error) 16 ReadBytes(byte) ([]byte, error) 17 ReadFrom(io.Reader) (int64, error) 18 ReadRune() (rune, int, error) 19 ReadString(byte) (string, error) 20 Reset() 21 Truncate(int) 22 UnreadByte() error 23 UnreadRune() error 24 WriteByte(byte) error 25 WriteRune(rune) (int, error) 26 WriteString(string) (int, error) 27 WriteTo(io.Writer) (int64, error) 28 Read([]byte) (int, error) 29 Write([]byte) (int, error) 30 String() string 31 } 32 33 type atomicBuffer struct { 34 b bytes.Buffer 35 m sync.RWMutex 36 } 37 38 func (b *atomicBuffer) Bytes() []byte { 39 b.m.RLock() 40 defer b.m.RUnlock() 41 return b.b.Bytes() 42 } 43 func (b *atomicBuffer) Cap() int { 44 b.m.RLock() 45 defer b.m.RUnlock() 46 return b.b.Cap() 47 } 48 func (b *atomicBuffer) Grow(n int) { 49 b.m.Lock() 50 defer b.m.Unlock() 51 b.b.Grow(n) 52 } 53 func (b *atomicBuffer) Len() int { 54 b.m.RLock() 55 defer b.m.RUnlock() 56 return b.b.Len() 57 } 58 func (b *atomicBuffer) Next(n int) []byte { 59 b.m.Lock() 60 defer b.m.Unlock() 61 return b.b.Next(n) 62 } 63 func (b *atomicBuffer) ReadByte() (c byte, err error) { 64 b.m.Lock() 65 defer b.m.Unlock() 66 return b.b.ReadByte() 67 } 68 func (b *atomicBuffer) ReadBytes(delim byte) (line []byte, err error) { 69 b.m.Lock() 70 defer b.m.Unlock() 71 return b.b.ReadBytes(delim) 72 } 73 func (b *atomicBuffer) ReadFrom(r io.Reader) (n int64, err error) { 74 b.m.Lock() 75 defer b.m.Unlock() 76 return b.b.ReadFrom(r) 77 } 78 func (b *atomicBuffer) ReadRune() (r rune, size int, err error) { 79 b.m.Lock() 80 defer b.m.Unlock() 81 return b.b.ReadRune() 82 } 83 func (b *atomicBuffer) ReadString(delim byte) (line string, err error) { 84 b.m.Lock() 85 defer b.m.Unlock() 86 return b.b.ReadString(delim) 87 } 88 func (b *atomicBuffer) Reset() { 89 b.m.Lock() 90 defer b.m.Unlock() 91 b.b.Reset() 92 } 93 func (b *atomicBuffer) Truncate(n int) { 94 b.m.Lock() 95 defer b.m.Unlock() 96 b.b.Truncate(n) 97 } 98 func (b *atomicBuffer) UnreadByte() error { 99 b.m.Lock() 100 defer b.m.Unlock() 101 return b.b.UnreadByte() 102 } 103 func (b *atomicBuffer) UnreadRune() error { 104 b.m.Lock() 105 defer b.m.Unlock() 106 return b.b.UnreadRune() 107 } 108 func (b *atomicBuffer) WriteByte(c byte) error { 109 b.m.Lock() 110 defer b.m.Unlock() 111 return b.b.WriteByte(c) 112 } 113 func (b *atomicBuffer) WriteRune(r rune) (n int, err error) { 114 b.m.Lock() 115 defer b.m.Unlock() 116 return b.b.WriteRune(r) 117 } 118 func (b *atomicBuffer) WriteString(s string) (n int, err error) { 119 b.m.Lock() 120 defer b.m.Unlock() 121 return b.b.WriteString(s) 122 } 123 func (b *atomicBuffer) WriteTo(w io.Writer) (n int64, err error) { 124 b.m.Lock() 125 defer b.m.Unlock() 126 return b.b.WriteTo(w) 127 } 128 129 func (b *atomicBuffer) Read(p []byte) (n int, err error) { 130 b.m.Lock() 131 defer b.m.Unlock() 132 return b.b.Read(p) 133 } 134 func (b *atomicBuffer) Write(p []byte) (n int, err error) { 135 b.m.Lock() 136 defer b.m.Unlock() 137 return b.b.Write(p) 138 } 139 func (b *atomicBuffer) String() string { 140 b.m.RLock() 141 defer b.m.RUnlock() 142 return b.b.String() 143 }