github.com/songzhibin97/gkit@v1.2.13/cache/buffer/iobuffer_.go (about)

     1  package buffer
     2  
     3  import "io"
     4  
     5  // IoBuffer 接口定义
     6  type IoBuffer interface {
     7  	// Read reads the next len(p) bytes from the buffer or until the buffer
     8  	// is drained. The return value n is the number of bytes read. If the
     9  	// buffer has no data to return, err is io.EOF (unless len(p) is zero);
    10  	// otherwise it is nil.
    11  	Read(p []byte) (n int, err error)
    12  
    13  	// ReadOnce make a one-shot read and appends it to the buffer, growing
    14  	// the buffer as needed. The return value n is the number of bytes read. Any
    15  	// error except io.EOF encountered during the read is also returned. If the
    16  	// buffer becomes too large, ReadFrom will panic with ErrTooLarge.
    17  	ReadOnce(r io.Reader) (n int64, err error)
    18  
    19  	// ReadFrom reads data from r until EOF and appends it to the buffer, growing
    20  	// the buffer as needed. The return value n is the number of bytes read. Any
    21  	// error except io.EOF encountered during the read is also returned. If the
    22  	// buffer becomes too large, ReadFrom will panic with ErrTooLarge.
    23  	ReadFrom(r io.Reader) (n int64, err error)
    24  
    25  	// Grow updates the length of the buffer by n, growing the buffer as
    26  	// needed. The return value n is the length of p; err is always nil. If the
    27  	// buffer becomes too large, Write will panic with ErrTooLarge.
    28  	Grow(n int) error
    29  
    30  	// Write appends the contents of p to the buffer, growing the buffer as
    31  	// needed. The return value n is the length of p; err is always nil. If the
    32  	// buffer becomes too large, Write will panic with ErrTooLarge.
    33  	Write(p []byte) (n int, err error)
    34  
    35  	// WriteString appends the string to the buffer, growing the buffer as
    36  	// needed. The return value n is the length of s; err is always nil. If the
    37  	// buffer becomes too large, Write will panic with ErrTooLarge.
    38  	WriteString(s string) (n int, err error)
    39  
    40  	// WriteByte appends the byte to the buffer, growing the buffer as
    41  	// needed. The return value n is the length of s; err is always nil. If the
    42  	// buffer becomes too large, Write will panic with ErrTooLarge.
    43  	WriteByte(p byte) error
    44  
    45  	// WriteUint16 appends the uint16 to the buffer, growing the buffer as
    46  	// needed. The return value n is the length of s; err is always nil. If the
    47  	// buffer becomes too large, Write will panic with ErrTooLarge.
    48  	WriteUint16(p uint16) error
    49  
    50  	// WriteUint32 appends the uint32 to the buffer, growing the buffer as
    51  	// needed. The return value n is the length of s; err is always nil. If the
    52  	// buffer becomes too large, Write will panic with ErrTooLarge.
    53  	WriteUint32(p uint32) error
    54  
    55  	// WriteUint64 appends the uint64 to the buffer, growing the buffer as
    56  	// needed. The return value n is the length of s; err is always nil. If the
    57  	// buffer becomes too large, Write will panic with ErrTooLarge.
    58  	WriteUint64(p uint64) error
    59  
    60  	// WriteTo writes data to w until the buffer is drained or an error occurs.
    61  	// The return value n is the number of bytes written; it always fits into an
    62  	// int, but it is int64 to match the io.WriterTo interface. Any error
    63  	// encountered during the write is also returned.
    64  	WriteTo(w io.Writer) (n int64, err error)
    65  
    66  	// Peek returns n bytes from buffer, without draining any buffered data.
    67  	// If n > readable buffer, nil will be returned.
    68  	// It can be used in codec to check first-n-bytes magic bytes
    69  	// Note: do not change content in return bytes, use write instead
    70  	Peek(n int) []byte
    71  
    72  	// Bytes returns all bytes from buffer, without draining any buffered data.
    73  	// It can be used to get fixed-length content, such as headers, body.
    74  	// Note: do not change content in return bytes, use write instead
    75  	Bytes() []byte
    76  
    77  	// Drain drains a offset length of bytes in buffer.
    78  	// It can be used with Bytes(), after consuming a fixed-length of data
    79  	Drain(offset int)
    80  
    81  	// Len returns the number of bytes of the unread portion of the buffer;
    82  	// b.Len() == len(b.Bytes()).
    83  	Len() int
    84  
    85  	// Cap returns the capacity of the buffer's underlying byte slice, that is, the
    86  	// total space allocated for the buffer's data.
    87  	Cap() int
    88  
    89  	// Reset resets the buffer to be empty,
    90  	// but it retains the underlying storage for use by future writes.
    91  	Reset()
    92  
    93  	// Clone makes a copy of IoBuffer struct
    94  	Clone() IoBuffer
    95  
    96  	// String returns the contents of the unread portion of the buffer
    97  	// as a string. If the Buffer is a nil pointer, it returns "<nil>".
    98  	String() string
    99  
   100  	// Alloc alloc bytes from BytePoolBuffer
   101  	Alloc(int)
   102  
   103  	// Free free bytes to BytePoolBuffer
   104  	Free()
   105  
   106  	// Count sets and returns reference count
   107  	Count(int32) int32
   108  
   109  	// EOF returns whether Io is EOF on the connection
   110  	EOF() bool
   111  
   112  	// SetEOF sets the IoBuffer EOF
   113  	SetEOF(eof bool)
   114  	Append(data []byte) error
   115  	CloseWithError(err error)
   116  }