-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathchunkedreader.go
63 lines (50 loc) · 1.25 KB
/
chunkedreader.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
57
58
59
60
61
62
63
// Package chunkedreader is a light weight wrapper for bufio that enables
// reading of byte streams in fixed size chunks.
// Kailash Nadh, http://nadh.in/code/chunkedreader
// March 2015
// MIT License
package chunkedreader
import (
"bufio"
"io"
)
type ChunkedReader struct {
reader *bufio.Reader
scanner *bufio.Scanner
length int
}
// Create a new instance of the ChunkedReader.
func New(rd io.Reader, length int) *ChunkedReader {
r := bufio.NewReader(rd)
c := &ChunkedReader{
reader: r,
scanner: bufio.NewScanner(r),
length: length,
}
// Assign the custom split function to the bufio Scanner
c.scanner.Split(c.split)
return c
}
func (c *ChunkedReader) Read() bool {
return c.scanner.Scan()
}
func (c *ChunkedReader) Bytes() []byte {
return c.scanner.Bytes()
}
// The custom bufio.Scanner split function. While it's meant
// to split streams by delimiters, we're splitting based on
// fixed lengths here.
func (c *ChunkedReader) split(data []byte, atEOF bool) (advance int, token []byte, err error) {
if atEOF && len(data) == 0 {
return 0, nil, nil
}
// Read a fixed size.
if len(data) >= c.length {
return c.length, data[0:c.length], nil
}
if atEOF {
return len(data), data, nil
}
// Request more data.
return 0, nil, nil
}