encode.go raw

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