mirror of
https://github.com/ipfs/kubo.git
synced 2026-03-02 06:47:51 +08:00
rm util.NewByteChanReader()
This commit is contained in:
parent
90d2113f99
commit
069b4b5c8e
34
util/util.go
34
util/util.go
@ -62,40 +62,6 @@ func ExpandPathnames(paths []string) ([]string, error) {
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// byteChanReader wraps a byte chan in a reader
|
||||
type byteChanReader struct {
|
||||
in chan []byte
|
||||
buf []byte
|
||||
}
|
||||
|
||||
func NewByteChanReader(in chan []byte) io.Reader {
|
||||
return &byteChanReader{in: in}
|
||||
}
|
||||
|
||||
func (bcr *byteChanReader) Read(output []byte) (int, error) {
|
||||
remain := output
|
||||
remainLen := len(output)
|
||||
outputLen := 0
|
||||
more := false
|
||||
next := bcr.buf
|
||||
|
||||
for {
|
||||
n := copy(remain, next)
|
||||
remainLen -= n
|
||||
outputLen += n
|
||||
if remainLen == 0 {
|
||||
bcr.buf = next[n:]
|
||||
return outputLen, nil
|
||||
}
|
||||
|
||||
remain = remain[n:]
|
||||
next, more = <-bcr.in
|
||||
if !more {
|
||||
return outputLen, io.EOF
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type randGen struct {
|
||||
rand.Rand
|
||||
}
|
||||
|
||||
@ -2,7 +2,6 @@ package util
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"math/rand"
|
||||
"testing"
|
||||
|
||||
mh "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash"
|
||||
@ -28,43 +27,6 @@ func TestKey(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestByteChanReader(t *testing.T) {
|
||||
if testing.Short() {
|
||||
t.SkipNow()
|
||||
}
|
||||
|
||||
var data bytes.Buffer
|
||||
var data2 bytes.Buffer
|
||||
dch := make(chan []byte, 8)
|
||||
randr := NewTimeSeededRand()
|
||||
|
||||
go func() {
|
||||
defer close(dch)
|
||||
for i := 0; i < rand.Intn(100)+100; i++ {
|
||||
chunk := make([]byte, rand.Intn(100000)+10)
|
||||
randr.Read(chunk)
|
||||
data.Write(chunk)
|
||||
dch <- chunk
|
||||
}
|
||||
}()
|
||||
|
||||
read := NewByteChanReader(dch)
|
||||
|
||||
// read in random, weird sizes to exercise saving buffer.
|
||||
for {
|
||||
buf := make([]byte, rand.Intn(10)*10)
|
||||
n, err := read.Read(buf)
|
||||
data2.Write(buf[:n])
|
||||
if err != nil {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if !bytes.Equal(data2.Bytes(), data.Bytes()) {
|
||||
t.Fatal("Reader failed to stream correct bytes")
|
||||
}
|
||||
}
|
||||
|
||||
func TestXOR(t *testing.T) {
|
||||
cases := [][3][]byte{
|
||||
[3][]byte{
|
||||
|
||||
Loading…
Reference in New Issue
Block a user