-
Notifications
You must be signed in to change notification settings - Fork 6
/
reader.go
74 lines (69 loc) · 1.16 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
package binary
import (
"bytes"
"compress/gzip"
"errors"
"io"
"log"
)
var data map[string][]string
var size map[string]int64
// buffer data
type buffer struct {
err error
data []byte
}
// Bytes to retrieve file data
func Bytes(filename string) ([]byte, error) {
var r bytes.Buffer
defer r.Truncate(0)
parts, ok := data[filename]
if !ok {
return nil, errors.New("file does not exist")
}
ch := make(chan *buffer, 1)
go func() {
var zbuf bytes.Buffer
defer zbuf.Truncate(0)
for _, s := range parts {
zbuf.Write([]byte(s))
}
gz, err := gzip.NewReader(&zbuf)
if err != nil {
ch <- &buffer{
err: err,
}
return
}
var buf bytes.Buffer
defer buf.Truncate(0)
if _, err = io.Copy(&buf, gz); err != nil {
ch <- &buffer{
err: err,
}
return
}
if err := gz.Close(); err != nil {
ch <- &buffer{
err: err,
}
return
}
ch <- &buffer{
data: buf.Bytes(),
}
}()
res := <-ch
if err := res.err; err != nil {
return nil, res.err
}
return res.data, nil
}
// MustBytes to read bytes data from file
func MustBytes(filename string) []byte {
b, err := Bytes(filename)
if err != nil {
log.Panic(err)
}
return b
}