mirror of https://github.com/golang/go.git
compress/gzip, compress/zlib: add HuffmanOnly as compression levels.
This exposes HuffmanOnly in zlib and gzip packages, which is currently unavailable. Change-Id: If5d103bbc8b5fce2f5d740fd103a235c5d1ed7cd Reviewed-on: https://go-review.googlesource.com/31186 Reviewed-by: Nigel Tao <nigeltao@golang.org> Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com> Run-TryBot: Joe Tsai <thebrokentoaster@gmail.com> TryBot-Result: Gobot Gobot <gobot@golang.org>
This commit is contained in:
parent
7ea5829717
commit
c9517b1ffe
|
|
@ -19,6 +19,7 @@ const (
|
|||
BestSpeed = flate.BestSpeed
|
||||
BestCompression = flate.BestCompression
|
||||
DefaultCompression = flate.DefaultCompression
|
||||
HuffmanOnly = flate.HuffmanOnly
|
||||
)
|
||||
|
||||
// A Writer is an io.WriteCloser.
|
||||
|
|
@ -52,11 +53,11 @@ func NewWriter(w io.Writer) *Writer {
|
|||
// NewWriterLevel is like NewWriter but specifies the compression level instead
|
||||
// of assuming DefaultCompression.
|
||||
//
|
||||
// The compression level can be DefaultCompression, NoCompression, or any
|
||||
// integer value between BestSpeed and BestCompression inclusive. The error
|
||||
// returned will be nil if the level is valid.
|
||||
// The compression level can be DefaultCompression, NoCompression, HuffmanOnly
|
||||
// or any integer value between BestSpeed and BestCompression inclusive.
|
||||
// The error returned will be nil if the level is valid.
|
||||
func NewWriterLevel(w io.Writer, level int) (*Writer, error) {
|
||||
if level < DefaultCompression || level > BestCompression {
|
||||
if level < HuffmanOnly || level > BestCompression {
|
||||
return nil, fmt.Errorf("gzip: invalid compression level: %d", level)
|
||||
}
|
||||
z := new(Writer)
|
||||
|
|
|
|||
|
|
@ -19,6 +19,7 @@ const (
|
|||
BestSpeed = flate.BestSpeed
|
||||
BestCompression = flate.BestCompression
|
||||
DefaultCompression = flate.DefaultCompression
|
||||
HuffmanOnly = flate.HuffmanOnly
|
||||
)
|
||||
|
||||
// A Writer takes data written to it and writes the compressed
|
||||
|
|
@ -47,9 +48,9 @@ func NewWriter(w io.Writer) *Writer {
|
|||
// NewWriterLevel is like NewWriter but specifies the compression level instead
|
||||
// of assuming DefaultCompression.
|
||||
//
|
||||
// The compression level can be DefaultCompression, NoCompression, or any
|
||||
// integer value between BestSpeed and BestCompression inclusive. The error
|
||||
// returned will be nil if the level is valid.
|
||||
// The compression level can be DefaultCompression, NoCompression, HuffmanOnly
|
||||
// or any integer value between BestSpeed and BestCompression inclusive.
|
||||
// The error returned will be nil if the level is valid.
|
||||
func NewWriterLevel(w io.Writer, level int) (*Writer, error) {
|
||||
return NewWriterLevelDict(w, level, nil)
|
||||
}
|
||||
|
|
@ -60,7 +61,7 @@ func NewWriterLevel(w io.Writer, level int) (*Writer, error) {
|
|||
// The dictionary may be nil. If not, its contents should not be modified until
|
||||
// the Writer is closed.
|
||||
func NewWriterLevelDict(w io.Writer, level int, dict []byte) (*Writer, error) {
|
||||
if level < DefaultCompression || level > BestCompression {
|
||||
if level < HuffmanOnly || level > BestCompression {
|
||||
return nil, fmt.Errorf("zlib: invalid compression level: %d", level)
|
||||
}
|
||||
return &Writer{
|
||||
|
|
@ -99,7 +100,7 @@ func (z *Writer) writeHeader() (err error) {
|
|||
// The next bit, FDICT, is set if a dictionary is given.
|
||||
// The final five FCHECK bits form a mod-31 checksum.
|
||||
switch z.level {
|
||||
case 0, 1:
|
||||
case -2, 0, 1:
|
||||
z.scratch[1] = 0 << 6
|
||||
case 2, 3, 4, 5:
|
||||
z.scratch[1] = 1 << 6
|
||||
|
|
|
|||
|
|
@ -147,6 +147,7 @@ func TestWriter(t *testing.T) {
|
|||
tag := fmt.Sprintf("#%d", i)
|
||||
testLevelDict(t, tag, b, DefaultCompression, "")
|
||||
testLevelDict(t, tag, b, NoCompression, "")
|
||||
testLevelDict(t, tag, b, HuffmanOnly, "")
|
||||
for level := BestSpeed; level <= BestCompression; level++ {
|
||||
testLevelDict(t, tag, b, level, "")
|
||||
}
|
||||
|
|
@ -157,6 +158,7 @@ func TestWriterBig(t *testing.T) {
|
|||
for i, fn := range filenames {
|
||||
testFileLevelDict(t, fn, DefaultCompression, "")
|
||||
testFileLevelDict(t, fn, NoCompression, "")
|
||||
testFileLevelDict(t, fn, HuffmanOnly, "")
|
||||
for level := BestSpeed; level <= BestCompression; level++ {
|
||||
testFileLevelDict(t, fn, level, "")
|
||||
if level >= 1 && testing.Short() && testenv.Builder() == "" {
|
||||
|
|
@ -174,6 +176,7 @@ func TestWriterDict(t *testing.T) {
|
|||
for i, fn := range filenames {
|
||||
testFileLevelDict(t, fn, DefaultCompression, dictionary)
|
||||
testFileLevelDict(t, fn, NoCompression, dictionary)
|
||||
testFileLevelDict(t, fn, HuffmanOnly, dictionary)
|
||||
for level := BestSpeed; level <= BestCompression; level++ {
|
||||
testFileLevelDict(t, fn, level, dictionary)
|
||||
if level >= 1 && testing.Short() && testenv.Builder() == "" {
|
||||
|
|
@ -191,8 +194,10 @@ func TestWriterReset(t *testing.T) {
|
|||
for _, fn := range filenames {
|
||||
testFileLevelDictReset(t, fn, NoCompression, nil)
|
||||
testFileLevelDictReset(t, fn, DefaultCompression, nil)
|
||||
testFileLevelDictReset(t, fn, HuffmanOnly, nil)
|
||||
testFileLevelDictReset(t, fn, NoCompression, []byte(dictionary))
|
||||
testFileLevelDictReset(t, fn, DefaultCompression, []byte(dictionary))
|
||||
testFileLevelDictReset(t, fn, HuffmanOnly, []byte(dictionary))
|
||||
if testing.Short() {
|
||||
break
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue