-
Notifications
You must be signed in to change notification settings - Fork 46
/
helpers.go
64 lines (51 loc) · 1.29 KB
/
helpers.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
package godbf
import (
"io"
"os"
)
type readerFunction func(r io.Reader, buf []byte) (int, error)
var reader readerFunction = io.ReadFull
// https://talks.golang.org/2012/10things.slide#8
type fileSystem interface {
Create(name string) (*os.File, error)
Open(name string) (file, error)
Stat(name string) (os.FileInfo, error)
}
// osFileSystem implements fileSystem using the local disk.
type osFileSystem struct{}
func (osFileSystem) Create(name string) (*os.File, error) { return os.Create(name) }
func (osFileSystem) Open(name string) (file, error) { return os.Open(name) }
func (osFileSystem) Stat(name string) (os.FileInfo, error) { return os.Stat(name) }
var fsWrapper fileSystem = osFileSystem{}
type file interface {
io.Closer
io.Reader
io.ReaderAt
io.Seeker
Stat() (os.FileInfo, error)
}
func readFile(filename string) ([]byte, error) {
f, err := fsWrapper.Open(filename)
if err != nil {
return nil, err
}
defer f.Close()
d, err := fsWrapper.Stat(filename)
if err != nil {
return nil, err
}
buf := make([]byte, d.Size())
_, err = reader(f, buf)
if err != nil {
return nil, err
}
return buf, err
}
func uint32ToBytes(x uint32) []byte {
var buf [4]byte
buf[0] = byte(x >> 0)
buf[1] = byte(x >> 8)
buf[2] = byte(x >> 16)
buf[3] = byte(x >> 24)
return buf[:]
}