-
Notifications
You must be signed in to change notification settings - Fork 0
/
memws.go
49 lines (44 loc) · 925 Bytes
/
memws.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
package memws
import (
"errors"
"io"
)
type WriteSeek struct {
buf []byte
pos int64
}
func (ws *WriteSeek) Write(p []byte) (int, error) {
c := ws.pos + int64(len(p))
if c > int64(cap(ws.buf)) {
newBuf := make([]byte, len(ws.buf), c+int64(len(p)))
copy(newBuf, ws.buf)
ws.buf = newBuf
}
if c > int64(len(ws.buf)) {
ws.buf = ws.buf[:c]
}
copy(ws.buf[ws.pos:], p)
ws.pos += int64(len(p))
return len(p), nil
}
func (ws *WriteSeek) Seek(offset int64, whence int) (int64, error) {
var abs int64
switch whence {
case io.SeekStart:
abs = offset
case io.SeekCurrent:
abs = offset + ws.pos
case io.SeekEnd:
abs = offset + int64(len(ws.buf))
default:
return 0, errors.New("writeseek.WriteSeek.Seek: invalid whence")
}
if abs < 0 {
return 0, errors.New("writeseek.WriteSeek.Seek: negative pos")
}
ws.pos = abs
return abs, nil
}
func (ws WriteSeek) String() string {
return string(ws.buf)
}