Files
EdgeNode/internal/compressions/writer_brotli.go

56 lines
1.1 KiB
Go
Raw Normal View History

2024-05-17 18:30:33 +08:00
// Copyright 2021 GoEdge goedge.cdn@gmail.com. All rights reserved.
2023-11-21 20:18:37 +08:00
//go:build !plus || !linux
2021-09-29 19:37:07 +08:00
package compressions
import (
"github.com/andybalholm/brotli"
"io"
)
type BrotliWriter struct {
2022-03-20 00:05:47 +08:00
BaseWriter
2021-09-29 19:37:07 +08:00
writer *brotli.Writer
level int
}
func NewBrotliWriter(writer io.Writer, level int) (Writer, error) {
2022-03-20 00:05:47 +08:00
return sharedBrotliWriterPool.Get(writer, level)
}
func newBrotliWriter(writer io.Writer) (*BrotliWriter, error) {
var level = GenerateCompressLevel(brotli.BestSpeed, brotli.BestCompression)
2021-09-29 19:37:07 +08:00
return &BrotliWriter{
2022-03-19 19:28:20 +08:00
writer: brotli.NewWriterOptions(writer, brotli.WriterOptions{
Quality: level,
2023-11-21 20:18:37 +08:00
LGWin: 14, // TODO 在全局设置里可以设置此值
2022-03-19 19:28:20 +08:00
}),
level: level,
2021-09-29 19:37:07 +08:00
}, nil
}
func (this *BrotliWriter) Write(p []byte) (int, error) {
return this.writer.Write(p)
}
func (this *BrotliWriter) Flush() error {
return this.writer.Flush()
}
2022-03-20 00:05:47 +08:00
func (this *BrotliWriter) Reset(newWriter io.Writer) {
this.writer.Reset(newWriter)
}
func (this *BrotliWriter) RawClose() error {
2021-09-29 19:37:07 +08:00
return this.writer.Close()
}
2022-03-20 00:05:47 +08:00
func (this *BrotliWriter) Close() error {
return this.Finish(this)
}
2021-09-29 19:37:07 +08:00
func (this *BrotliWriter) Level() int {
return this.level
}