-
Notifications
You must be signed in to change notification settings - Fork 1k
/
stream.go
219 lines (192 loc) · 4 KB
/
stream.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package jsoniter
import (
"io"
)
var bytesNull []byte
var bytesTrue []byte
var bytesFalse []byte
func init() {
bytesNull = []byte("null")
bytesTrue = []byte("true")
bytesFalse = []byte("false")
}
type Stream struct {
out io.Writer
buf []byte
n int
Error error
indention int
IndentionStep int
}
func NewStream(out io.Writer, bufSize int) *Stream {
return &Stream{out, make([]byte, bufSize), 0, nil, 0, 0}
}
// Available returns how many bytes are unused in the buffer.
func (b *Stream) Available() int {
return len(b.buf) - b.n
}
// Buffered returns the number of bytes that have been written into the current buffer.
func (b *Stream) Buffered() int {
return b.n
}
// Write writes the contents of p into the buffer.
// It returns the number of bytes written.
// If nn < len(p), it also returns an error explaining
// why the write is short.
func (b *Stream) Write(p []byte) (nn int, err error) {
for len(p) > b.Available() && b.Error == nil {
var n int
if b.Buffered() == 0 {
// Large write, empty buffer.
// Write directly from p to avoid copy.
n, b.Error = b.out.Write(p)
} else {
n = copy(b.buf[b.n:], p)
b.n += n
b.Flush()
}
nn += n
p = p[n:]
}
if b.Error != nil {
return nn, b.Error
}
n := copy(b.buf[b.n:], p)
b.n += n
nn += n
return nn, nil
}
// WriteByte writes a single byte.
func (b *Stream) writeByte(c byte) error {
if b.Error != nil {
return b.Error
}
if b.Available() <= 0 && b.Flush() != nil {
return b.Error
}
b.buf[b.n] = c
b.n++
return nil
}
// Flush writes any buffered data to the underlying io.Writer.
func (b *Stream) Flush() error {
if b.Error != nil {
return b.Error
}
if b.n == 0 {
return nil
}
n, err := b.out.Write(b.buf[0:b.n])
if n < b.n && err == nil {
err = io.ErrShortWrite
}
if err != nil {
if n > 0 && n < b.n {
copy(b.buf[0:b.n - n], b.buf[n:b.n])
}
b.n -= n
b.Error = err
return err
}
b.n = 0
return nil
}
func (b *Stream) WriteRaw(s string) {
for len(s) > b.Available() && b.Error == nil {
n := copy(b.buf[b.n:], s)
b.n += n
s = s[n:]
b.Flush()
}
if b.Error != nil {
return
}
n := copy(b.buf[b.n:], s)
b.n += n
}
func (b *Stream) WriteString(s string) {
b.writeByte('"')
for len(s) > b.Available() && b.Error == nil {
n := copy(b.buf[b.n:], s)
b.n += n
s = s[n:]
b.Flush()
}
if b.Error != nil {
return
}
n := copy(b.buf[b.n:], s)
b.n += n
b.writeByte('"')
}
func (stream *Stream) WriteNil() {
stream.Write(bytesNull)
}
func (stream *Stream) WriteTrue() {
stream.Write(bytesTrue)
}
func (stream *Stream) WriteFalse() {
stream.Write(bytesFalse)
}
func (stream *Stream) WriteBool(val bool) {
if val {
stream.Write(bytesTrue)
} else {
stream.Write(bytesFalse)
}
}
func (stream *Stream) WriteObjectStart() {
stream.indention += stream.IndentionStep
stream.writeByte('{')
stream.writeIndention(0)
}
func (stream *Stream) WriteObjectField(field string) {
stream.WriteString(field)
stream.writeByte(':')
}
func (stream *Stream) WriteObjectEnd() {
stream.writeIndention(stream.IndentionStep)
stream.indention -= stream.IndentionStep
stream.writeByte('}')
}
func (stream *Stream) WriteEmptyObject() {
stream.writeByte('{')
stream.writeByte('}')
}
func (stream *Stream) WriteMore() {
stream.writeByte(',')
stream.writeIndention(0)
}
func (stream *Stream) WriteArrayStart() {
stream.indention += stream.IndentionStep
stream.writeByte('[')
stream.writeIndention(0)
}
func (stream *Stream) WriteEmptyArray() {
stream.writeByte('[')
stream.writeByte(']')
}
func (stream *Stream) WriteArrayEnd() {
stream.writeIndention(stream.IndentionStep)
stream.indention -= stream.IndentionStep
stream.writeByte(']')
}
func (stream *Stream) writeIndention(delta int) {
if (stream.indention == 0) {
return
}
stream.writeByte('\n')
toWrite := stream.indention - delta
i := 0
for {
for ; i < toWrite && stream.n < len(stream.buf); i++ {
stream.buf[stream.n] = ' '
stream.n ++
}
if i == toWrite {
break;
} else {
stream.Flush()
}
}
}