forked from mstoykov/mp4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmulti_read_closer.go
60 lines (51 loc) · 1.12 KB
/
multi_read_closer.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
package mp4
import (
"fmt"
"io"
)
type multiReadCloser struct {
readers []io.ReadCloser
index int
}
func newMultiReadCloser(readerClosers ...io.ReadCloser) io.ReadCloser {
return &multiReadCloser{
readers: readerClosers,
}
}
func (m *multiReadCloser) Read(p []byte) (int, error) {
if 0 == len(m.readers) {
return 0, io.EOF
}
size, err := m.readers[0].Read(p)
if err != nil {
if err != io.EOF {
return size, err
}
if closeErr := m.readers[0].Close(); closeErr != nil {
fmt.Printf("Got error while closing no longer needed readers inside multiReadCloser: %s\n", closeErr)
}
m.readers = m.readers[1:]
if 0 != len(m.readers) {
err = nil
}
}
return size, err
}
func (m *multiReadCloser) Close() error {
var err error
for _, reader := range m.readers {
err = AppendError(err, reader.Close())
}
m.readers = nil
return err
}
// AppendError appends one error to the other and doing the right thing if one or more of them are nil
func AppendError(err1 error, err2 error) error {
if err2 == nil {
return err1
}
if err1 == nil {
return err2
}
return fmt.Errorf("%s after %s", err2, err1)
}