Files
EdgeNode/internal/compressions/writer_gzip.go

62 lines
1.1 KiB
Go
Raw Normal View History

2021-09-29 19:37:07 +08:00
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
package compressions
import (
"compress/gzip"
"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, level int) (Writer, error) {
2021-09-29 19:37:07 +08:00
if level <= 0 {
level = gzip.BestSpeed
} else if level > gzip.BestCompression {
level = gzip.BestCompression
}
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
}