Based on encoding/json encodeByteSlice from the Go Standard Library https://golang.org/src/encoding/json/encode.go
(w *bytes.Buffer, scratch []byte, v []byte)
| 117 | // Based on encoding/json encodeByteSlice from the Go Standard Library |
| 118 | // https://golang.org/src/encoding/json/encode.go |
| 119 | func encodeByteSlice(w *bytes.Buffer, scratch []byte, v []byte) { |
| 120 | if v == nil { |
| 121 | w.WriteString(null) |
| 122 | return |
| 123 | } |
| 124 | |
| 125 | w.WriteRune(quote) |
| 126 | |
| 127 | encodedLen := base64.StdEncoding.EncodedLen(len(v)) |
| 128 | if encodedLen <= len(scratch) { |
| 129 | // If the encoded bytes fit in e.scratch, avoid an extra |
| 130 | // allocation and use the cheaper Encoding.Encode. |
| 131 | dst := scratch[:encodedLen] |
| 132 | base64.StdEncoding.Encode(dst, v) |
| 133 | w.Write(dst) |
| 134 | } else if encodedLen <= 1024 { |
| 135 | // The encoded bytes are short enough to allocate for, and |
| 136 | // Encoding.Encode is still cheaper. |
| 137 | dst := make([]byte, encodedLen) |
| 138 | base64.StdEncoding.Encode(dst, v) |
| 139 | w.Write(dst) |
| 140 | } else { |
| 141 | // The encoded bytes are too long to cheaply allocate, and |
| 142 | // Encoding.Encode is no longer noticeably cheaper. |
| 143 | enc := base64.NewEncoder(base64.StdEncoding, w) |
| 144 | enc.Write(v) |
| 145 | enc.Close() |
| 146 | } |
| 147 | |
| 148 | w.WriteRune(quote) |
| 149 | } |
no test coverage detected