rm util.NewByteChanReader()
Henry committed
Mar 12, 2015 at 14:50 UTC
069b4b5c8e013c7119b6f2e4c8a75e69526d095d
2 files changed
-72
util/util.go
-34
@@ -62,40 +62,6 @@ func ExpandPathnames(paths []string) ([]string, error) {
62
return out, nil
63
}
64
65
-// byteChanReader wraps a byte chan in a reader
66
-type byteChanReader struct {
67
- in chan []byte
68
- buf []byte
69
-}
70
-
71
-func NewByteChanReader(in chan []byte) io.Reader {
72
- return &byteChanReader{in: in}
73
-}
74
-
75
-func (bcr *byteChanReader) Read(output []byte) (int, error) {
76
- remain := output
77
- remainLen := len(output)
78
- outputLen := 0
79
- more := false
80
- next := bcr.buf
81
-
82
- for {
83
- n := copy(remain, next)
84
- remainLen -= n
85
- outputLen += n
86
- if remainLen == 0 {
87
- bcr.buf = next[n:]
88
- return outputLen, nil
89
- }
90
-
91
- remain = remain[n:]
92
- next, more = <-bcr.in
93
- if !more {
94
- return outputLen, io.EOF
95
- }
96
- }
97
-}
98
-
65
type randGen struct {
66
rand.Rand
67
}
util/util_test.go
-38
@@ -2,7 +2,6 @@ package util
2
3
import (
4
"bytes"
5
- "math/rand"
5
"testing"
6
7
mh "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash"
@@ -28,43 +27,6 @@ func TestKey(t *testing.T) {
27
}
28
}
29
31
-func TestByteChanReader(t *testing.T) {
32
- if testing.Short() {
33
- t.SkipNow()
34
- }
35
-
36
- var data bytes.Buffer
37
- var data2 bytes.Buffer
38
- dch := make(chan []byte, 8)
39
- randr := NewTimeSeededRand()
40
-
41
- go func() {
42
- defer close(dch)
43
- for i := 0; i < rand.Intn(100)+100; i++ {
44
- chunk := make([]byte, rand.Intn(100000)+10)
45
- randr.Read(chunk)
46
- data.Write(chunk)
47
- dch <- chunk
48
- }
49
- }()
50
-
51
- read := NewByteChanReader(dch)
52
-
53
- // read in random, weird sizes to exercise saving buffer.
54
- for {
55
- buf := make([]byte, rand.Intn(10)*10)
56
- n, err := read.Read(buf)
57
- data2.Write(buf[:n])
58
- if err != nil {
59
- break
60
- }
61
- }
62
-
63
- if !bytes.Equal(data2.Bytes(), data.Bytes()) {
64
- t.Fatal("Reader failed to stream correct bytes")
65
- }
66
-}
67
-
30
func TestXOR(t *testing.T) {
31
cases := [][3][]byte{
32
[3][]byte{