github.com/tickstep/library-go@v0.1.1/requester/rio/buf.go (about)

     1  package rio
     2  
     3  import (
     4  	"unsafe"
     5  )
     6  
     7  // Buffer 为固定长度的 Buf, 实现 io.WriterAt 接口
     8  type Buffer struct {
     9  	Buf []byte
    10  }
    11  
    12  // NewBuffer 初始化 Buffer
    13  func NewBuffer(buf []byte) *Buffer {
    14  	return &Buffer{
    15  		Buf: buf,
    16  	}
    17  }
    18  
    19  // ReadAt 实现 io.ReadAt 接口
    20  // 不进行越界检查
    21  func (b *Buffer) ReadAt(p []byte, off int64) (n int, err error) {
    22  	n = copy(p, b.Buf[off:])
    23  	return n, nil
    24  }
    25  
    26  // WriteAt 实现 io.WriterAt 接口
    27  // 不进行越界检查
    28  func (b *Buffer) WriteAt(p []byte, off int64) (n int, err error) {
    29  	n = copy(b.Buf[off:], p)
    30  	return n, nil
    31  }
    32  
    33  // Bytes 返回 buf
    34  func (b *Buffer) Bytes() []byte {
    35  	return b.Buf
    36  }
    37  
    38  func (b *Buffer) String() string {
    39  	return *(*string)(unsafe.Pointer(&b.Buf))
    40  }