-
Notifications
You must be signed in to change notification settings - Fork 64
/
chunk_reader.go
56 lines (49 loc) · 1.48 KB
/
chunk_reader.go
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
46
47
48
49
50
51
52
53
54
55
56
// Copyright 2017 Keybase, Inc. All rights reserved. Use of
// this source code is governed by the included BSD license.
package saltpack
// chunker is an interface for a type that emits a sequence of
// plaintext chunks.
//
// Implementations should follow exampleChunker in
// chunk_reader_test.go pretty closely.
type chunker interface {
// getNextChunk() returns a plaintext chunk with an error. If
// the chunk is empty, the error must be non-nil. Once
// getNextChunk() returns a non-nil error (which may be
// io.EOF), it can assume that it will never be called again.
getNextChunk() ([]byte, error)
}
// chunkReader is an io.Reader adaptor for chunker.
type chunkReader struct {
chunker chunker
prevChunk []byte
prevErr error
}
func newChunkReader(chunker chunker) *chunkReader {
return &chunkReader{chunker: chunker}
}
func (r *chunkReader) Read(p []byte) (n int, err error) {
// Copy data into p until it is full, or getNextChunk()
// returns a non-nil error.
for {
// Drain r.prevChunk first before checking for an error.
if len(r.prevChunk) > 0 {
copied := copy(p[n:], r.prevChunk)
n += copied
r.prevChunk = r.prevChunk[copied:]
if len(r.prevChunk) > 0 {
// p is full.
return n, nil
}
}
if r.prevErr != nil {
// r.prevChunk is fully drained, so return the
// error.
return n, r.prevErr
}
r.prevChunk, r.prevErr = r.chunker.getNextChunk()
if len(r.prevChunk) == 0 && r.prevErr == nil {
panic("empty chunk and nil error")
}
}
}