2024-05-17 18:30:33 +08:00
|
|
|
// Copyright 2021 GoEdge goedge.cdn@gmail.com. All rights reserved.
|
2023-11-21 20:18:37 +08:00
|
|
|
//go:build !plus || !linux
|
2021-10-18 16:50:06 +08:00
|
|
|
|
|
|
|
|
package compressions
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"github.com/andybalholm/brotli"
|
|
|
|
|
"io"
|
2021-12-29 10:57:15 +08:00
|
|
|
"strings"
|
2021-10-18 16:50:06 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type BrotliReader struct {
|
2022-03-20 00:05:47 +08:00
|
|
|
BaseReader
|
|
|
|
|
|
2021-10-18 16:50:06 +08:00
|
|
|
reader *brotli.Reader
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewBrotliReader(reader io.Reader) (Reader, error) {
|
2022-03-20 00:05:47 +08:00
|
|
|
return sharedBrotliReaderPool.Get(reader)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func newBrotliReader(reader io.Reader) (Reader, error) {
|
2021-10-18 16:50:06 +08:00
|
|
|
return &BrotliReader{reader: brotli.NewReader(reader)}, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (this *BrotliReader) Read(p []byte) (n int, err error) {
|
2021-12-29 10:57:15 +08:00
|
|
|
n, err = this.reader.Read(p)
|
|
|
|
|
if err != nil && strings.Contains(err.Error(), "excessive") {
|
|
|
|
|
err = io.EOF
|
|
|
|
|
}
|
|
|
|
|
return
|
2021-10-18 16:50:06 +08:00
|
|
|
}
|
|
|
|
|
|
2022-03-20 00:05:47 +08:00
|
|
|
func (this *BrotliReader) Reset(reader io.Reader) error {
|
|
|
|
|
return this.reader.Reset(reader)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (this *BrotliReader) RawClose() error {
|
2021-10-18 16:50:06 +08:00
|
|
|
return nil
|
|
|
|
|
}
|
2022-03-20 00:05:47 +08:00
|
|
|
|
|
|
|
|
func (this *BrotliReader) Close() error {
|
|
|
|
|
return this.Finish(this)
|
|
|
|
|
}
|