MCPcopy Create free account
hub / github.com/aws/smithy-go / encodeByteSlice

Function encodeByteSlice

encoding/json/value.go:124–154  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

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

Callers 1

Base64EncodeBytesMethod · 0.70

Calls 5

WriteStringMethod · 0.65
WriteRuneMethod · 0.65
WriteMethod · 0.65
EncodeMethod · 0.45
CloseMethod · 0.45

Tested by

no test coverage detected