2019-06-13 17:05:52 +03:00
|
|
|
/*
|
|
|
|
NAME
|
|
|
|
lex.go
|
|
|
|
|
|
|
|
AUTHOR
|
|
|
|
Trek Hopton <trek@ausocean.org>
|
|
|
|
|
|
|
|
LICENSE
|
|
|
|
This file is Copyright (C) 2019 the Australian Ocean Lab (AusOcean)
|
|
|
|
|
|
|
|
It is free software: you can redistribute it and/or modify them
|
|
|
|
under the terms of the GNU General Public License as published by the
|
|
|
|
Free Software Foundation, either version 3 of the License, or (at your
|
|
|
|
option) any later version.
|
|
|
|
|
|
|
|
It is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License in gpl.txt.
|
|
|
|
If not, see [GNU licenses](http://www.gnu.org/licenses).
|
|
|
|
*/
|
|
|
|
|
2019-06-03 20:01:35 +03:00
|
|
|
package codecutil
|
|
|
|
|
|
|
|
import (
|
2019-06-14 13:19:49 +03:00
|
|
|
"fmt"
|
2019-06-03 20:01:35 +03:00
|
|
|
"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 {
|
2019-06-14 13:19:49 +03:00
|
|
|
if n <= 0 {
|
|
|
|
return fmt.Errorf("invalid buffer size: %v", n)
|
|
|
|
}
|
|
|
|
if t < 0 {
|
|
|
|
return fmt.Errorf("invalid delay: %v", t)
|
|
|
|
}
|
2019-06-03 20:01:35 +03:00
|
|
|
var tick <-chan time.Time
|
2019-06-14 13:19:49 +03:00
|
|
|
if t > 0 {
|
2019-06-03 20:01:35 +03:00
|
|
|
ticker := time.NewTicker(t)
|
|
|
|
defer ticker.Stop()
|
|
|
|
tick = ticker.C
|
|
|
|
}
|
|
|
|
|
2019-06-14 13:19:49 +03:00
|
|
|
buf := make([]byte, n)
|
2019-06-03 20:01:35 +03:00
|
|
|
for {
|
2019-06-14 13:19:49 +03:00
|
|
|
if t != 0 {
|
|
|
|
<-tick
|
|
|
|
}
|
|
|
|
off, err := src.Read(buf)
|
2019-06-03 20:01:35 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-06-14 13:19:49 +03:00
|
|
|
_, err = dst.Write(buf[:off])
|
2019-06-03 20:01:35 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|