check writer level when use NewWriterLevel
Signed-off-by: luke <chenjinguang@huawei.com>
This commit is contained in:
parent
1d750214c2
commit
5b0203fbd3
|
@ -484,7 +484,7 @@ func BenchmarkEncodeLevels(b *testing.B) {
|
||||||
b.ReportAllocs()
|
b.ReportAllocs()
|
||||||
b.SetBytes(int64(len(opticks)))
|
b.SetBytes(int64(len(opticks)))
|
||||||
for i := 0; i < b.N; i++ {
|
for i := 0; i < b.N; i++ {
|
||||||
w := NewWriterLevel(ioutil.Discard, level)
|
w, _ := NewWriterLevel(ioutil.Discard, level)
|
||||||
w.Write(opticks)
|
w.Write(opticks)
|
||||||
w.Close()
|
w.Close()
|
||||||
}
|
}
|
||||||
|
@ -500,7 +500,7 @@ func BenchmarkEncodeLevelsReset(b *testing.B) {
|
||||||
|
|
||||||
for level := BestSpeed; level <= BestCompression; level++ {
|
for level := BestSpeed; level <= BestCompression; level++ {
|
||||||
buf := new(bytes.Buffer)
|
buf := new(bytes.Buffer)
|
||||||
w := NewWriterLevel(buf, level)
|
w, _ := NewWriterLevel(buf, level)
|
||||||
w.Write(opticks)
|
w.Write(opticks)
|
||||||
w.Close()
|
w.Close()
|
||||||
b.Run(fmt.Sprintf("%d", level), func(b *testing.B) {
|
b.Run(fmt.Sprintf("%d", level), func(b *testing.B) {
|
||||||
|
@ -524,7 +524,7 @@ func BenchmarkDecodeLevels(b *testing.B) {
|
||||||
|
|
||||||
for level := BestSpeed; level <= BestCompression; level++ {
|
for level := BestSpeed; level <= BestCompression; level++ {
|
||||||
buf := new(bytes.Buffer)
|
buf := new(bytes.Buffer)
|
||||||
w := NewWriterLevel(buf, level)
|
w, _ := NewWriterLevel(buf, level)
|
||||||
w.Write(opticks)
|
w.Write(opticks)
|
||||||
w.Close()
|
w.Close()
|
||||||
compressed := buf.Bytes()
|
compressed := buf.Bytes()
|
||||||
|
|
11
writer.go
11
writer.go
|
@ -2,6 +2,7 @@ package brotli
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -30,17 +31,21 @@ var (
|
||||||
// It is the caller's responsibility to call Close on the Writer when done.
|
// It is the caller's responsibility to call Close on the Writer when done.
|
||||||
// Writes may be buffered and not flushed until Close.
|
// Writes may be buffered and not flushed until Close.
|
||||||
func NewWriter(dst io.Writer) *Writer {
|
func NewWriter(dst io.Writer) *Writer {
|
||||||
return NewWriterLevel(dst, DefaultCompression)
|
b, _ := NewWriterLevel(dst, DefaultCompression)
|
||||||
|
return b
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewWriterLevel is like NewWriter but specifies the compression level instead
|
// NewWriterLevel is like NewWriter but specifies the compression level instead
|
||||||
// of assuming DefaultCompression.
|
// of assuming DefaultCompression.
|
||||||
// The compression level can be DefaultCompression or any integer value between
|
// The compression level can be DefaultCompression or any integer value between
|
||||||
// BestSpeed and BestCompression inclusive.
|
// BestSpeed and BestCompression inclusive.
|
||||||
func NewWriterLevel(dst io.Writer, level int) *Writer {
|
func NewWriterLevel(dst io.Writer, level int) (*Writer, error) {
|
||||||
|
if level < BestSpeed || level > BestCompression {
|
||||||
|
return nil, fmt.Errorf("brotli: invalid compression level: %d", level)
|
||||||
|
}
|
||||||
return NewWriterOptions(dst, WriterOptions{
|
return NewWriterOptions(dst, WriterOptions{
|
||||||
Quality: level,
|
Quality: level,
|
||||||
})
|
}), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewWriterOptions is like NewWriter but specifies WriterOptions
|
// NewWriterOptions is like NewWriter but specifies WriterOptions
|
||||||
|
|
Loading…
Reference in New Issue