forked from mumax/3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reader.go
81 lines (69 loc) · 1.51 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
package httpfs
// Utility functions on top of standard httpfs protocol
import (
"bufio"
"bytes"
"io"
"io/ioutil"
)
const BUFSIZE = 16 * 1024 * 1024 // bufio buffer size
// create a file for writing, clobbers previous content if any.
func Create(URL string) (WriteCloseFlusher, error) {
_ = Remove(URL)
err := Touch(URL)
if err != nil {
return nil, err
}
return &bufWriter{bufio.NewWriterSize(&appendWriter{URL, 0}, BUFSIZE)}, nil
}
func MustCreate(URL string) WriteCloseFlusher {
f, err := Create(URL)
if err != nil {
panic(err)
}
return f
}
type WriteCloseFlusher interface {
io.WriteCloser
Flush() error
}
// open a file for reading
func Open(URL string) (io.ReadCloser, error) {
data, err := Read(URL)
if err != nil {
return nil, err
}
return ioutil.NopCloser(bytes.NewReader(data)), nil
}
func MustOpen(URL string) io.ReadCloser {
f, err := Open(URL)
if err != nil {
panic(err)
}
return f
}
type bufWriter struct {
buf *bufio.Writer
}
func (w *bufWriter) Write(p []byte) (int, error) { return w.buf.Write(p) }
func (w *bufWriter) Close() error {
err := w.buf.Flush()
w.buf = nil // Dangling pointer somewhere?
if err != nil {
return err
}
return nil
}
func (w *bufWriter) Flush() error { return w.buf.Flush() }
type appendWriter struct {
URL string
byteCount int64
}
func (w *appendWriter) Write(p []byte) (int, error) {
err := AppendSize(w.URL, p, w.byteCount)
if err != nil {
return 0, err // don't know how many bytes written
}
w.byteCount += int64(len(p))
return len(p), nil
}