-
Notifications
You must be signed in to change notification settings - Fork 0
/
writeseeker.go
121 lines (105 loc) · 2.57 KB
/
writeseeker.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package io2
import (
"bytes"
"io"
)
// WriteSeekCloser is the interface that groups the basic Write, Seek and Close methods.
type WriteSeekCloser interface {
io.Writer
io.Seeker
io.Closer
}
// WriteSeekBuffer implements io.WriteSeeker that using in-memory byte buffer.
type WriteSeekBuffer struct {
buf *bytes.Buffer
off int
len int
}
var _ WriteSeekCloser = (*WriteSeekBuffer)(nil)
// NewWriteSeekBuffer returns an WriteSeekBuffer with the initial capacity.
func NewWriteSeekBuffer(capacity int) *WriteSeekBuffer {
return &WriteSeekBuffer{
buf: bytes.NewBuffer(make([]byte, capacity)),
}
}
// NewWriteSeekBufferBytes returns an WriteSeekBuffer with the initial buffer.
func NewWriteSeekBufferBytes(buf []byte) *WriteSeekBuffer {
off := len(buf)
return &WriteSeekBuffer{
buf: bytes.NewBuffer(buf),
off: off,
len: off,
}
}
// Write appends the contents of p to the buffer, growing the buffer as needed.
// The return value n is the length of p; err is always nil.
func (b *WriteSeekBuffer) Write(p []byte) (int, error) {
n := len(p)
noff := b.off + n
if grow := noff - b.buf.Len(); grow > 0 {
b.buf.Write(make([]byte, grow))
}
copy(b.buf.Bytes()[b.off:noff], p)
b.off = noff
if noff > b.len {
b.len = noff
}
return n, nil
}
// Seek sets the offset for the next Write to offset, interpreted according to whence:
// SeekStart means relative to the start of the file,
// SeekCurrent means relative to the current offset,
// SeekEnd means relative to the end.
// Seek returns the new offset relative to the start of the file and an error, if any.
func (b *WriteSeekBuffer) Seek(offset int64, whence int) (int64, error) {
off := int(offset)
noff := 0
switch whence {
case io.SeekStart:
noff = off
case io.SeekCurrent:
noff = b.off + off
case io.SeekEnd:
noff = b.buf.Len() + off
}
if noff < 0 {
noff = 0
}
b.off = noff
return int64(noff), nil
}
// Close calls b.Truncate(0).
func (b *WriteSeekBuffer) Close() error {
b.Truncate(0)
return nil
}
// Offset returns the offset.
func (b *WriteSeekBuffer) Offset() int {
return b.off
}
// Len returns the number of bytes of the buffer; b.Len() == len(b.Bytes()).
func (b *WriteSeekBuffer) Len() int {
return b.len
}
// Bytes returns a slice of length b.Len() of the buffer.
func (b *WriteSeekBuffer) Bytes() []byte {
n := b.buf.Len()
if n > b.len {
n = b.len
}
return b.buf.Bytes()[:n]
}
// Truncate changes the size of the buffer with offset.
func (b *WriteSeekBuffer) Truncate(n int) {
if n < 0 {
n = b.off + n
}
if n < 0 {
n = 0
}
if n < b.buf.Len() {
b.buf.Truncate(n)
}
b.off = n
b.len = n
}