diff options
author | Máximo Cuadros <mcuadros@gmail.com> | 2016-09-25 23:58:59 +0200 |
---|---|---|
committer | Máximo Cuadros <mcuadros@gmail.com> | 2016-09-25 23:58:59 +0200 |
commit | b9c0a09435392913c0054382500c805cd7cb596b (patch) | |
tree | d5a4bebff33b02215b25515ab769f277c0c07bb9 /formats/objfile/common.go | |
parent | 859775d320d574979c63a114de1437e3c5d9114c (diff) | |
download | go-git-b9c0a09435392913c0054382500c805cd7cb596b.tar.gz |
formats: objfile idomatic reader/writer
Diffstat (limited to 'formats/objfile/common.go')
-rw-r--r-- | formats/objfile/common.go | 79 |
1 files changed, 0 insertions, 79 deletions
diff --git a/formats/objfile/common.go b/formats/objfile/common.go deleted file mode 100644 index 839f92c..0000000 --- a/formats/objfile/common.go +++ /dev/null @@ -1,79 +0,0 @@ -package objfile - -import ( - "errors" - "io" - "strconv" - - "gopkg.in/src-d/go-git.v4/core" -) - -var ( - // ErrClosed is returned when the objfile Reader or Writer is already closed. - ErrClosed = errors.New("objfile: already closed") - // ErrHeader is returned when the objfile has an invalid header. - ErrHeader = errors.New("objfile: invalid header") - // ErrNegativeSize is returned when a negative object size is declared. - ErrNegativeSize = errors.New("objfile: negative object size") -) - -type header struct { - t core.ObjectType - size int64 -} - -func (h *header) Read(r io.Reader) error { - t, err := h.readSlice(r, ' ') - if err != nil { - return err - } - - h.t, err = core.ParseObjectType(string(t)) - if err != nil { - return err - } - - size, err := h.readSlice(r, 0) - if err != nil { - return err - } - - h.size, err = strconv.ParseInt(string(size), 10, 64) - if err != nil { - return ErrHeader - } - - if h.size < 0 { - return ErrNegativeSize - } - - return nil -} - -func (h *header) Write(w io.Writer) error { - b := h.t.Bytes() - b = append(b, ' ') - b = append(b, []byte(strconv.FormatInt(h.size, 10))...) - b = append(b, 0) - _, err := w.Write(b) - return err -} - -// readSlice reads one byte at a time from r until it encounters delim or an -// error. -func (h *header) readSlice(r io.Reader, delim byte) ([]byte, error) { - var buf [1]byte - value := make([]byte, 0, 16) - for { - if n, err := r.Read(buf[:]); err != nil && (err != io.EOF || n == 0) { - if err == io.EOF { - return nil, ErrHeader - } - return nil, err - } - if buf[0] == delim { - return value, nil - } - value = append(value, buf[0]) - } -} |