72 lines
1.4 KiB
Go
72 lines
1.4 KiB
Go
// Copyright 2023 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
|
//go:build plus && linux
|
|
|
|
package compressions
|
|
|
|
import (
|
|
"github.com/TeaOSLab/EdgeNode/internal/compressions/cbrotli"
|
|
"io"
|
|
)
|
|
|
|
const (
|
|
BrotliBestSpeed = 0
|
|
BrotliBestCompression = 11
|
|
|
|
BrotliDefaultLGWin = 14
|
|
)
|
|
|
|
type BrotliWriter struct {
|
|
BaseWriter
|
|
|
|
writer *cbrotli.Writer
|
|
|
|
level int
|
|
}
|
|
|
|
func NewBrotliWriter(writer io.Writer, level int) (Writer, error) {
|
|
return sharedBrotliWriterPool.Get(writer, level)
|
|
}
|
|
|
|
func newBrotliWriter(writer io.Writer) (*BrotliWriter, error) {
|
|
var level = GenerateCompressLevel(BrotliBestSpeed, BrotliBestCompression)
|
|
|
|
return &BrotliWriter{
|
|
writer: cbrotli.NewWriter(writer, cbrotli.WriterOptions{
|
|
Quality: level,
|
|
LGWin: BrotliDefaultLGWin,
|
|
}),
|
|
level: level,
|
|
}, nil
|
|
}
|
|
|
|
func (this *BrotliWriter) Write(p []byte) (int, error) {
|
|
return this.writer.Write(p)
|
|
}
|
|
|
|
func (this *BrotliWriter) Flush() error {
|
|
return this.writer.Flush()
|
|
}
|
|
|
|
func (this *BrotliWriter) Reset(newWriter io.Writer) {
|
|
_ = this.writer.Close()
|
|
if newWriter == nil {
|
|
return
|
|
}
|
|
this.writer = cbrotli.NewWriter(newWriter, cbrotli.WriterOptions{
|
|
Quality: this.level,
|
|
LGWin: BrotliDefaultLGWin,
|
|
})
|
|
}
|
|
|
|
func (this *BrotliWriter) RawClose() error {
|
|
return this.writer.Close()
|
|
}
|
|
|
|
func (this *BrotliWriter) Close() error {
|
|
return this.Finish(this)
|
|
}
|
|
|
|
func (this *BrotliWriter) Level() int {
|
|
return this.level
|
|
}
|