bufconn_unsafe.go 882 B

12345678910111213141516171819202122232425262728293031323334
  1. package net
  2. import (
  3. "io"
  4. "unsafe"
  5. )
  6. // bufioReader copy from stdlib bufio/bufio.go
  7. // This structure has remained unchanged from go1.5 to go1.21.
  8. type bufioReader struct {
  9. buf []byte
  10. rd io.Reader // reader provided by the client
  11. r, w int // buf read and write positions
  12. err error
  13. lastByte int // last byte read for UnreadByte; -1 means invalid
  14. lastRuneSize int // size of last rune read for UnreadRune; -1 means invalid
  15. }
  16. func (c *BufferedConn) AppendData(buf []byte) (ok bool) {
  17. b := (*bufioReader)(unsafe.Pointer(c.r))
  18. pos := len(b.buf) - b.w - len(buf)
  19. if pos >= -b.r { // len(b.buf)-(b.w - b.r) >= len(buf)
  20. if pos < 0 { // len(b.buf)-b.w < len(buf)
  21. // Slide existing data to beginning.
  22. copy(b.buf, b.buf[b.r:b.w])
  23. b.w -= b.r
  24. b.r = 0
  25. }
  26. b.w += copy(b.buf[b.w:], buf)
  27. return true
  28. }
  29. return false
  30. }