...
1
2
3
4
5 package slicewriter
6
7 import (
8 "fmt"
9 "io"
10 )
11
12
13
14
15
16
17 type WriteSeeker struct {
18 payload []byte
19 off int64
20 }
21
22 func (sws *WriteSeeker) Write(p []byte) (n int, err error) {
23 amt := len(p)
24 towrite := sws.payload[sws.off:]
25 if len(towrite) < amt {
26 sws.payload = append(sws.payload, make([]byte, amt-len(towrite))...)
27 towrite = sws.payload[sws.off:]
28 }
29 copy(towrite, p)
30 sws.off += int64(amt)
31 return amt, nil
32 }
33
34
35
36
37
38 func (sws *WriteSeeker) Seek(offset int64, whence int) (int64, error) {
39 switch whence {
40 case io.SeekStart:
41 if sws.off != offset && (offset < 0 || offset > int64(len(sws.payload))) {
42 return 0, fmt.Errorf("invalid seek: new offset %d (out of range [0 %d]", offset, len(sws.payload))
43 }
44 sws.off = offset
45 return offset, nil
46 case io.SeekCurrent:
47 newoff := sws.off + offset
48 if newoff != sws.off && (newoff < 0 || newoff > int64(len(sws.payload))) {
49 return 0, fmt.Errorf("invalid seek: new offset %d (out of range [0 %d]", newoff, len(sws.payload))
50 }
51 sws.off += offset
52 return sws.off, nil
53 case io.SeekEnd:
54 newoff := int64(len(sws.payload)) + offset
55 if newoff != sws.off && (newoff < 0 || newoff > int64(len(sws.payload))) {
56 return 0, fmt.Errorf("invalid seek: new offset %d (out of range [0 %d]", newoff, len(sws.payload))
57 }
58 sws.off = newoff
59 return sws.off, nil
60 }
61
62 return 0, fmt.Errorf("unsupported seek mode %d", whence)
63 }
64
65
66
67 func (sws *WriteSeeker) BytesWritten() []byte {
68 return sws.payload
69 }
70
71 func (sws *WriteSeeker) Read(p []byte) (n int, err error) {
72 amt := len(p)
73 toread := sws.payload[sws.off:]
74 if len(toread) < amt {
75 amt = len(toread)
76 }
77 copy(p, toread)
78 sws.off += int64(amt)
79 return amt, nil
80 }
81
View as plain text