| 720 | } |
| 721 | |
| 722 | func encodeByteSlice(e *encodeState, v reflect.Value, _ encOpts) { |
| 723 | if v.IsNil() { |
| 724 | e.WriteString("null") |
| 725 | return |
| 726 | } |
| 727 | s := v.Bytes() |
| 728 | e.WriteByte('"') |
| 729 | encodedLen := base64.StdEncoding.EncodedLen(len(s)) |
| 730 | if encodedLen <= len(e.scratch) { |
| 731 | // If the encoded bytes fit in e.scratch, avoid an extra |
| 732 | // allocation and use the cheaper Encoding.Encode. |
| 733 | dst := e.scratch[:encodedLen] |
| 734 | base64.StdEncoding.Encode(dst, s) |
| 735 | e.Write(dst) |
| 736 | } else if encodedLen <= 1024 { |
| 737 | // The encoded bytes are short enough to allocate for, and |
| 738 | // Encoding.Encode is still cheaper. |
| 739 | dst := make([]byte, encodedLen) |
| 740 | base64.StdEncoding.Encode(dst, s) |
| 741 | e.Write(dst) |
| 742 | } else { |
| 743 | // The encoded bytes are too long to cheaply allocate, and |
| 744 | // Encoding.Encode is no longer noticeably cheaper. |
| 745 | enc := base64.NewEncoder(base64.StdEncoding, e) |
| 746 | enc.Write(s) |
| 747 | enc.Close() |
| 748 | } |
| 749 | e.WriteByte('"') |
| 750 | } |
| 751 | |
| 752 | // sliceEncoder just wraps an arrayEncoder, checking to make sure the value isn't nil. |
| 753 | type sliceEncoder struct { |