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"
|
|
|
|
)
|
|
|
|
|
2019-06-18 10:54:32 +03:00
|
|
|
// ByteLexer is used to lex bytes using a buffer size which is configured upon construction.
|
2019-06-18 08:20:36 +03:00
|
|
|
type ByteLexer struct {
|
2019-11-13 12:26:13 +03:00
|
|
|
bufSize int
|
2019-06-18 08:20:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewByteLexer returns a pointer to a ByteLexer with the given buffer size.
|
2019-11-13 12:26:13 +03:00
|
|
|
func NewByteLexer(s int) (*ByteLexer, error) {
|
|
|
|
if s <= 0 {
|
|
|
|
return nil, fmt.Errorf("invalid buffer size: %v", s)
|
|
|
|
}
|
|
|
|
return &ByteLexer{bufSize: s}, nil
|
2019-06-18 08:20:36 +03:00
|
|
|
}
|
|
|
|
|
2019-07-15 11:25:42 +03:00
|
|
|
// zeroTicks can be used to create an instant ticker.
|
|
|
|
var zeroTicks chan time.Time
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
zeroTicks = make(chan time.Time)
|
|
|
|
close(zeroTicks)
|
|
|
|
}
|
|
|
|
|
2019-11-13 12:26:13 +03:00
|
|
|
// Lex reads l.bufSize bytes from src and writes them to dst every d seconds.
|
2019-07-15 11:25:42 +03:00
|
|
|
func (l *ByteLexer) Lex(dst io.Writer, src io.Reader, d time.Duration) error {
|
|
|
|
if d < 0 {
|
|
|
|
return fmt.Errorf("invalid delay: %v", d)
|
2019-06-03 20:01:35 +03:00
|
|
|
}
|
|
|
|
|
2019-08-02 16:56:15 +03:00
|
|
|
var ticker *time.Ticker
|
|
|
|
if d == 0 {
|
|
|
|
ticker = &time.Ticker{C: zeroTicks}
|
|
|
|
} else {
|
|
|
|
ticker = time.NewTicker(d)
|
|
|
|
defer ticker.Stop()
|
|
|
|
}
|
|
|
|
|
2019-11-13 12:26:13 +03:00
|
|
|
buf := make([]byte, l.bufSize)
|
2019-06-03 20:01:35 +03:00
|
|
|
for {
|
2019-07-15 08:47:16 +03:00
|
|
|
<-ticker.C
|
2019-06-14 13:19:49 +03:00
|
|
|
off, err := src.Read(buf)
|
2022-02-18 02:40:01 +03:00
|
|
|
// The only error that will stop the lexer is an EOF.
|
|
|
|
if err == io.EOF {
|
2019-06-03 20:01:35 +03:00
|
|
|
return err
|
2022-02-18 02:40:01 +03:00
|
|
|
} else if err != nil {
|
|
|
|
continue
|
2019-06-03 20:01:35 +03:00
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|