forked from sammiq/dec-decode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fixed-record.go
72 lines (60 loc) · 1.63 KB
/
fixed-record.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
61
62
63
64
65
66
67
68
69
70
71
72
package main
import (
"errors"
"unicode/utf8"
)
//FixedRecord provides a fixed storage Buffer that implements io.Writer
type FixedRecord interface {
Record() []byte
Size() int
Bytes() []byte
Len() int
Write(p []byte) (n int, err error)
WriteString(s string) (n int, err error)
WriteByte(c byte) error
WriteRune(r rune) (n int, err error)
}
type internalFixedRecord struct {
buf []byte
off int
}
func (b *internalFixedRecord) Record() []byte { return b.buf }
func (b *internalFixedRecord) Size() int { return len(b.buf) }
func (b *internalFixedRecord) Bytes() []byte { return b.buf[0:b.off] }
func (b *internalFixedRecord) Len() int { return b.off }
func (b *internalFixedRecord) Write(p []byte) (n int, err error) {
if b.off+len(p) > b.Size() {
return 0, errors.New("too much data to write")
}
n = copy(b.buf[b.off:], p)
b.off += n
return n, nil
}
func (b *internalFixedRecord) WriteString(s string) (n int, err error) {
if b.off+len(s) > b.Size() {
return 0, errors.New("too much data to write")
}
n = copy(b.buf[b.off:], s)
b.off += n
return n, nil
}
func (b *internalFixedRecord) WriteByte(c byte) error {
if b.off+1 > b.Size() {
return errors.New("too much data to write")
}
b.buf[b.off] = c
b.off++
return nil
}
func (b *internalFixedRecord) WriteRune(r rune) (n int, err error) {
if b.off+utf8.RuneLen(r) > b.Size() {
return 0, errors.New("too much data to write")
}
n = utf8.EncodeRune(b.buf[b.off:b.off+utf8.UTFMax], r)
b.off += n
return n, nil
}
//NewFixedRecord creates a write buffer with a given size
func NewFixedRecord(size int) FixedRecord {
return &internalFixedRecord{buf: make([]byte, size)}
}