read.go 860 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package yu_io
  2. import (
  3. "io"
  4. )
  5. // ReadAll
  6. //
  7. // @Description: 从流中读取所有数据
  8. func ReadAll(r io.Reader) []byte {
  9. j_buf := make([]byte, 0, 8192)
  10. for {
  11. if len(j_buf) == cap(j_buf) {
  12. // Add more capacity (let append pick how much).
  13. j_buf = append(j_buf, 0)[:len(j_buf)]
  14. }
  15. n, err := r.Read(j_buf[len(j_buf):cap(j_buf)])
  16. j_buf = j_buf[:len(j_buf)+n]
  17. if err != nil {
  18. return j_buf
  19. }
  20. }
  21. }
  22. // ReadAllS
  23. //
  24. // @Description: 从流中读取所有数据
  25. func ReadAllS(r io.Reader) string {
  26. return string(ReadAll(r))
  27. }
  28. // ReadAllClose
  29. //
  30. // @Description: 从流中读取所有数据并关闭流
  31. func ReadAllClose(r io.ReadCloser) []byte {
  32. defer r.Close()
  33. return ReadAll(r)
  34. }
  35. // ReadAllSClose
  36. //
  37. // @Description: 从流中读取所有数据并关闭流
  38. func ReadAllSClose(r io.ReadCloser) string {
  39. return string(ReadAllClose(r))
  40. }