"Fossies" - the Fresh Open Source Software Archive

Member "xdelta3-3.1.0/go/src/xdelta/rstream.go" (19 Dec 2015, 1503 Bytes) of package /linux/misc/xdelta3-3.1.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Go source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 package xdelta
    2 
    3 
    4 import (
    5     "io"
    6     "math/rand"
    7 )
    8 
    9 const (
   10     blocksize = 1<<17
   11 )
   12 
   13 func (t *TestGroup) WriteRstreams(desc string, seed, offset, len int64,
   14     src, tgt io.WriteCloser) {
   15     t.Go("src-write:"+desc, func (g *Goroutine) {
   16         writeOne(g, seed, 0, len, tgt, false)
   17     })
   18     t.Go("tgt-write:"+desc, func (g *Goroutine) {
   19         writeOne(g, seed, offset, len, src, true)
   20     })
   21 }
   22 
   23 func writeOne(g *Goroutine, seed, offset, len int64, stream io.WriteCloser, readall bool) {
   24     if !readall {
   25         // Allow the source-read to fail or block until the process terminates.
   26         // This behavior is reserved for the decoder, which is not required to
   27         // read the entire source.
   28         g.OK()
   29     }
   30     if offset != 0 {
   31         // Fill with other random data until the offset
   32         if err := writeRand(g, rand.New(rand.NewSource(^seed)), offset, stream); err != nil {
   33             g.Panic(err)
   34         }
   35     }
   36     if err := writeRand(g, rand.New(rand.NewSource(seed)),
   37         len - offset, stream); err != nil {
   38         g.Panic(err)
   39     }
   40     if err := stream.Close(); err != nil {
   41         g.Panic(err)
   42     }
   43     g.OK()
   44 }
   45 
   46 func writeRand(g *Goroutine, r *rand.Rand, len int64, s io.Writer) error {
   47     blk := make([]byte, blocksize)
   48     for len > 0 {
   49         fillRand(r, blk)
   50         c := blocksize
   51         if len < blocksize {
   52             c = int(len)
   53         }
   54         if _, err := s.Write(blk[0:c]); err != nil {
   55             return err
   56         }
   57         len -= int64(c)
   58     }
   59     return nil
   60 }
   61 
   62 func fillRand(r *rand.Rand, blk []byte) {
   63     for p := 0; p < len(blk); {
   64         v := r.Int63()
   65         for i := 7; i != 0 && p < len(blk); i-- {
   66             blk[p] = byte(v)
   67             p++
   68             v >>= 8
   69         }
   70     }
   71 }