2
2
mirror of https://github.com/octoleo/restic.git synced 2024-12-24 11:55:28 +00:00

Improve RandomReader

This commit is contained in:
Alexander Neumann 2016-01-17 19:46:48 +01:00
parent e7bf936d2b
commit e689d499e7

View File

@ -1,7 +1,6 @@
package test_helper package test_helper
import ( import (
"bytes"
"compress/bzip2" "compress/bzip2"
"compress/gzip" "compress/gzip"
"crypto/rand" "crypto/rand"
@ -86,10 +85,24 @@ func Random(seed, count int) []byte {
return buf return buf
} }
type rndReader struct {
src *mrand.Rand
}
func (r *rndReader) Read(p []byte) (int, error) {
fmt.Printf("Read(%v)\n", len(p))
for i := range p {
p[i] = byte(r.src.Uint32())
}
return len(p), nil
}
// RandomReader returns a reader that returns size bytes of pseudo-random data // RandomReader returns a reader that returns size bytes of pseudo-random data
// derived from the seed. // derived from the seed.
func RandomReader(seed, size int) *bytes.Reader { func RandomReader(seed, size int) io.Reader {
return bytes.NewReader(Random(seed, size)) r := &rndReader{src: mrand.New(mrand.NewSource(int64(seed)))}
return io.LimitReader(r, int64(size))
} }
// GenRandom returns a []byte filled with up to 1000 random bytes. // GenRandom returns a []byte filled with up to 1000 random bytes.