Files
EdgeNode/internal/compressions/writer_gzip.go

58 lines
1.0 KiB
Go
Raw Normal View History

2024-05-17 18:30:33 +08:00
// Copyright 2021 GoEdge goedge.cdn@gmail.com. All rights reserved.
2021-09-29 19:37:07 +08:00
package compressions
import (
2022-04-30 22:22:30 +08:00
"github.com/klauspost/compress/gzip"
2021-09-29 19:37:07 +08:00
"io"
)
type GzipWriter struct {
2022-03-20 00:05:47 +08:00
BaseWriter
2021-09-29 19:37:07 +08:00
writer *gzip.Writer
level int
}
func NewGzipWriter(writer io.Writer, level int) (Writer, error) {
2022-03-20 00:05:47 +08:00
return sharedGzipWriterPool.Get(writer, level)
}
func newGzipWriter(writer io.Writer) (Writer, error) {
var level = GenerateCompressLevel(gzip.BestSpeed, gzip.BestCompression)
2021-09-29 19:37:07 +08:00
gzipWriter, err := gzip.NewWriterLevel(writer, level)
if err != nil {
return nil, err
}
return &GzipWriter{
writer: gzipWriter,
level: level,
}, nil
}
func (this *GzipWriter) Write(p []byte) (int, error) {
return this.writer.Write(p)
}
func (this *GzipWriter) Flush() error {
return this.writer.Flush()
}
2022-03-20 00:05:47 +08:00
func (this *GzipWriter) Reset(writer io.Writer) {
this.writer.Reset(writer)
}
func (this *GzipWriter) RawClose() error {
2021-09-29 19:37:07 +08:00
return this.writer.Close()
}
2022-03-20 00:05:47 +08:00
func (this *GzipWriter) Close() error {
return this.Finish(this)
}
2021-09-29 19:37:07 +08:00
func (this *GzipWriter) Level() int {
return this.level
}