Browse Source

lazy part_set reader

pull/205/head
Ethan Buchman 9 years ago
parent
commit
a34f9e082e
1 changed files with 37 additions and 0 deletions
  1. +37
    -0
      types/part_set.go

+ 37
- 0
types/part_set.go View File

@ -230,6 +230,8 @@ func (ps *PartSet) GetReader() io.Reader {
if !ps.IsComplete() { if !ps.IsComplete() {
PanicSanity("Cannot GetReader() on incomplete PartSet") PanicSanity("Cannot GetReader() on incomplete PartSet")
} }
return NewPartSetReader(ps.parts)
buf := []byte{} buf := []byte{}
for _, part := range ps.parts { for _, part := range ps.parts {
buf = append(buf, part.Bytes...) buf = append(buf, part.Bytes...)
@ -237,6 +239,41 @@ func (ps *PartSet) GetReader() io.Reader {
return bytes.NewReader(buf) return bytes.NewReader(buf)
} }
type PartSetReader struct {
i int
parts []*Part
reader *bytes.Reader
}
func NewPartSetReader(parts []*Part) *PartSetReader {
return &PartSetReader{
i: 0,
parts: parts,
reader: bytes.NewReader(parts[0].Bytes),
}
}
func (psr *PartSetReader) Read(p []byte) (n int, err error) {
readerLen := psr.reader.Len()
if readerLen >= len(p) {
return psr.reader.Read(p)
} else if readerLen > 0 {
n1, err := psr.Read(p[:readerLen])
if err != nil {
return n1, err
}
n2, err := psr.Read(p[readerLen:])
return n1 + n2, err
}
psr.i += 1
if psr.i >= len(psr.parts) {
return 0, fmt.Errorf("Attempt to read from PartSet but no parts left")
}
psr.reader = bytes.NewReader(psr.parts[psr.i].Bytes)
return psr.Read(p)
}
func (ps *PartSet) StringShort() string { func (ps *PartSet) StringShort() string {
if ps == nil { if ps == nil {
return "nil-PartSet" return "nil-PartSet"


Loading…
Cancel
Save