github.com/TeaOSLab/EdgeNode@v1.3.8/internal/compressions/writer_gzip.go (about) 1 // Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved. 2 3 package compressions 4 5 import ( 6 "github.com/klauspost/compress/gzip" 7 "io" 8 ) 9 10 type GzipWriter struct { 11 BaseWriter 12 13 writer *gzip.Writer 14 level int 15 } 16 17 func NewGzipWriter(writer io.Writer, level int) (Writer, error) { 18 return sharedGzipWriterPool.Get(writer, level) 19 } 20 21 func newGzipWriter(writer io.Writer) (Writer, error) { 22 var level = GenerateCompressLevel(gzip.BestSpeed, gzip.BestCompression) 23 24 gzipWriter, err := gzip.NewWriterLevel(writer, level) 25 if err != nil { 26 return nil, err 27 } 28 29 return &GzipWriter{ 30 writer: gzipWriter, 31 level: level, 32 }, nil 33 } 34 35 func (this *GzipWriter) Write(p []byte) (int, error) { 36 return this.writer.Write(p) 37 } 38 39 func (this *GzipWriter) Flush() error { 40 return this.writer.Flush() 41 } 42 43 func (this *GzipWriter) Reset(writer io.Writer) { 44 this.writer.Reset(writer) 45 } 46 47 func (this *GzipWriter) RawClose() error { 48 return this.writer.Close() 49 } 50 51 func (this *GzipWriter) Close() error { 52 return this.Finish(this) 53 } 54 55 func (this *GzipWriter) Level() int { 56 return this.level 57 }