why DecodeAll failed? #859
-
For the following 2 functions, I can get a correct dst with the first one, while the 2nd one will have a zeroed dst. // I know the uncompressed size, so I will pre-define a dst(`make([]byte, <size>)`) before use zstdDeCompress
func zstdDeCompress(input, dst []byte) error {
decoder, err := zstd.NewReader(bytes.NewBuffer(input))
defer decoder.Close()
if err != nil {
return err
}
_, err = decoder.Read(dst)
if err != nil {
return err
}
return nil
} func zstdDeCompress(input, dst []byte) error {
decoder, err := zstd.NewReader(nil)
defer decoder.Close()
if err != nil {
return err
}
dstNew, err = decoder.DecodeAll(input, dst)
if err != nil {
return err
}
dst = dstNew
return nil
} |
Beta Was this translation helpful? Give feedback.
Answered by
klauspost
Sep 14, 2023
Replies: 1 comment 1 reply
-
DecodeAll appends to dst. Using Remember to re-use your decoder and not create a new one every time. It does of course not check if dst is over-sized, so you will need to check that and return an error yourself. |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
realcp1018
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
DecodeAll appends to dst.
Using
decoder.DecodeAll(input, dst[:0:len(dst)])
with and also having the WithDecodeAllCapLimit should do it.Remember to re-use your decoder and not create a new one every time.
It does of course not check if dst is over-sized, so you will need to check that and return an error yourself.