refactor(countrw): move to iout

This commit is contained in:
sentriz
2022-04-12 23:25:54 +01:00
parent d7655cb9d1
commit fd211d706a
6 changed files with 132 additions and 27 deletions

23
iout/copy_range.go Normal file
View File

@@ -0,0 +1,23 @@
package iout
import (
"errors"
"fmt"
"io"
)
func CopyRange(w io.Writer, r io.Reader, start, length int64) error {
if _, err := io.CopyN(io.Discard, r, start); err != nil && !errors.Is(err, io.EOF) {
return fmt.Errorf("discard %d: %w", start, err)
}
if length == 0 {
if _, err := io.Copy(w, r); err != nil && !errors.Is(err, io.EOF) {
return fmt.Errorf("direct copy: %w", err)
}
return nil
}
if _, err := io.CopyN(w, io.MultiReader(r, NewNullReader()), length); err != nil && !errors.Is(err, io.EOF) {
return fmt.Errorf("copy %d: %w", length, err)
}
return nil
}