github.com/iikira/iikira-go-utils@v0.0.0-20230610031953-f2cb11cde33a/requester/rio/multi.go (about) 1 package rio 2 3 import ( 4 "io" 5 ) 6 7 // MultiReaderLen 合并多个ReaderLen 8 func MultiReaderLen(readerLens ...ReaderLen) ReaderLen { 9 // TODO: 和copy对比 10 r := make([]io.Reader, 0, len(readerLens)) 11 for k := range readerLens { 12 if readerLens[k] == nil { 13 continue 14 } 15 r = append(r, readerLens[k]) 16 } 17 return &multiReaderLen{ 18 mrls: readerLens, 19 multiReader: io.MultiReader(r...), 20 } 21 } 22 23 type multiReaderLen struct { 24 mrls []ReaderLen 25 multiReader io.Reader 26 } 27 28 func (mrl *multiReaderLen) Read(p []byte) (n int, err error) { 29 return mrl.multiReader.Read(p) 30 } 31 32 func (mrl *multiReaderLen) Len() int { 33 var i int 34 for _, v := range mrl.mrls { 35 i += v.Len() 36 } 37 return i 38 } 39 40 // MultiReaderLen64 合并多个ReaderLen64 41 func MultiReaderLen64(readerLen64s ...ReaderLen64) ReaderLen64 { 42 // TODO: 和copy对比 43 r := make([]io.Reader, 0, len(readerLen64s)) 44 for k := range readerLen64s { 45 if readerLen64s[k] == nil { 46 continue 47 } 48 r = append(r, readerLen64s[k]) 49 } 50 return &multiReaderLen64{ 51 mrl64s: readerLen64s, 52 multiReader: io.MultiReader(r...), 53 } 54 } 55 56 type multiReaderLen64 struct { 57 mrl64s []ReaderLen64 58 multiReader io.Reader 59 } 60 61 func (mrl64 *multiReaderLen64) Read(p []byte) (n int, err error) { 62 return mrl64.multiReader.Read(p) 63 } 64 65 func (mrl64 *multiReaderLen64) Len() int64 { 66 var l int64 67 for _, v := range mrl64.mrl64s { 68 l += v.Len() 69 } 70 return l 71 }