aboutsummaryrefslogtreecommitdiffstats
path: root/formats/packfile/common.go
blob: d207563de11735ca2ab620e849a7065f6a016f24 (plain) (blame)
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
package packfile

import (
	"bufio"
	"fmt"
	"io"
)

type trackingReader struct {
	r        io.Reader
	position int64
}

func NewTrackingReader(r io.Reader) *trackingReader {
	return &trackingReader{
		r: bufio.NewReader(r),
	}
}

func (t *trackingReader) Read(p []byte) (n int, err error) {
	n, err = t.r.Read(p)
	if err != nil {
		return 0, err
	}

	t.position += int64(n)
	return n, err
}

func (t *trackingReader) ReadByte() (c byte, err error) {
	var p [1]byte
	n, err := t.r.Read(p[:])
	if err != nil {
		return 0, err
	}

	if n > 1 {
		return 0, fmt.Errorf("read %d bytes, should have read just 1", n)
	}

	t.position++
	return p[0], nil
}

// close is used with defer to close the given io.Closer and check its
// returned error value. If Close returns an error and the given *error
// is not nil, *error is set to the error returned by Close.
//
// close is typically used with named return values like so:
//
//   func do(obj *Object) (err error) {
//     w, err := obj.Writer()
//     if err != nil {
//       return nil
//     }
//     defer close(w, &err)
//     // work with w
//   }
func close(c io.Closer, err *error) {
	if cerr := c.Close(); cerr != nil && *err == nil {
		*err = cerr
	}
}