github.com/zntrio/harp/v2@v2.0.9/pkg/sdk/value/compression/reader.go (about)

     1  // Licensed to Elasticsearch B.V. under one or more contributor
     2  // license agreements. See the NOTICE file distributed with
     3  // this work for additional information regarding copyright
     4  // ownership. Elasticsearch B.V. licenses this file to you under
     5  // the Apache License, Version 2.0 (the "License"); you may
     6  // not use this file except in compliance with the License.
     7  // You may obtain a copy of the License at
     8  //
     9  //     http://www.apache.org/licenses/LICENSE-2.0
    10  //
    11  // Unless required by applicable law or agreed to in writing,
    12  // software distributed under the License is distributed on an
    13  // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    14  // KIND, either express or implied.  See the License for the
    15  // specific language governing permissions and limitations
    16  // under the License.
    17  
    18  package compression
    19  
    20  import (
    21  	"compress/lzw"
    22  	"fmt"
    23  	"io"
    24  	"strings"
    25  
    26  	"github.com/klauspost/compress/flate"
    27  	"github.com/klauspost/compress/gzip"
    28  	"github.com/klauspost/compress/s2"
    29  	"github.com/klauspost/compress/zlib"
    30  	"github.com/klauspost/compress/zstd"
    31  	"github.com/pierrec/lz4"
    32  	"github.com/ulikunitz/xz"
    33  )
    34  
    35  // -----------------------------------------------------------------------------
    36  
    37  // NewReader returns a writer implementation according to given algorithm.
    38  func NewReader(r io.Reader, algorithm string) (io.ReadCloser, error) {
    39  	// Normalize input
    40  	algorithm = strings.TrimSpace(strings.ToLower(algorithm))
    41  
    42  	var (
    43  		compressedReader io.ReadCloser
    44  		readerErr        error
    45  	)
    46  
    47  	// Apply transformation
    48  	switch algorithm {
    49  	case "identity":
    50  		compressedReader = io.NopCloser(r)
    51  	case "gzip":
    52  		compressedReader, readerErr = gzip.NewReader(r)
    53  	case "lzw", "lzw-lsb":
    54  		compressedReader = lzw.NewReader(r, lzw.LSB, 8)
    55  	case "lzw-msb":
    56  		compressedReader = lzw.NewReader(r, lzw.MSB, 8)
    57  	case "lz4":
    58  		compressedReader = io.NopCloser(lz4.NewReader(r))
    59  	case "s2", "snappy":
    60  		compressedReader = io.NopCloser(s2.NewReader(r))
    61  	case "zlib":
    62  		compressedReader, readerErr = zlib.NewReader(r)
    63  	case "flate", "deflate":
    64  		compressedReader = flate.NewReader(r)
    65  	case "lzma":
    66  		reader, err := xz.NewReader(r)
    67  		if err != nil {
    68  			readerErr = err
    69  		} else {
    70  			compressedReader = io.NopCloser(reader)
    71  		}
    72  	case "zstd":
    73  		reader, err := zstd.NewReader(r)
    74  		if err != nil {
    75  			readerErr = err
    76  		} else {
    77  			compressedReader = reader.IOReadCloser()
    78  		}
    79  	default:
    80  		return nil, fmt.Errorf("unhandled compression algorithm %q", algorithm)
    81  	}
    82  	if readerErr != nil {
    83  		return nil, fmt.Errorf("unable to initialize %q compressor: %w", algorithm, readerErr)
    84  	}
    85  
    86  	// No error
    87  	return compressedReader, nil
    88  }