File: seek.go

package info (click to toggle)
golang-github-kr-binarydist 0.0~git20120828.0.9955b0a-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 156 kB
  • ctags: 61
  • sloc: makefile: 13
file content (43 lines) | stat: -rw-r--r-- 774 bytes parent folder | download | duplicates (2)
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
package binarydist

import (
	"errors"
)

type seekBuffer struct {
	buf []byte
	pos int
}

func (b *seekBuffer) Write(p []byte) (n int, err error) {
	n = copy(b.buf[b.pos:], p)
	if n == len(p) {
		b.pos += n
		return n, nil
	}
	b.buf = append(b.buf, p[n:]...)
	b.pos += len(p)
	return len(p), nil
}

func (b *seekBuffer) Seek(offset int64, whence int) (ret int64, err error) {
	var abs int64
	switch whence {
	case 0:
		abs = offset
	case 1:
		abs = int64(b.pos) + offset
	case 2:
		abs = int64(len(b.buf)) + offset
	default:
		return 0, errors.New("binarydist: invalid whence")
	}
	if abs < 0 {
		return 0, errors.New("binarydist: negative position")
	}
	if abs >= 1<<31 {
		return 0, errors.New("binarydist: position out of range")
	}
	b.pos = int(abs)
	return abs, nil
}