forked from hamba/avro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
writer.go
194 lines (158 loc) · 3.7 KB
/
writer.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package avro
import (
"encoding/binary"
"io"
"math"
)
// WriterFunc is a function used to customize the Writer.
type WriterFunc func(w *Writer)
// WithWriterConfig specifies the configuration to use with a writer.
func WithWriterConfig(cfg API) WriterFunc {
return func(w *Writer) {
w.cfg = cfg.(*frozenConfig)
}
}
// Writer is an Avro specific io.Writer.
type Writer struct {
cfg *frozenConfig
out io.Writer
buf []byte
Error error
}
// NewWriter creates a new Writer.
func NewWriter(out io.Writer, bufSize int, opts ...WriterFunc) *Writer {
writer := &Writer{
cfg: DefaultConfig.(*frozenConfig),
out: out,
buf: make([]byte, 0, bufSize),
Error: nil,
}
for _, opt := range opts {
opt(writer)
}
return writer
}
// Reset resets the Writer with a new io.Writer attached.
func (w *Writer) Reset(out io.Writer) {
w.out = out
w.buf = w.buf[:0]
}
// Buffered returns the number of buffered bytes.
func (w *Writer) Buffered() int {
return len(w.buf)
}
// Buffer gets the Writer buffer.
func (w *Writer) Buffer() []byte {
return w.buf
}
// Flush writes any buffered data to the underlying io.Writer.
func (w *Writer) Flush() error {
if w.out == nil {
return nil
}
if w.Error != nil {
return w.Error
}
n, err := w.out.Write(w.buf)
if n < len(w.buf) && err == nil {
err = io.ErrShortWrite
}
if err != nil {
if w.Error == nil {
w.Error = err
}
return err
}
w.buf = w.buf[:0]
return nil
}
func (w *Writer) writeByte(b byte) {
w.buf = append(w.buf, b)
}
// Write writes raw bytes to the Writer.
func (w *Writer) Write(b []byte) (int, error) {
w.buf = append(w.buf, b...)
return len(b), nil
}
// WriteBool writes a Bool to the Writer.
func (w *Writer) WriteBool(b bool) {
if b {
w.writeByte(0x01)
return
}
w.writeByte(0x00)
}
// WriteInt writes an Int to the Writer.
func (w *Writer) WriteInt(i int32) {
e := uint64((uint32(i) << 1) ^ uint32(i>>31))
w.encodeInt(e)
}
// WriteLong writes a Long to the Writer.
func (w *Writer) WriteLong(i int64) {
e := (uint64(i) << 1) ^ uint64(i>>63)
w.encodeInt(e)
}
func (w *Writer) encodeInt(i uint64) {
if i == 0 {
w.writeByte(0)
return
}
for i > 0 {
b := byte(i) & 0x7F
i >>= 7
if i != 0 {
b |= 0x80
}
w.writeByte(b)
}
}
// WriteFloat writes a Float to the Writer.
func (w *Writer) WriteFloat(f float32) {
b := make([]byte, 4)
binary.LittleEndian.PutUint32(b, math.Float32bits(f))
w.buf = append(w.buf, b...)
}
// WriteDouble writes a Double to the Writer.
func (w *Writer) WriteDouble(f float64) {
b := make([]byte, 8)
binary.LittleEndian.PutUint64(b, math.Float64bits(f))
w.buf = append(w.buf, b...)
}
// WriteBytes writes Bytes to the Writer.
func (w *Writer) WriteBytes(b []byte) {
w.WriteLong(int64(len(b)))
w.buf = append(w.buf, b...)
}
// WriteString reads a String to the Writer.
func (w *Writer) WriteString(s string) {
w.WriteLong(int64(len(s)))
w.buf = append(w.buf, s...)
}
// WriteBlockHeader writes a Block Header to the Writer.
func (w *Writer) WriteBlockHeader(l, s int64) {
if s > 0 && !w.cfg.config.DisableBlockSizeHeader {
w.WriteLong(-l)
w.WriteLong(s)
return
}
w.WriteLong(l)
}
// WriteBlockCB writes a block using the callback.
func (w *Writer) WriteBlockCB(callback func(w *Writer) int64) int64 {
var dummyHeader [18]byte
headerStart := len(w.buf)
// Write dummy header
_, _ = w.Write(dummyHeader[:])
// Write block data
capturedAt := len(w.buf)
length := callback(w)
size := int64(len(w.buf) - capturedAt)
// Take a reference to the block data
captured := w.buf[capturedAt:len(w.buf)]
// Rewrite the header
w.buf = w.buf[:headerStart]
w.WriteBlockHeader(length, size)
// Copy the block data back to its position
w.buf = append(w.buf, captured...)
return length
}