You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

join.go 1.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package io
  2. import (
  3. "io"
  4. "gitlink.org.cn/cloudream/common/utils/lo"
  5. "gitlink.org.cn/cloudream/common/utils/math"
  6. )
  7. func Join(strs []io.Reader) io.ReadCloser {
  8. pr, pw := io.Pipe()
  9. go func() {
  10. var closeErr error
  11. buf := make([]byte, 4096)
  12. outer:
  13. for _, str := range strs {
  14. for {
  15. bufLen := len(buf)
  16. if bufLen == 0 {
  17. break outer
  18. }
  19. rd, err := str.Read(buf[:bufLen])
  20. if err != nil {
  21. if err != io.EOF {
  22. closeErr = err
  23. break outer
  24. }
  25. err = WriteAll(pw, buf[:rd])
  26. if err != nil {
  27. closeErr = err
  28. break outer
  29. }
  30. break
  31. }
  32. err = WriteAll(pw, buf[:rd])
  33. if err != nil {
  34. closeErr = err
  35. break outer
  36. }
  37. }
  38. }
  39. pw.CloseWithError(closeErr)
  40. }()
  41. return pr
  42. }
  43. type chunkedJoin struct {
  44. inputs []io.Reader
  45. chunkSize int
  46. currentInput int
  47. currentRead int
  48. err error
  49. }
  50. func (s *chunkedJoin) Read(buf []byte) (int, error) {
  51. if s.err != nil {
  52. return 0, s.err
  53. }
  54. if len(s.inputs) == 0 {
  55. return 0, io.EOF
  56. }
  57. bufLen := math.Min(math.Min(s.chunkSize, len(buf)), s.chunkSize-s.currentRead)
  58. rd, err := s.inputs[s.currentInput].Read(buf[:bufLen])
  59. if err == nil {
  60. s.currentRead += rd
  61. if s.currentRead == s.chunkSize {
  62. s.currentInput = (s.currentInput + 1) % len(s.inputs)
  63. s.currentRead = 0
  64. }
  65. return rd, nil
  66. }
  67. if err == io.EOF {
  68. s.inputs = lo.RemoveAt(s.inputs, s.currentInput)
  69. // 此处不需要+1
  70. if len(s.inputs) > 0 {
  71. s.currentInput = s.currentInput % len(s.inputs)
  72. s.currentRead = 0
  73. }
  74. return rd, nil
  75. }
  76. s.err = err
  77. return rd, err
  78. }
  79. func (s *chunkedJoin) Close() error {
  80. s.err = io.ErrClosedPipe
  81. return nil
  82. }
  83. func ChunkedJoin(inputs []io.Reader, chunkSize int) io.ReadCloser {
  84. return &chunkedJoin{
  85. inputs: inputs,
  86. chunkSize: chunkSize,
  87. }
  88. }