@@ -10,11 +10,9 @@ type Stream struct {
10
10
cfg * frozenConfig
11
11
out io.Writer
12
12
buf []byte
13
- n int
14
13
Error error
15
14
indention int
16
15
Attachment interface {} // open for customized encoder
17
- floatBuf []byte
18
16
}
19
17
20
18
// NewStream create new stream instance.
@@ -25,11 +23,9 @@ func NewStream(cfg API, out io.Writer, bufSize int) *Stream {
25
23
return & Stream {
26
24
cfg : cfg .(* frozenConfig ),
27
25
out : out ,
28
- buf : make ([]byte , bufSize ),
29
- n : 0 ,
26
+ buf : make ([]byte , 0 , bufSize ),
30
27
Error : nil ,
31
28
indention : 0 ,
32
- floatBuf : make ([]byte , 0 , 32 ),
33
29
}
34
30
}
35
31
@@ -41,120 +37,57 @@ func (stream *Stream) Pool() StreamPool {
41
37
// Reset reuse this stream instance by assign a new writer
42
38
func (stream * Stream ) Reset (out io.Writer ) {
43
39
stream .out = out
44
- stream .n = 0
40
+ stream .buf = stream . buf [: 0 ]
45
41
}
46
42
47
43
// Available returns how many bytes are unused in the buffer.
48
44
func (stream * Stream ) Available () int {
49
- return len (stream .buf ) - stream .n
45
+ return cap (stream .buf ) - len ( stream .buf )
50
46
}
51
47
52
48
// Buffered returns the number of bytes that have been written into the current buffer.
53
49
func (stream * Stream ) Buffered () int {
54
- return stream .n
50
+ return len ( stream .buf )
55
51
}
56
52
57
53
// Buffer if writer is nil, use this method to take the result
58
54
func (stream * Stream ) Buffer () []byte {
59
- return stream .buf [: stream . n ]
55
+ return stream .buf
60
56
}
61
57
62
58
// Write writes the contents of p into the buffer.
63
59
// It returns the number of bytes written.
64
60
// If nn < len(p), it also returns an error explaining
65
61
// why the write is short.
66
62
func (stream * Stream ) Write (p []byte ) (nn int , err error ) {
67
- for len (p ) > stream .Available () && stream .Error == nil {
68
- if stream .out == nil {
69
- stream .growAtLeast (len (p ))
70
- } else {
71
- var n int
72
- if stream .Buffered () == 0 {
73
- // Large write, empty buffer.
74
- // Write directly from p to avoid copy.
75
- n , stream .Error = stream .out .Write (p )
76
- } else {
77
- n = copy (stream .buf [stream .n :], p )
78
- stream .n += n
79
- stream .Flush ()
80
- }
81
- nn += n
82
- p = p [n :]
83
- }
84
- }
85
- if stream .Error != nil {
86
- return nn , stream .Error
63
+ stream .buf = append (stream .buf , p ... )
64
+ if stream .out != nil {
65
+ nn , err = stream .out .Write (stream .buf )
66
+ stream .buf = stream .buf [nn :]
67
+ return
87
68
}
88
- n := copy (stream .buf [stream .n :], p )
89
- stream .n += n
90
- nn += n
91
- return nn , nil
69
+ return len (p ), nil
92
70
}
93
71
94
72
// WriteByte writes a single byte.
95
73
func (stream * Stream ) writeByte (c byte ) {
96
- if stream .Error != nil {
97
- return
98
- }
99
- if stream .Available () < 1 {
100
- stream .growAtLeast (1 )
101
- }
102
- stream .buf [stream .n ] = c
103
- stream .n ++
74
+ stream .buf = append (stream .buf , c )
104
75
}
105
76
106
77
func (stream * Stream ) writeTwoBytes (c1 byte , c2 byte ) {
107
- if stream .Error != nil {
108
- return
109
- }
110
- if stream .Available () < 2 {
111
- stream .growAtLeast (2 )
112
- }
113
- stream .buf [stream .n ] = c1
114
- stream .buf [stream .n + 1 ] = c2
115
- stream .n += 2
78
+ stream .buf = append (stream .buf , c1 , c2 )
116
79
}
117
80
118
81
func (stream * Stream ) writeThreeBytes (c1 byte , c2 byte , c3 byte ) {
119
- if stream .Error != nil {
120
- return
121
- }
122
- if stream .Available () < 3 {
123
- stream .growAtLeast (3 )
124
- }
125
- stream .buf [stream .n ] = c1
126
- stream .buf [stream .n + 1 ] = c2
127
- stream .buf [stream .n + 2 ] = c3
128
- stream .n += 3
82
+ stream .buf = append (stream .buf , c1 , c2 , c3 )
129
83
}
130
84
131
85
func (stream * Stream ) writeFourBytes (c1 byte , c2 byte , c3 byte , c4 byte ) {
132
- if stream .Error != nil {
133
- return
134
- }
135
- if stream .Available () < 4 {
136
- stream .growAtLeast (4 )
137
- }
138
- stream .buf [stream .n ] = c1
139
- stream .buf [stream .n + 1 ] = c2
140
- stream .buf [stream .n + 2 ] = c3
141
- stream .buf [stream .n + 3 ] = c4
142
- stream .n += 4
86
+ stream .buf = append (stream .buf , c1 , c2 , c3 , c4 )
143
87
}
144
88
145
89
func (stream * Stream ) writeFiveBytes (c1 byte , c2 byte , c3 byte , c4 byte , c5 byte ) {
146
- if stream .Error != nil {
147
- return
148
- }
149
- if stream .Available () < 5 {
150
- stream .growAtLeast (5 )
151
- }
152
- stream .buf [stream .n ] = c1
153
- stream .buf [stream .n + 1 ] = c2
154
- stream .buf [stream .n + 2 ] = c3
155
- stream .buf [stream .n + 3 ] = c4
156
- stream .buf [stream .n + 4 ] = c5
157
- stream .n += 5
90
+ stream .buf = append (stream .buf , c1 , c2 , c3 , c4 , c5 )
158
91
}
159
92
160
93
// Flush writes any buffered data to the underlying io.Writer.
@@ -165,56 +98,20 @@ func (stream *Stream) Flush() error {
165
98
if stream .Error != nil {
166
99
return stream .Error
167
100
}
168
- if stream .n == 0 {
169
- return nil
170
- }
171
- n , err := stream .out .Write (stream .buf [0 :stream .n ])
172
- if n < stream .n && err == nil {
173
- err = io .ErrShortWrite
174
- }
101
+ n , err := stream .out .Write (stream .buf )
175
102
if err != nil {
176
- if n > 0 && n < stream .n {
177
- copy ( stream .buf [ 0 : stream . n - n ], stream . buf [ n : stream . n ])
103
+ if stream .Error == nil {
104
+ stream .Error = err
178
105
}
179
- stream .n -= n
180
- stream .Error = err
181
106
return err
182
107
}
183
- stream .n = 0
108
+ stream .buf = stream . buf [ n :]
184
109
return nil
185
110
}
186
111
187
- func (stream * Stream ) ensure (minimal int ) {
188
- available := stream .Available ()
189
- if available < minimal {
190
- stream .growAtLeast (minimal )
191
- }
192
- }
193
-
194
- func (stream * Stream ) growAtLeast (minimal int ) {
195
- if stream .out != nil {
196
- stream .Flush ()
197
- if stream .Available () >= minimal {
198
- return
199
- }
200
- }
201
- toGrow := len (stream .buf )
202
- if toGrow < minimal {
203
- toGrow = minimal
204
- }
205
- newBuf := make ([]byte , len (stream .buf )+ toGrow )
206
- copy (newBuf , stream .Buffer ())
207
- stream .buf = newBuf
208
- }
209
-
210
112
// WriteRaw write string out without quotes, just like []byte
211
113
func (stream * Stream ) WriteRaw (s string ) {
212
- stream .ensure (len (s ))
213
- if stream .Error != nil {
214
- return
215
- }
216
- n := copy (stream .buf [stream .n :], s )
217
- stream .n += n
114
+ stream .buf = append (stream .buf , s ... )
218
115
}
219
116
220
117
// WriteNil write null to stream
@@ -275,6 +172,7 @@ func (stream *Stream) WriteEmptyObject() {
275
172
func (stream * Stream ) WriteMore () {
276
173
stream .writeByte (',' )
277
174
stream .writeIndention (0 )
175
+ stream .Flush ()
278
176
}
279
177
280
178
// WriteArrayStart write [ with possible indention
@@ -302,9 +200,7 @@ func (stream *Stream) writeIndention(delta int) {
302
200
}
303
201
stream .writeByte ('\n' )
304
202
toWrite := stream .indention - delta
305
- stream .ensure (toWrite )
306
- for i := 0 ; i < toWrite && stream .n < len (stream .buf ); i ++ {
307
- stream .buf [stream .n ] = ' '
308
- stream .n ++
203
+ for i := 0 ; i < toWrite ; i ++ {
204
+ stream .buf = append (stream .buf , ' ' )
309
205
}
310
206
}
0 commit comments