forked from TrueCloudLab/restic
Improve RandomReader
This commit is contained in:
parent
e7bf936d2b
commit
e689d499e7
1 changed files with 16 additions and 3 deletions
|
@ -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.
|
||||||
|
|
Loading…
Reference in a new issue