-
Notifications
You must be signed in to change notification settings - Fork 0
/
multireader.go
339 lines (313 loc) · 7.41 KB
/
multireader.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
package io2
import (
"errors"
"fmt"
"io"
"strings"
)
// MultiReader represents a multiple reader.
type MultiReader interface {
io.Reader
// Current returns a current index of multiple readers. The current starts 0.
Current() int
}
// MultiReadCloser is the interface that groups the MultiReader and Close methods.
type MultiReadCloser interface {
MultiReader
io.Closer
}
// MultiReadCloser is the interface that groups the MultiReader, Seek and SeekReader methods.
type MultiReadSeeker interface {
MultiReader
io.Seeker
// SeekReader sets the offset of multiple readers.
SeekReader(current int) (int64, error)
}
// MultiReadSeekCloser is the interface that groups the MultiReadSeeker and Close methods.
type MultiReadSeekCloser interface {
MultiReadSeeker
io.Closer
}
var errSkip = errors.New("skip")
type singleReader struct {
io.ReadSeekCloser
off int64
length int64
}
type multiReader struct {
rs []*singleReader
current int
length int64
}
var _ io.ReadSeekCloser = (*multiReader)(nil)
// NewMultiReader creates a Reader that's the logical concatenation
// of the provided input readers.
func NewMultiReader(rs ...io.Reader) MultiReader {
ds := make([]*singleReader, len(rs))
for i, r := range rs {
ds[i] = &singleReader{ReadSeekCloser: Delegate(r)}
}
return &multiReader{rs: ds}
}
// NewMultiReadCloser create a ReaderCloser that's the logical concatenation
// of the provided input readers.
func NewMultiReadCloser(rs ...io.ReadCloser) MultiReadCloser {
ds := make([]*singleReader, len(rs))
for i, r := range rs {
ds[i] = &singleReader{ReadSeekCloser: Delegate(r)}
}
return &multiReader{rs: ds}
}
// NewMultiReadSeeker creates a ReadSeeker that's the logical concatenation
// of the provided input readers.
func NewMultiReadSeeker(rs ...io.ReadSeeker) (MultiReadSeeker, error) {
ds := make([]io.ReadSeekCloser, len(rs))
for i, r := range rs {
ds[i] = NopReadSeekCloser(r)
}
return NewMultiReadSeekCloser(ds...)
}
// NewMultiReadSeekCloser creates a ReadSeekCloser that's the logical
// concatenation of the provided input readers.
func NewMultiReadSeekCloser(rs ...io.ReadSeekCloser) (MultiReadSeekCloser, error) {
length := int64(0)
ds := make([]*singleReader, len(rs))
for i, r := range rs {
n, err := r.Seek(0, io.SeekEnd)
if err != nil {
return nil, err
}
if _, err = r.Seek(0, io.SeekStart); err != nil {
return nil, err
}
ds[i] = &singleReader{
ReadSeekCloser: Delegate(r),
length: n,
}
length += n
}
return &multiReader{rs: ds, length: length}, nil
}
func NewMultiStringReader(strs ...string) MultiReadSeeker {
length := int64(0)
ds := make([]*singleReader, len(strs))
for i, str := range strs {
ds[i] = &singleReader{
ReadSeekCloser: NopReadSeekCloser(strings.NewReader(str)),
length: int64(len(str)),
}
length += ds[i].length
}
return &multiReader{rs: ds, length: length}
}
func NewMultiFileReader(filenames ...string) (MultiReadSeekCloser, error) {
length := int64(0)
ds := make([]*singleReader, len(filenames))
for i, filename := range filenames {
f, err := osOpen(filename)
if err != nil {
return nil, err
}
info, err := fsStat(f)
if err != nil {
return nil, err
}
ds[i] = &singleReader{
ReadSeekCloser: f,
length: info.Size(),
}
length += ds[i].length
}
return &multiReader{rs: ds, length: length}, nil
}
// Current returns a current index of multiple readers.
func (mr *multiReader) Current() int {
return mr.current
}
func (mr *multiReader) each(i int, offset int64, fn func(r *singleReader) error) error {
mr.current = i
if offset >= 0 {
for ; mr.current < len(mr.rs); mr.current++ {
if err := fn(mr.rs[mr.current]); err != nil {
if err == errSkip {
err = nil
}
return err
}
}
if mr.current >= len(mr.rs) {
mr.current = len(mr.rs) - 1
}
return nil
}
for ; mr.current >= 0; mr.current-- {
if err := fn(mr.rs[mr.current]); err != nil {
if err == errSkip {
err = nil
}
return err
}
}
if mr.current < 0 {
mr.current = 0
}
return nil
}
func (mr *multiReader) Read(p []byte) (n int, err error) {
if mr.current >= len(mr.rs) {
return 0, io.EOF
}
off := 0
for ; mr.current < len(mr.rs); mr.current++ {
r := mr.rs[mr.current]
n, err = r.Read(p[off:])
if err != nil {
return 0, err
}
r.off += int64(n)
off += n
if off >= len(p) {
return off, nil
}
}
return off, nil
}
func (mr *multiReader) Seek(offset int64, whence int) (int64, error) {
switch whence {
case io.SeekStart:
return mr.seekStart(offset, 0)
case io.SeekCurrent:
return mr.seekCurrent(offset)
case io.SeekEnd:
return mr.seekEnd(offset, len(mr.rs)-1)
}
return 0, errors.New("invalid whence")
}
// SeekReader sets the offset of multiple readers. The current starts 0.
func (mr *multiReader) SeekReader(current int) (int64, error) {
var offset int64
for i, r := range mr.rs {
if i >= current {
break
}
offset += r.length
}
return mr.Seek(offset, io.SeekStart)
}
func (mr *multiReader) resetTails() error {
for i := mr.current + 1; i < len(mr.rs); i++ {
r := mr.rs[i]
if _, err := r.Seek(0, io.SeekStart); err != nil {
return err
}
r.off = 0
}
return nil
}
func (mr *multiReader) seekStart(offset int64, start int) (int64, error) {
off := int64(0)
for i := 0; i < start; i++ {
off += mr.rs[i].length
}
err := mr.each(start, offset, func(r *singleReader) error {
safeOffset := offset
if mr.current != len(mr.rs)-1 && safeOffset > (r.length-r.off) {
safeOffset = (r.length - r.off)
}
n, err := r.Seek(safeOffset, io.SeekStart)
if err != nil {
return err
}
r.off = n
if offset < r.length {
off += offset
return errSkip
}
offset -= safeOffset
off += safeOffset
return nil
})
if err != nil {
return 0, err
}
if err := mr.resetTails(); err != nil {
return 0, err
}
return off, nil
}
func (mr *multiReader) seekCurrent(offset int64) (int64, error) {
off := int64(0)
for i := 0; i < mr.current; i++ {
off += mr.rs[i].length
}
r := mr.rs[mr.current]
safeOffset := offset
if offset >= 0 {
if mr.current != len(mr.rs)-1 && safeOffset > (r.length-r.off) {
safeOffset = (r.length - r.off)
}
} else {
if mr.current != 0 && -safeOffset > r.off {
safeOffset = -r.off
}
}
n, err := r.Seek(safeOffset, io.SeekCurrent)
if err != nil {
return 0, err
}
r.off = n
if diffset := offset - safeOffset; diffset != 0 {
if offset >= 0 {
return mr.seekStart(diffset, mr.current+1)
}
return mr.seekEnd(diffset, mr.current-1)
}
if err := mr.resetTails(); err != nil {
return 0, err
}
return off + n, nil
}
func (mr *multiReader) seekEnd(offset int64, end int) (int64, error) {
off := mr.length
for i := end + 1; i < len(mr.rs); i++ {
off -= mr.rs[i].length
}
err := mr.each(end, offset, func(r *singleReader) error {
safeOffset := offset
if mr.current != 0 && -safeOffset > r.length {
safeOffset = -r.length
}
n, err := r.Seek(safeOffset, io.SeekEnd)
if err != nil {
return err
}
r.off = n
if -offset < r.length {
off += offset
return errSkip
}
offset += r.length
off -= r.length
return nil
})
if err != nil {
return 0, err
}
return off, nil
}
func (mr *multiReader) Close() error {
var errs []string
mr.each(0, 1, func(r *singleReader) error {
if err := r.Close(); err != nil {
errs = append(errs, err.Error())
}
return nil
})
if len(errs) > 0 {
return fmt.Errorf("failed to close: %s", strings.Join(errs, "; "))
}
mr.rs = nil
mr.current = 0
mr.length = 0
return nil
}