| 222 | } |
| 223 | |
| 224 | func (e *encoder) Write(p []byte) (n int, err error) { |
| 225 | if e.err != nil { |
| 226 | return 0, e.err |
| 227 | } |
| 228 | |
| 229 | for len(p) >= len(e.bufin)-e.fill { |
| 230 | //copy len(e.buf) - fill bytes into e.buf to make it full |
| 231 | to_copy := len(e.bufin) - e.fill |
| 232 | copy(e.bufin[e.fill:], p[:to_copy]) |
| 233 | p = p[to_copy:] |
| 234 | |
| 235 | //write the encoded whole buffer |
| 236 | encodeChunk(e.encoded[:], e.bufin[:]) |
| 237 | _, e.err = e.w.Write(e.encoded[:]) |
| 238 | if e.err != nil { |
| 239 | return n, e.err |
| 240 | } |
| 241 | n += 4 |
| 242 | e.fill = 0 |
| 243 | } |
| 244 | for i := 0; i < len(p); i++ { |
| 245 | e.bufin[e.fill] = p[i] |
| 246 | e.fill += 1 |
| 247 | } |
| 248 | return n, e.w.(*bufio.Writer).Flush() |
| 249 | } |
| 250 | |
| 251 | func (e *encoder) Close() error { |
| 252 | if e.err == nil && e.fill > 0 { |