github.com/ssdev-go/moby@v17.12.1-ce-rc2+incompatible/pkg/ioutils/buffer.go (about) 1 package ioutils 2 3 import ( 4 "errors" 5 "io" 6 ) 7 8 var errBufferFull = errors.New("buffer is full") 9 10 type fixedBuffer struct { 11 buf []byte 12 pos int 13 lastRead int 14 } 15 16 func (b *fixedBuffer) Write(p []byte) (int, error) { 17 n := copy(b.buf[b.pos:cap(b.buf)], p) 18 b.pos += n 19 20 if n < len(p) { 21 if b.pos == cap(b.buf) { 22 return n, errBufferFull 23 } 24 return n, io.ErrShortWrite 25 } 26 return n, nil 27 } 28 29 func (b *fixedBuffer) Read(p []byte) (int, error) { 30 n := copy(p, b.buf[b.lastRead:b.pos]) 31 b.lastRead += n 32 return n, nil 33 } 34 35 func (b *fixedBuffer) Len() int { 36 return b.pos - b.lastRead 37 } 38 39 func (b *fixedBuffer) Cap() int { 40 return cap(b.buf) 41 } 42 43 func (b *fixedBuffer) Reset() { 44 b.pos = 0 45 b.lastRead = 0 46 b.buf = b.buf[:0] 47 } 48 49 func (b *fixedBuffer) String() string { 50 return string(b.buf[b.lastRead:b.pos]) 51 }