lz4

package module
v0.0.0-...-535f0fd Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 12, 2019 License: BSD-3-Clause Imports: 6 Imported by: 0

README

golz4

Godoc license

Golang interface to LZ4 compression.

Forked from github.com/cloudflare/golz4 but with significant differences:

  • input/output arg order has been swapped to follow Go convention, ie Compress(in, out) -> Compress(out, in)
  • lz4 131 used which fixes several segfaults

Benchmark

BenchmarkCompress-8             	 5000000	       234 ns/op	 183.73 MB/s	       0 B/op	       0 allocs/op
BenchmarkCompressUncompress-8   	20000000	        62.4 ns/op	 688.60 MB/s	       0 B/op	       0 allocs/op
BenchmarkStreamCompress-8       	   50000	     32842 ns/op	2003.41 MB/s	  278537 B/op	       4 allocs/op
BenchmarkStreamUncompress-8     	  500000	      2867 ns/op	22855.34 MB/s	      52 B/op	       2 allocs/op

Documentation

Overview

Package lz4 implements compression using lz4.c and lz4hc.c

Copyright (c) 2016 Datadog Copyright (c) 2013 CloudFlare, Inc.

Index

Constants

View Source
const (
	// MaxInputSize is the max supported input size. see macro LZ4_MAX_INPUT_SIZE.
	MaxInputSize = 0x7E000000 // 2 113 929 216 bytes

)

Variables

This section is empty.

Functions

func Compress

func Compress(out, in []byte) (outSize int, err error)

Compress compresses in and puts the content in out. len(out) should have enough space for the compressed data (use CompressBound to calculate). Returns the number of bytes in the out slice.

func CompressAllocHdr

func CompressAllocHdr(in []byte) (out []byte, err error)

CompressAllocHdr is like Compress, but allocates the out slice itself and automatically resizes it to the proper size of the compressed output. This can be more convenient to use if you are in a situation where you cannot reuse buffers.

func CompressBound

func CompressBound(in []byte) int

CompressBound calculates the size of the output buffer needed by Compress. This is based on the following macro:

#define LZ4_COMPRESSBOUND(isize)

((unsigned int)(isize) > (unsigned int)LZ4_MAX_INPUT_SIZE ? 0 : (isize) + ((isize)/255) + 16)

func CompressBoundHdr

func CompressBoundHdr(in []byte) int

CompressBoundHdr returns the upper bounds of the size of the compressed byte plus space for a length header.

func CompressBoundInt

func CompressBoundInt(inputSize int) int

CompressBoundInt returns the maximum size that LZ4 compression may output in a "worst case" scenario (input data not compressible). see macro LZ4_COMPRESSBOUND.

func CompressHC

func CompressHC(out, in []byte) (int, error)

CompressHC compresses in and puts the content in out. len(out) should have enough space for the compressed data (use CompressBound to calculate). Returns the number of bytes in the out slice. Determines the compression level automatically.

func CompressHCHdr

func CompressHCHdr(out, in []byte) (count int, err error)

CompressHCHdr implements high-compression ratio compression.

func CompressHCLevel

func CompressHCLevel(out, in []byte, level int) (outSize int, err error)

CompressHCLevel compresses in at the given compression level and puts the content in out. len(out) should have enough space for the compressed data (use CompressBound to calculate). Returns the number of bytes in the out slice. To automatically choose the compression level, use 0. Otherwise, use any value in the inclusive range 1 (worst) through 16 (best). Most applications will prefer CompressHC.

func CompressHCLevelHdr

func CompressHCLevelHdr(out, in []byte, level int) (count int, err error)

CompressHCLevelHdr implements high-compression ratio compression.

func CompressHdr

func CompressHdr(out, in []byte) (count int, err error)

CompressHdr compresses in to out. It returns the number of bytes written to out and any errors that may have been encountered. This version adds a 4-byte little endian "header" indicating the length of the original message so that it may be decompressed successfully later.

func NewReader

func NewReader(r io.Reader) io.ReadCloser

NewReader creates a new io.ReadCloser. Reads from the returned ReadCloser read and decompress data from r. It is the caller's responsibility to call Close on the ReadCloser when done. If this is not done, underlying objects in the lz4 library will not be freed.

func Uncompress

func Uncompress(out, in []byte) (outSize int, err error)

Uncompress with a known output size. len(out) should be equal to the length of the uncompressed out.

func UncompressAllocHdr

func UncompressAllocHdr(out, in []byte) ([]byte, error)

UncompressAllocHdr uncompresses the stream from in into out if out has enough space. Otherwise, a new slice is allocated automatically and returned. This function uses the "length header" to detemrine how much space is necessary fo the result message, which CloudFlare's implementation doesn't have.

func UncompressHdr

func UncompressHdr(out, in []byte) error

UncompressHdr uncompresses in into out. Out must have enough space allocated for the uncompressed message.

Types

type Writer

type Writer struct {
	// contains filtered or unexported fields
}

Writer is an io.WriteCloser that lz4 compress its input.

func NewWriter

func NewWriter(w io.Writer) *Writer

NewWriter creates a new Writer. Writes to the writer will be written in compressed form to w.

func (*Writer) Close

func (w *Writer) Close() error

Close releases all the resources occupied by Writer. w cannot be used after the release.

func (*Writer) Write

func (w *Writer) Write(src []byte) (int, error)

Write writes a compressed form of src to the underlying io.Writer.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL