File: helpers_test.go

package info (click to toggle)
golang-github-datadog-zstd 1.4.5%2Bpatch1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 164 kB
  • sloc: sh: 9; makefile: 2
file content (45 lines) | stat: -rw-r--r-- 895 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package zstd

/*
From https://github.com/dustin/randbo
All credits for the code below goes there :) (There wasn't a license so I'm distributing as is)
*/

import (
	"io"
	"math/rand"
	"time"
)

// randbytes creates a stream of non-crypto quality random bytes
type randbytes struct {
	rand.Source
}

// NewRandBytes creates a new random reader with a time source.
func NewRandBytes() io.Reader {
	return NewRandBytesFrom(rand.NewSource(time.Now().UnixNano()))
}

// NewRandBytesFrom creates a new reader from your own rand.Source
func NewRandBytesFrom(src rand.Source) io.Reader {
	return &randbytes{src}
}

// Read satisfies io.Reader
func (r *randbytes) Read(p []byte) (n int, err error) {
	todo := len(p)
	offset := 0
	for {
		val := int64(r.Int63())
		for i := 0; i < 8; i++ {
			p[offset] = byte(val)
			todo--
			if todo == 0 {
				return len(p), nil
			}
			offset++
			val >>= 8
		}
	}
}