github.com/Asutorufa/yuhaiin@v0.3.6-0.20240502055049-7984da7023a0/pkg/net/netapi/io.go (about) 1 package netapi 2 3 import ( 4 "io" 5 "unsafe" 6 ) 7 8 type Reader struct { 9 io.Reader 10 } 11 12 func NewReader(r io.Reader) *Reader { 13 return &Reader{r} 14 } 15 16 func (r *Reader) ReadByte() (byte, error) { 17 b := make([]byte, 1) 18 _, err := r.Read(b) 19 return b[0], err 20 } 21 22 func (r *Reader) ReadBytes(n int) ([]byte, error) { 23 b := make([]byte, n) 24 _, err := io.ReadFull(r, b) 25 return b, err 26 } 27 28 type Writer struct { 29 io.Writer 30 } 31 32 func NewWriter(w io.Writer) *Writer { 33 return &Writer{w} 34 } 35 36 func (w *Writer) WriteByte(b byte) error { 37 _, err := w.Write([]byte{b}) 38 return err 39 } 40 41 func (w *Writer) WriteString(b string) (int, error) { 42 return w.Write(unsafe.Slice(unsafe.StringData(b), len(b))) 43 }