DetectCompressionFormat returns an Algorithm and DecompressorFunc if the input is recognized as a compressed format, an invalid value and nil otherwise. Because it consumes the start of input, other consumers must use the returned io.Reader instead to also read from the beginning.
(input io.Reader)
| 119 | // value and nil otherwise. |
| 120 | // Because it consumes the start of input, other consumers must use the returned io.Reader instead to also read from the beginning. |
| 121 | func DetectCompressionFormat(input io.Reader) (Algorithm, DecompressorFunc, io.Reader, error) { |
| 122 | buffer := [8]byte{} |
| 123 | |
| 124 | n, err := io.ReadAtLeast(input, buffer[:], len(buffer)) |
| 125 | if err != nil && err != io.EOF && err != io.ErrUnexpectedEOF { |
| 126 | // This is a “real” error. We could just ignore it this time, process the data we have, and hope that the source will report the same error again. |
| 127 | // Instead, fail immediately with the original error cause instead of a possibly secondary/misleading error returned later. |
| 128 | return Algorithm{}, nil, nil, err |
| 129 | } |
| 130 | |
| 131 | var retAlgo Algorithm |
| 132 | var decompressor DecompressorFunc |
| 133 | for _, algo := range compressionAlgorithms { |
| 134 | prefix := internal.AlgorithmPrefix(algo) |
| 135 | if len(prefix) > 0 && bytes.HasPrefix(buffer[:n], prefix) { |
| 136 | logrus.Debugf("Detected compression format %s", algo.Name()) |
| 137 | retAlgo = algo |
| 138 | decompressor = internal.AlgorithmDecompressor(algo) |
| 139 | break |
| 140 | } |
| 141 | } |
| 142 | if decompressor == nil { |
| 143 | logrus.Debugf("No compression detected") |
| 144 | } |
| 145 | |
| 146 | return retAlgo, decompressor, io.MultiReader(bytes.NewReader(buffer[:n]), input), nil |
| 147 | } |
| 148 | |
| 149 | // DetectCompression returns a DecompressorFunc if the input is recognized as a compressed format, nil otherwise. |
| 150 | // Because it consumes the start of input, other consumers must use the returned io.Reader instead to also read from the beginning. |
no test coverage detected
searching dependent graphs…