-
Notifications
You must be signed in to change notification settings - Fork 10
/
reader.go
107 lines (93 loc) · 2.72 KB
/
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// Pulled from https://github.com/youtube/vitess 229422035ca0c716ad0c1397ea1351fe62b0d35a
// Copyright 2012, Google Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package czlib
import "io"
// err starts out as nil
// we will call inflateEnd when we set err to a value:
// - whatever error is returned by the underlying reader
// - io.EOF if Close was called
type reader struct {
r io.Reader
in []byte
strm zstream
err error
skipIn bool
}
// NewReader creates a new io.ReadCloser. Reads from the returned io.ReadCloser
//read and decompress data from r. The implementation buffers input and may read
// more data than necessary from r.
// It is the caller's responsibility to call Close on the ReadCloser when done.
func NewReader(r io.Reader) (io.ReadCloser, error) {
return NewReaderBuffer(r, DEFAULT_COMPRESSED_BUFFER_SIZE)
}
// NewReaderBuffer has the same behavior as NewReader but the user can provides
// a custom buffer size.
func NewReaderBuffer(r io.Reader, bufferSize int) (io.ReadCloser, error) {
z := &reader{r: r, in: make([]byte, bufferSize)}
if err := z.strm.inflateInit(); err != nil {
return nil, err
}
return z, nil
}
func (z *reader) Read(p []byte) (int, error) {
if z.err != nil {
return 0, z.err
}
if len(p) == 0 {
return 0, nil
}
// read and deflate until the output buffer is full
z.strm.setOutBuf(p, len(p))
for {
// if we have no data to inflate, read more
if !z.skipIn && z.strm.availIn() == 0 {
var n int
n, z.err = z.r.Read(z.in)
// If we got data and EOF, pretend we didn't get the
// EOF. That way we will return the right values
// upstream. Note this will trigger another read
// later on, that should return (0, EOF).
if n > 0 && z.err == io.EOF {
z.err = nil
}
// FIXME(alainjobart) this code is not compliant with
// the Reader interface. We should process all the
// data we got from the reader, and then return the
// error, whatever it is.
if (z.err != nil && z.err != io.EOF) || (n == 0 && z.err == io.EOF) {
z.strm.inflateEnd()
return 0, z.err
}
z.strm.setInBuf(z.in, n)
} else {
z.skipIn = false
}
// inflate some
ret, err := z.strm.inflate(zNoFlush)
if err != nil {
z.err = err
z.strm.inflateEnd()
return 0, z.err
}
// if we read something, we're good
have := len(p) - z.strm.availOut()
if have > 0 {
z.skipIn = ret == Z_OK && z.strm.availOut() == 0
return have, z.err
}
}
}
// Close closes the Reader. It does not close the underlying io.Reader.
func (z *reader) Close() error {
if z.err != nil {
if z.err != io.EOF {
return z.err
}
return nil
}
z.strm.inflateEnd()
z.err = io.EOF
return nil
}