Decode returns the decoded form of src for the given compression type. The buf allows passing various buffer implementations that make decoding more efficient. See NewSyncDecodeBuffer and NewConcurrentDecodeBuffer for further details. For non-zstd compression types, it is valid to pass nil buf. De
(t Type, src []byte, buf DecodeBuffer)
| 91 | // Decode is concurrency-safe, however note the concurrency limits for the |
| 92 | // buffer of your choice. |
| 93 | func Decode(t Type, src []byte, buf DecodeBuffer) (ret []byte, err error) { |
| 94 | if len(src) == 0 || t == "" || t == None { |
| 95 | return src, nil |
| 96 | } |
| 97 | if t == Snappy { |
| 98 | var b []byte |
| 99 | if buf != nil { |
| 100 | b = buf.get() |
| 101 | defer func() { |
| 102 | buf.set(ret) |
| 103 | }() |
| 104 | } |
| 105 | // The snappy library uses `len` to calculate if we need a new buffer. |
| 106 | // In order to allocate as few buffers as possible make the length |
| 107 | // equal to the capacity. |
| 108 | b = b[:cap(b)] |
| 109 | return snappy.Decode(b, src) |
| 110 | } |
| 111 | if t == Zstd { |
| 112 | if buf == nil { |
| 113 | return nil, errors.New("zstd requested but DecodeBuffer was not provided") |
| 114 | } |
| 115 | b := buf.get() |
| 116 | defer func() { |
| 117 | buf.set(ret) |
| 118 | }() |
| 119 | return buf.zstdDecBuf().DecodeAll(src, b[:0]) |
| 120 | } |
| 121 | return nil, fmt.Errorf("unsupported compression type: %s", t) |
| 122 | } |
searching dependent graphs…