github.com/apache/arrow/go/v14@v14.0.1/parquet/compress/gzip.go (about) 1 // Licensed to the Apache Software Foundation (ASF) under one 2 // or more contributor license agreements. See the NOTICE file 3 // distributed with this work for additional information 4 // regarding copyright ownership. The ASF licenses this file 5 // to you under the Apache License, Version 2.0 (the 6 // "License"); you may not use this file except in compliance 7 // with the License. You may obtain a copy of the License at 8 // 9 // http://www.apache.org/licenses/LICENSE-2.0 10 // 11 // Unless required by applicable law or agreed to in writing, software 12 // distributed under the License is distributed on an "AS IS" BASIS, 13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 // See the License for the specific language governing permissions and 15 // limitations under the License. 16 17 package compress 18 19 import ( 20 "bytes" 21 "fmt" 22 "io" 23 24 "github.com/klauspost/compress/gzip" 25 ) 26 27 type gzipCodec struct{} 28 29 func (gzipCodec) NewReader(r io.Reader) io.ReadCloser { 30 ret, err := gzip.NewReader(r) 31 if err != nil { 32 panic(fmt.Errorf("codec: gzip: %w", err)) 33 } 34 return ret 35 } 36 37 func (gzipCodec) Decode(dst, src []byte) []byte { 38 rdr, err := gzip.NewReader(bytes.NewReader(src)) 39 if err != nil { 40 panic(err) 41 } 42 43 if dst != nil { 44 n, err := io.ReadFull(rdr, dst) 45 if err != nil { 46 panic(err) 47 } 48 return dst[:n] 49 } 50 51 dst, err = io.ReadAll(rdr) 52 if err != nil { 53 panic(err) 54 } 55 56 return dst 57 } 58 59 func (g gzipCodec) EncodeLevel(dst, src []byte, level int) []byte { 60 maxlen := int(g.CompressBound(int64(len(src)))) 61 if dst == nil || cap(dst) < maxlen { 62 dst = make([]byte, 0, maxlen) 63 } 64 buf := bytes.NewBuffer(dst[:0]) 65 w, err := gzip.NewWriterLevel(buf, level) 66 if err != nil { 67 panic(err) 68 } 69 _, err = w.Write(src) 70 if err != nil { 71 panic(err) 72 } 73 if err := w.Close(); err != nil { 74 panic(err) 75 } 76 return buf.Bytes() 77 } 78 79 func (g gzipCodec) Encode(dst, src []byte) []byte { 80 return g.EncodeLevel(dst, src, DefaultCompressionLevel) 81 } 82 83 func (gzipCodec) CompressBound(len int64) int64 { 84 return len + ((len + 7) >> 3) + ((len + 63) >> 6) + 5 85 } 86 87 func (gzipCodec) NewWriter(w io.Writer) io.WriteCloser { 88 return gzip.NewWriter(w) 89 } 90 91 func (gzipCodec) NewWriterLevel(w io.Writer, level int) (io.WriteCloser, error) { 92 return gzip.NewWriterLevel(w, level) 93 } 94 95 func init() { 96 codecs[Codecs.Gzip] = gzipCodec{} 97 }