1
1
mirror of https://github.com/wader/fq.git synced 2024-12-24 13:52:02 +03:00
fq/internal/ioextra/ioextra.go

57 lines
931 B
Go
Raw Normal View History

2020-06-08 03:29:51 +03:00
package ioextra
import (
"context"
2020-06-08 03:29:51 +03:00
"errors"
"io"
)
func SeekerEnd(s io.Seeker) (int64, error) {
cPos, err := s.Seek(0, io.SeekCurrent)
if err != nil {
return 0, err
}
epos, err := s.Seek(0, io.SeekEnd)
if err != nil {
return 0, err
}
if _, err := s.Seek(cPos, io.SeekStart); err != nil {
return 0, err
}
return epos, nil
}
2021-09-20 18:05:24 +03:00
type ReadErrSeeker struct{ io.Reader }
2020-06-08 03:29:51 +03:00
2021-09-20 18:05:24 +03:00
func (r *ReadErrSeeker) Seek(offset int64, whence int) (int64, error) {
return 0, errors.New("seek")
}
type CtxWriter struct {
io.Writer
Ctx context.Context
}
func (o CtxWriter) Write(p []byte) (n int, err error) {
if o.Ctx != nil {
if err := o.Ctx.Err(); err != nil {
return 0, err
}
}
return o.Writer.Write(p)
}
type DiscardCtxWriter struct {
Ctx context.Context
}
func (o DiscardCtxWriter) Write(p []byte) (n int, err error) {
if o.Ctx != nil {
if err := o.Ctx.Err(); err != nil {
return 0, err
}
}
return n, nil
}