github.com/TeaOSLab/EdgeNode@v1.3.8/internal/compressions/writer_zstd.go (about)

     1  // Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
     2  
     3  package compressions
     4  
     5  import (
     6  	"github.com/klauspost/compress/zstd"
     7  	"io"
     8  )
     9  
    10  type ZSTDWriter struct {
    11  	BaseWriter
    12  
    13  	writer *zstd.Encoder
    14  	level  int
    15  }
    16  
    17  func NewZSTDWriter(writer io.Writer, level int) (Writer, error) {
    18  	return sharedZSTDWriterPool.Get(writer, level)
    19  }
    20  
    21  func newZSTDWriter(writer io.Writer) (Writer, error) {
    22  	var level = 1
    23  	var zstdLevel = zstd.SpeedFastest
    24  
    25  	zstdWriter, err := zstd.NewWriter(writer, zstd.WithEncoderLevel(zstdLevel), zstd.WithWindowSize(16<<10), zstd.WithLowerEncoderMem(true))
    26  	if err != nil {
    27  		return nil, err
    28  	}
    29  
    30  	return &ZSTDWriter{
    31  		writer:    zstdWriter,
    32  		level:     level,
    33  	}, nil
    34  }
    35  
    36  func (this *ZSTDWriter) Write(p []byte) (int, error) {
    37  	return this.writer.Write(p)
    38  }
    39  
    40  func (this *ZSTDWriter) Flush() error {
    41  	return this.writer.Flush()
    42  }
    43  
    44  func (this *ZSTDWriter) Reset(writer io.Writer) {
    45  	this.writer.Reset(writer)
    46  }
    47  
    48  func (this *ZSTDWriter) RawClose() error {
    49  	return this.writer.Close()
    50  }
    51  
    52  func (this *ZSTDWriter) Close() error {
    53  	return this.Finish(this)
    54  }
    55  
    56  func (this *ZSTDWriter) Level() int {
    57  	return this.level
    58  }