av/codec/codecutil/lex.go

32 lines
520 B
Go
Raw Normal View History

package codecutil
import (
"io"
"time"
)
// LexBytes reads n bytes from src and writes them to dst every t seconds.
func LexBytes(dst io.Writer, src io.Reader, t time.Duration, n int) error {
var tick <-chan time.Time
if t == 0 {
tick = make(chan time.Time)
} else {
ticker := time.NewTicker(t)
defer ticker.Stop()
tick = ticker.C
}
for {
<-tick
buf := make([]byte, n)
_, err := src.Read(buf)
if err != nil {
return err
}
_, err = dst.Write(buf)
if err != nil {
return err
}
}
}