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

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