Skip to content

Commit c9517b1

Browse files
klauspostnigeltao
authored andcommitted
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 <[email protected]> Reviewed-by: Joe Tsai <[email protected]> Run-TryBot: Joe Tsai <[email protected]> TryBot-Result: Gobot Gobot <[email protected]>
1 parent 7ea5829 commit c9517b1

File tree

3 files changed

+16
-9
lines changed

3 files changed

+16
-9
lines changed

src/compress/gzip/gzip.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const (
1919
BestSpeed = flate.BestSpeed
2020
BestCompression = flate.BestCompression
2121
DefaultCompression = flate.DefaultCompression
22+
HuffmanOnly = flate.HuffmanOnly
2223
)
2324

2425
// A Writer is an io.WriteCloser.
@@ -52,11 +53,11 @@ func NewWriter(w io.Writer) *Writer {
5253
// NewWriterLevel is like NewWriter but specifies the compression level instead
5354
// of assuming DefaultCompression.
5455
//
55-
// The compression level can be DefaultCompression, NoCompression, or any
56-
// integer value between BestSpeed and BestCompression inclusive. The error
57-
// returned will be nil if the level is valid.
56+
// The compression level can be DefaultCompression, NoCompression, HuffmanOnly
57+
// or any integer value between BestSpeed and BestCompression inclusive.
58+
// The error returned will be nil if the level is valid.
5859
func NewWriterLevel(w io.Writer, level int) (*Writer, error) {
59-
if level < DefaultCompression || level > BestCompression {
60+
if level < HuffmanOnly || level > BestCompression {
6061
return nil, fmt.Errorf("gzip: invalid compression level: %d", level)
6162
}
6263
z := new(Writer)

src/compress/zlib/writer.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const (
1919
BestSpeed = flate.BestSpeed
2020
BestCompression = flate.BestCompression
2121
DefaultCompression = flate.DefaultCompression
22+
HuffmanOnly = flate.HuffmanOnly
2223
)
2324

2425
// A Writer takes data written to it and writes the compressed
@@ -47,9 +48,9 @@ func NewWriter(w io.Writer) *Writer {
4748
// NewWriterLevel is like NewWriter but specifies the compression level instead
4849
// of assuming DefaultCompression.
4950
//
50-
// The compression level can be DefaultCompression, NoCompression, or any
51-
// integer value between BestSpeed and BestCompression inclusive. The error
52-
// returned will be nil if the level is valid.
51+
// The compression level can be DefaultCompression, NoCompression, HuffmanOnly
52+
// or any integer value between BestSpeed and BestCompression inclusive.
53+
// The error returned will be nil if the level is valid.
5354
func NewWriterLevel(w io.Writer, level int) (*Writer, error) {
5455
return NewWriterLevelDict(w, level, nil)
5556
}
@@ -60,7 +61,7 @@ func NewWriterLevel(w io.Writer, level int) (*Writer, error) {
6061
// The dictionary may be nil. If not, its contents should not be modified until
6162
// the Writer is closed.
6263
func NewWriterLevelDict(w io.Writer, level int, dict []byte) (*Writer, error) {
63-
if level < DefaultCompression || level > BestCompression {
64+
if level < HuffmanOnly || level > BestCompression {
6465
return nil, fmt.Errorf("zlib: invalid compression level: %d", level)
6566
}
6667
return &Writer{
@@ -99,7 +100,7 @@ func (z *Writer) writeHeader() (err error) {
99100
// The next bit, FDICT, is set if a dictionary is given.
100101
// The final five FCHECK bits form a mod-31 checksum.
101102
switch z.level {
102-
case 0, 1:
103+
case -2, 0, 1:
103104
z.scratch[1] = 0 << 6
104105
case 2, 3, 4, 5:
105106
z.scratch[1] = 1 << 6

src/compress/zlib/writer_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ func TestWriter(t *testing.T) {
147147
tag := fmt.Sprintf("#%d", i)
148148
testLevelDict(t, tag, b, DefaultCompression, "")
149149
testLevelDict(t, tag, b, NoCompression, "")
150+
testLevelDict(t, tag, b, HuffmanOnly, "")
150151
for level := BestSpeed; level <= BestCompression; level++ {
151152
testLevelDict(t, tag, b, level, "")
152153
}
@@ -157,6 +158,7 @@ func TestWriterBig(t *testing.T) {
157158
for i, fn := range filenames {
158159
testFileLevelDict(t, fn, DefaultCompression, "")
159160
testFileLevelDict(t, fn, NoCompression, "")
161+
testFileLevelDict(t, fn, HuffmanOnly, "")
160162
for level := BestSpeed; level <= BestCompression; level++ {
161163
testFileLevelDict(t, fn, level, "")
162164
if level >= 1 && testing.Short() && testenv.Builder() == "" {
@@ -174,6 +176,7 @@ func TestWriterDict(t *testing.T) {
174176
for i, fn := range filenames {
175177
testFileLevelDict(t, fn, DefaultCompression, dictionary)
176178
testFileLevelDict(t, fn, NoCompression, dictionary)
179+
testFileLevelDict(t, fn, HuffmanOnly, dictionary)
177180
for level := BestSpeed; level <= BestCompression; level++ {
178181
testFileLevelDict(t, fn, level, dictionary)
179182
if level >= 1 && testing.Short() && testenv.Builder() == "" {
@@ -191,8 +194,10 @@ func TestWriterReset(t *testing.T) {
191194
for _, fn := range filenames {
192195
testFileLevelDictReset(t, fn, NoCompression, nil)
193196
testFileLevelDictReset(t, fn, DefaultCompression, nil)
197+
testFileLevelDictReset(t, fn, HuffmanOnly, nil)
194198
testFileLevelDictReset(t, fn, NoCompression, []byte(dictionary))
195199
testFileLevelDictReset(t, fn, DefaultCompression, []byte(dictionary))
200+
testFileLevelDictReset(t, fn, HuffmanOnly, []byte(dictionary))
196201
if testing.Short() {
197202
break
198203
}

0 commit comments

Comments
 (0)