mirror of
https://github.com/TeaOSLab/EdgeNode.git
synced 2025-11-03 23:20:25 +08:00
60 lines
1.2 KiB
Go
60 lines
1.2 KiB
Go
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
|
//go:build !plus || !linux
|
|
|
|
package compressions
|
|
|
|
import (
|
|
"github.com/andybalholm/brotli"
|
|
"io"
|
|
)
|
|
|
|
type BrotliWriter struct {
|
|
BaseWriter
|
|
|
|
writer *brotli.Writer
|
|
level int
|
|
}
|
|
|
|
func NewBrotliWriter(writer io.Writer, level int) (Writer, error) {
|
|
return sharedBrotliWriterPool.Get(writer, level)
|
|
}
|
|
|
|
func newBrotliWriter(writer io.Writer, level int) (*BrotliWriter, error) {
|
|
if level <= 0 {
|
|
level = brotli.BestSpeed
|
|
} else if level > brotli.BestCompression {
|
|
level = brotli.BestCompression
|
|
}
|
|
return &BrotliWriter{
|
|
writer: brotli.NewWriterOptions(writer, brotli.WriterOptions{
|
|
Quality: level,
|
|
LGWin: 14, // TODO 在全局设置里可以设置此值
|
|
}),
|
|
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.Reset(newWriter)
|
|
}
|
|
|
|
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
|
|
}
|