-
Notifications
You must be signed in to change notification settings - Fork 7
/
ListMerger.go
62 lines (54 loc) · 1.6 KB
/
ListMerger.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
package resource
import (
"bytes"
"io"
)
type listMerger struct {
list List
}
// Compound indicates whether the resource is a compound one, which can contain zero, one, or more blocks.
// Simple resources always contain exactly one block.
func (view listMerger) Compound() bool {
return view.list[0].Compound()
}
// ContentType specifies how the data in the resource shall be interpreted.
func (view listMerger) ContentType() ContentType {
return view.list[0].ContentType()
}
// Compressed indicates whether the data should be compressed when serialized.
func (view listMerger) Compressed() bool {
return view.list[0].Compressed()
}
// BlockCount returns the maximum count of all resources.
func (view listMerger) BlockCount() (max int) {
for _, layer := range view.list {
count := layer.BlockCount()
if max < count {
max = count
}
}
return
}
// Block returns a reader for the identified block in the resource.
// The view returns the block from the first resource that has a non-empty block.
// The resources are checked from last-to-first.
func (view listMerger) Block(index int) (reader io.Reader, err error) {
blockCount := view.BlockCount()
if (index < 0) || (index >= blockCount) {
return nil, ErrBlockNotFound(index, blockCount)
}
for layer := len(view.list) - 1; (layer >= 0) && (reader == nil); layer-- {
tempReader, tempErr := view.list[layer].Block(index)
if tempErr == nil {
var buf [1]byte
read, _ := tempReader.Read(buf[:])
if read == 1 {
reader, err = view.list[layer].Block(index)
}
}
}
if (reader == nil) && (err == nil) {
reader = bytes.NewBuffer(nil)
}
return
}