trash/trash.go

67 lines
1.2 KiB
Go
Raw Normal View History

2016-04-20 08:37:55 -07:00
// Package trash provides simple readers with meaningless output.
2015-08-19 22:14:35 -07:00
package trash
2016-04-20 08:37:55 -07:00
import (
"io"
"time"
)
2015-08-19 22:14:35 -07:00
func init() {
2016-06-02 23:16:06 -07:00
Reader = &reader{0xca}
Zeros = &reader{0x00}
Fs = &reader{0xff}
HiLo = &reader{0xaa}
LoHi = &reader{0x55}
2015-08-19 22:14:35 -07:00
}
// Reader provides a steady stream of trash (non-random bytes) when read from
var Reader io.Reader
2016-06-02 23:16:06 -07:00
// Zeros provides a steady stream of 0x00
var Zeros io.Reader
// Fs provides a steady stream of 0xff
var Fs io.Reader
// HiLo provides a steady stream of 0xaa
var HiLo io.Reader
// LoHi provides a steady stream of 0x55
var LoHi io.Reader
2016-04-20 08:37:55 -07:00
// TimeoutReader returns a reader that returns io.EOF after dur.
func TimeoutReader(dur time.Duration) io.Reader {
return &timeoutReader{timeout: time.Now().Add(dur)}
}
2016-06-02 23:16:06 -07:00
type reader struct {
pattern byte
}
2015-08-19 22:14:35 -07:00
func (r *reader) Read(p []byte) (int, error) {
c := 0
2016-04-20 08:37:55 -07:00
var err error
for i := 0; i < len(p); i++ {
c++
2016-06-02 23:16:06 -07:00
p[i] = r.pattern
2016-04-20 08:37:55 -07:00
}
return c, err
}
type timeoutReader struct {
timeout time.Time
}
func (tor *timeoutReader) Read(p []byte) (int, error) {
c := 0
var err error
2015-08-19 22:14:35 -07:00
for i := 0; i < len(p); i++ {
c++
p[i] = 0xca
}
2016-04-20 08:37:55 -07:00
if time.Now().After(tor.timeout) {
err = io.EOF
}
return c, err
2015-08-19 22:14:35 -07:00
}