-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathwriterseeker.go
72 lines (62 loc) · 1.72 KB
/
writerseeker.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
package writerseeker
import (
"bytes"
"errors"
"io"
)
// WriterSeeker is an in-memory io.WriteSeeker implementation
type WriterSeeker struct {
buf bytes.Buffer
pos int
}
// Write writes to the buffer of this WriterSeeker instance
func (ws *WriterSeeker) Write(p []byte) (n int, err error) {
// If the offset is past the end of the buffer, grow the buffer with null bytes.
if extra := ws.pos - ws.buf.Len(); extra > 0 {
if _, err := ws.buf.Write(make([]byte, extra)); err != nil {
return n, err
}
}
// If the offset isn't at the end of the buffer, write as much as we can.
if ws.pos < ws.buf.Len() {
n = copy(ws.buf.Bytes()[ws.pos:], p)
p = p[n:]
}
// If there are remaining bytes, append them to the buffer.
if len(p) > 0 {
var bn int
bn, err = ws.buf.Write(p)
n += bn
}
ws.pos += n
return n, err
}
// Seek seeks in the buffer of this WriterSeeker instance
func (ws *WriterSeeker) Seek(offset int64, whence int) (int64, error) {
newPos, offs := 0, int(offset)
switch whence {
case io.SeekStart:
newPos = offs
case io.SeekCurrent:
newPos = ws.pos + offs
case io.SeekEnd:
newPos = ws.buf.Len() + offs
}
if newPos < 0 {
return 0, errors.New("negative result pos")
}
ws.pos = newPos
return int64(newPos), nil
}
// Reader returns an io.Reader. Use it, for example, with io.Copy, to copy the content of the WriterSeeker buffer to an io.Writer
func (ws *WriterSeeker) Reader() io.Reader {
return bytes.NewReader(ws.buf.Bytes())
}
// Close :
func (ws *WriterSeeker) Close() error {
return nil
}
// BytesReader returns a *bytes.Reader. Use it when you need a reader that implements the io.ReadSeeker interface
func (ws *WriterSeeker) BytesReader() *bytes.Reader {
return bytes.NewReader(ws.buf.Bytes())
}