github.com/apache/arrow/go/v7@v7.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  	"io"
    22  	"io/ioutil"
    23  
    24  	"github.com/klauspost/compress/gzip"
    25  	"golang.org/x/xerrors"
    26  )
    27  
    28  type gzipCodec struct{}
    29  
    30  func (gzipCodec) NewReader(r io.Reader) io.ReadCloser {
    31  	ret, err := gzip.NewReader(r)
    32  	if err != nil {
    33  		panic(xerrors.Errorf("codec: gzip: %w", err))
    34  	}
    35  	return ret
    36  }
    37  
    38  func (gzipCodec) Decode(dst, src []byte) []byte {
    39  	rdr, err := gzip.NewReader(bytes.NewReader(src))
    40  	if err != nil {
    41  		panic(err)
    42  	}
    43  
    44  	if dst != nil {
    45  		n, err := io.ReadFull(rdr, dst)
    46  		if err != nil {
    47  			panic(err)
    48  		}
    49  		return dst[:n]
    50  	}
    51  
    52  	dst, err = ioutil.ReadAll(rdr)
    53  	if err != nil {
    54  		panic(err)
    55  	}
    56  
    57  	return dst
    58  }
    59  
    60  func (g gzipCodec) EncodeLevel(dst, src []byte, level int) []byte {
    61  	maxlen := int(g.CompressBound(int64(len(src))))
    62  	if dst == nil || cap(dst) < maxlen {
    63  		dst = make([]byte, 0, maxlen)
    64  	}
    65  	buf := bytes.NewBuffer(dst[:0])
    66  	w, err := gzip.NewWriterLevel(buf, level)
    67  	if err != nil {
    68  		panic(err)
    69  	}
    70  	_, err = w.Write(src)
    71  	if err != nil {
    72  		panic(err)
    73  	}
    74  	if err := w.Close(); err != nil {
    75  		panic(err)
    76  	}
    77  	return buf.Bytes()
    78  }
    79  
    80  func (g gzipCodec) Encode(dst, src []byte) []byte {
    81  	return g.EncodeLevel(dst, src, DefaultCompressionLevel)
    82  }
    83  
    84  func (gzipCodec) CompressBound(len int64) int64 {
    85  	return len + ((len + 7) >> 3) + ((len + 63) >> 6) + 5
    86  }
    87  
    88  func (gzipCodec) NewWriter(w io.Writer) io.WriteCloser {
    89  	return gzip.NewWriter(w)
    90  }
    91  
    92  func (gzipCodec) NewWriterLevel(w io.Writer, level int) (io.WriteCloser, error) {
    93  	return gzip.NewWriterLevel(w, level)
    94  }
    95  
    96  func init() {
    97  	codecs[Codecs.Gzip] = gzipCodec{}
    98  }