diff options
Diffstat (limited to 'xdelta3/go/src/xdelta/rstream.go')
-rw-r--r-- | xdelta3/go/src/xdelta/rstream.go | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/xdelta3/go/src/xdelta/rstream.go b/xdelta3/go/src/xdelta/rstream.go new file mode 100644 index 0000000..3e7265f --- /dev/null +++ b/xdelta3/go/src/xdelta/rstream.go | |||
@@ -0,0 +1,60 @@ | |||
1 | package xdelta | ||
2 | |||
3 | |||
4 | import ( | ||
5 | "io" | ||
6 | "math/rand" | ||
7 | ) | ||
8 | |||
9 | const ( | ||
10 | blocksize = 16380 | ||
11 | ) | ||
12 | |||
13 | func WriteRstreams(t *TestGroup, seed, offset, len int64, | ||
14 | first, second io.WriteCloser) { | ||
15 | go writeOne(t, seed, 0, len, first) | ||
16 | go writeOne(t, seed, offset, len, second) | ||
17 | } | ||
18 | |||
19 | func writeOne(t *TestGroup, seed, offset, len int64, stream io.WriteCloser) error { | ||
20 | t.WaitGroup.Add(1) | ||
21 | if offset != 0 { | ||
22 | // Fill with other random data until the offset | ||
23 | if err := writeRand(rand.New(rand.NewSource(^seed)), offset, stream); err != nil { | ||
24 | return err | ||
25 | } | ||
26 | } | ||
27 | if err := writeRand(rand.New(rand.NewSource(seed)), | ||
28 | len - offset, stream); err != nil { | ||
29 | return err | ||
30 | } | ||
31 | t.WaitGroup.Done() | ||
32 | return stream.Close() | ||
33 | } | ||
34 | |||
35 | func writeRand(r *rand.Rand, len int64, s io.Writer) error { | ||
36 | blk := make([]byte, blocksize) | ||
37 | for len > 0 { | ||
38 | fillRand(r, blk) | ||
39 | c := blocksize | ||
40 | if len < blocksize { | ||
41 | c = int(len) | ||
42 | } | ||
43 | if _, err := s.Write(blk[0:c]); err != nil { | ||
44 | return err | ||
45 | } | ||
46 | len -= int64(c) | ||
47 | } | ||
48 | return nil | ||
49 | } | ||
50 | |||
51 | func fillRand(r *rand.Rand, blk []byte) { | ||
52 | for p := 0; p < blocksize; { | ||
53 | v := r.Int63() | ||
54 | for i := 7; i != 0; i-- { | ||
55 | blk[p] = byte(v) | ||
56 | p++ | ||
57 | v >>= 8 | ||
58 | } | ||
59 | } | ||
60 | } | ||