encode.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. // Copyright 2011 The Snappy-Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package snappy
  5. import (
  6. "io"
  7. "github.com/klauspost/compress/s2"
  8. )
  9. // Encode returns the encoded form of src. The returned slice may be a sub-
  10. // slice of dst if dst was large enough to hold the entire encoded block.
  11. // Otherwise, a newly allocated slice will be returned.
  12. //
  13. // The dst and src must not overlap. It is valid to pass a nil dst.
  14. //
  15. // Encode handles the Snappy block format, not the Snappy stream format.
  16. func Encode(dst, src []byte) []byte {
  17. return s2.EncodeSnappyBetter(dst, src)
  18. }
  19. // MaxEncodedLen returns the maximum length of a snappy block, given its
  20. // uncompressed length.
  21. //
  22. // It will return a negative value if srcLen is too large to encode.
  23. func MaxEncodedLen(srcLen int) int {
  24. return s2.MaxEncodedLen(srcLen)
  25. }
  26. // NewWriter returns a new Writer that compresses to w.
  27. //
  28. // The Writer returned does not buffer writes. There is no need to Flush or
  29. // Close such a Writer.
  30. //
  31. // Deprecated: the Writer returned is not suitable for many small writes, only
  32. // for few large writes. Use NewBufferedWriter instead, which is efficient
  33. // regardless of the frequency and shape of the writes, and remember to Close
  34. // that Writer when done.
  35. func NewWriter(w io.Writer) *Writer {
  36. return s2.NewWriter(w, s2.WriterSnappyCompat(), s2.WriterBetterCompression(), s2.WriterFlushOnWrite(), s2.WriterConcurrency(1))
  37. }
  38. // NewBufferedWriter returns a new Writer that compresses to w, using the
  39. // framing format described at
  40. // https://github.com/google/snappy/blob/master/framing_format.txt
  41. //
  42. // The Writer returned buffers writes. Users must call Close to guarantee all
  43. // data has been forwarded to the underlying io.Writer. They may also call
  44. // Flush zero or more times before calling Close.
  45. func NewBufferedWriter(w io.Writer) *Writer {
  46. return s2.NewWriter(w, s2.WriterSnappyCompat(), s2.WriterBetterCompression())
  47. }
  48. // Writer is an io.Writer that can write Snappy-compressed bytes.
  49. //
  50. // Writer handles the Snappy stream format, not the Snappy block format.
  51. type Writer = s2.Writer