github.com/hamba/avro@v1.8.0/internal/bytesx/reset.go (about)

     1  // Package bytesx implements bytes extensions.
     2  package bytesx
     3  
     4  import "io"
     5  
     6  // ResetReader implements the io.Reader reading from a resettable byte slice.
     7  type ResetReader struct {
     8  	buf  []byte
     9  	head int
    10  	tail int
    11  }
    12  
    13  // NewResetReader returns a new Reader reading from b.
    14  func NewResetReader(b []byte) *ResetReader {
    15  	r := &ResetReader{}
    16  	r.Reset(b)
    17  
    18  	return r
    19  }
    20  
    21  // Read reads bytes into p.
    22  func (r *ResetReader) Read(p []byte) (int, error) {
    23  	if r.head == r.tail {
    24  		return 0, io.EOF
    25  	}
    26  
    27  	n := copy(p, r.buf[r.head:])
    28  	r.head += n
    29  
    30  	return n, nil
    31  }
    32  
    33  // Reset resets the byte slice being read from.
    34  func (r *ResetReader) Reset(b []byte) {
    35  	r.buf = b
    36  	r.head = 0
    37  	r.tail = len(b)
    38  }