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

     1  // Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
     2  //go:build !plus || !linux
     3  
     4  package compressions
     5  
     6  import (
     7  	"github.com/andybalholm/brotli"
     8  	"io"
     9  )
    10  
    11  type BrotliWriter struct {
    12  	BaseWriter
    13  
    14  	writer *brotli.Writer
    15  	level  int
    16  }
    17  
    18  func NewBrotliWriter(writer io.Writer, level int) (Writer, error) {
    19  	return sharedBrotliWriterPool.Get(writer, level)
    20  }
    21  
    22  func newBrotliWriter(writer io.Writer) (*BrotliWriter, error) {
    23  	var level = GenerateCompressLevel(brotli.BestSpeed, brotli.BestCompression)
    24  	return &BrotliWriter{
    25  		writer: brotli.NewWriterOptions(writer, brotli.WriterOptions{
    26  			Quality: level,
    27  			LGWin:   14, // TODO 在全局设置里可以设置此值
    28  		}),
    29  		level: level,
    30  	}, nil
    31  }
    32  
    33  func (this *BrotliWriter) Write(p []byte) (int, error) {
    34  	return this.writer.Write(p)
    35  }
    36  
    37  func (this *BrotliWriter) Flush() error {
    38  	return this.writer.Flush()
    39  }
    40  
    41  func (this *BrotliWriter) Reset(newWriter io.Writer) {
    42  	this.writer.Reset(newWriter)
    43  }
    44  
    45  func (this *BrotliWriter) RawClose() error {
    46  	return this.writer.Close()
    47  }
    48  
    49  func (this *BrotliWriter) Close() error {
    50  	return this.Finish(this)
    51  }
    52  
    53  func (this *BrotliWriter) Level() int {
    54  	return this.level
    55  }