github.com/linuxboot/fiano@v1.2.0/pkg/compression/lz4.go (about)

     1  // Copyright 2018 the LinuxBoot 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 compression
     6  
     7  import (
     8  	"bytes"
     9  	"io"
    10  
    11  	"github.com/pierrec/lz4"
    12  )
    13  
    14  // LZ4 implements Compressor and uses a Go-based implementation.
    15  type LZ4 struct{}
    16  
    17  // Name returns the type of compression employed.
    18  func (c *LZ4) Name() string {
    19  	return "LZ4"
    20  }
    21  
    22  // Decode decodes a byte slice of LZ4 data.
    23  func (c *LZ4) Decode(encodedData []byte) ([]byte, error) {
    24  	return io.ReadAll(lz4.NewReader(bytes.NewBuffer(encodedData)))
    25  }
    26  
    27  // Encode encodes a byte slice with LZ4.
    28  func (c *LZ4) Encode(decodedData []byte) ([]byte, error) {
    29  
    30  	buf := bytes.Buffer{}
    31  	w := lz4.NewWriter(&buf)
    32  	_, err := w.Write(decodedData)
    33  	if err != nil {
    34  		return nil, err
    35  	}
    36  	w.Flush()
    37  
    38  	return buf.Bytes(), nil
    39  }