github.com/mutagen-io/mutagen@v0.18.0-rc1/sspl/pkg/compression/zstd/zstd.go (about)

     1  //go:build mutagensspl
     2  
     3  // Copyright (c) 2023-present Mutagen IO, Inc.
     4  //
     5  // This program is free software: you can redistribute it and/or modify it under
     6  // the terms of the Server Side Public License, version 1, as published by
     7  // MongoDB, Inc.
     8  //
     9  // This program is distributed in the hope that it will be useful, but WITHOUT
    10  // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    11  // FOR A PARTICULAR PURPOSE. See the Server Side Public License for more
    12  // details.
    13  //
    14  // You should have received a copy of the Server Side Public License along with
    15  // this program. If not, see
    16  // <http://www.mongodb.com/licensing/server-side-public-license>.
    17  
    18  package zstd
    19  
    20  import (
    21  	"io"
    22  
    23  	"github.com/klauspost/compress/zstd"
    24  
    25  	"github.com/mutagen-io/mutagen/pkg/stream"
    26  )
    27  
    28  // NewDecompressor creates a new Zstandard decompressor that reads from the
    29  // specified stream with a default configuration.
    30  func NewDecompressor(compressed io.Reader) io.ReadCloser {
    31  	// Create the decompressor. We check for errors, but we don't include them
    32  	// as part of the interface because they can only occur with an invalid
    33  	// decompressor configuration (which can't occur when we only use defaults).
    34  	decompressor, err := zstd.NewReader(compressed)
    35  	if err != nil {
    36  		panic("Zstandard decompressor construction failed")
    37  	}
    38  
    39  	// Adapt the decompressor to the expected interface.
    40  	return decompressor.IOReadCloser()
    41  }
    42  
    43  // NewCompressor creates a new Zstandard compressor that writes to the specified
    44  // stream with a default configuration.
    45  func NewCompressor(compressed io.Writer) stream.WriteFlushCloser {
    46  	// Create the compressor. We check for errors, but we don't include them as
    47  	// part of the interface because they can only occur with an invalid
    48  	// compressor configuration (which can't occur when we only use defaults).
    49  	compressor, err := zstd.NewWriter(compressed)
    50  	if err != nil {
    51  		panic("Zstandard compressor construction failed")
    52  	}
    53  
    54  	// Success.
    55  	return compressor
    56  }