forked from djherbis/buffer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
multi.go
185 lines (158 loc) · 3.92 KB
/
multi.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package buffer
import (
"bytes"
"encoding/gob"
"io"
"math"
)
type chain struct {
Buf BufferAt
Next BufferAt
}
type nopBufferAt struct {
Buffer
}
func (buf *nopBufferAt) ReadAt(p []byte, off int64) (int, error) {
panic("ReadAt not implemented")
}
func (buf *nopBufferAt) WriteAt(p []byte, off int64) (int, error) {
panic("WriteAt not implemented")
}
// toBufferAt converts a Buffer to a BufferAt with nop ReadAt and WriteAt funcs
func toBufferAt(buf Buffer) BufferAt {
return &nopBufferAt{Buffer: buf}
}
// NewMultiAt returns a BufferAt which is the logical concatenation of the passed BufferAts.
// The data in the buffers is shifted such that there is no non-empty buffer following
// a non-full buffer, this process is also run after every Read.
// If no buffers are passed, the returned Buffer is nil.
func NewMultiAt(buffers ...BufferAt) BufferAt {
if len(buffers) == 0 {
return nil
} else if len(buffers) == 1 {
return buffers[0]
}
buf := &chain{
Buf: buffers[0],
Next: NewMultiAt(buffers[1:]...),
}
buf.Defrag()
return buf
}
// NewMulti returns a Buffer which is the logical concatenation of the passed buffers.
// The data in the buffers is shifted such that there is no non-empty buffer following
// a non-full buffer, this process is also run after every Read.
// If no buffers are passed, the returned Buffer is nil.
func NewMulti(buffers ...Buffer) Buffer {
bufAt := make([]BufferAt, len(buffers))
for i, buf := range buffers {
bufAt[i] = toBufferAt(buf)
}
return NewMultiAt(bufAt...)
}
func (buf *chain) Reset() {
buf.Next.Reset()
buf.Buf.Reset()
}
func (buf *chain) Cap() (n int64) {
Next := buf.Next.Cap()
if buf.Buf.Cap() > math.MaxInt64-Next {
return math.MaxInt64
}
return buf.Buf.Cap() + Next
}
func (buf *chain) Len() (n int64) {
Next := buf.Next.Len()
if buf.Buf.Len() > math.MaxInt64-Next {
return math.MaxInt64
}
return buf.Buf.Len() + Next
}
func (buf *chain) Defrag() {
for !Full(buf.Buf) && !Empty(buf.Next) {
r := io.LimitReader(buf.Next, Gap(buf.Buf))
if _, err := io.Copy(buf.Buf, r); err != nil && err != io.EOF {
return
}
}
}
func (buf *chain) Read(p []byte) (n int, err error) {
n, err = buf.Buf.Read(p)
if len(p[n:]) > 0 && (err == nil || err == io.EOF) {
m, err := buf.Next.Read(p[n:])
n += m
if err != nil {
return n, err
}
}
buf.Defrag()
return n, err
}
func (buf *chain) ReadAt(p []byte, off int64) (n int, err error) {
if buf.Buf.Len() < off {
return buf.Next.ReadAt(p, off-buf.Buf.Len())
}
n, err = buf.Buf.ReadAt(p, off)
if len(p[n:]) > 0 && (err == nil || err == io.EOF) {
var m int
m, err = buf.Next.ReadAt(p[n:], 0)
n += m
}
return n, err
}
func (buf *chain) Write(p []byte) (n int, err error) {
if n, err = buf.Buf.Write(p); err == io.ErrShortWrite {
err = nil
}
p = p[n:]
if len(p) > 0 && err == nil {
m, err := buf.Next.Write(p)
n += m
if err != nil {
return n, err
}
}
return n, err
}
func (buf *chain) WriteAt(p []byte, off int64) (n int, err error) {
switch {
case buf.Buf.Cap() <= off: // past the end
return buf.Next.WriteAt(p, off-buf.Buf.Cap())
case buf.Buf.Cap() >= off+int64(len(p)): // fits in
return buf.Buf.WriteAt(p, off)
default: // partial fit
n, err = buf.Buf.WriteAt(p, off)
if len(p[n:]) > 0 && (err == nil || err == io.ErrShortWrite) {
var m int
m, err = buf.Next.WriteAt(p[n:], 0)
n += m
}
return n, err
}
}
func init() {
gob.Register(&chain{})
gob.Register(&nopBufferAt{})
}
func (buf *chain) MarshalBinary() ([]byte, error) {
b := bytes.NewBuffer(nil)
enc := gob.NewEncoder(b)
if err := enc.Encode(&buf.Buf); err != nil {
return nil, err
}
if err := enc.Encode(&buf.Next); err != nil {
return nil, err
}
return b.Bytes(), nil
}
func (buf *chain) UnmarshalBinary(data []byte) error {
b := bytes.NewBuffer(data)
dec := gob.NewDecoder(b)
if err := dec.Decode(&buf.Buf); err != nil {
return err
}
if err := dec.Decode(&buf.Next); err != nil {
return err
}
return nil
}