NewReader creates a new decoder. A nil Reader can be provided in which case Reset can be used to start a decode. A Decoder can be used in two modes: 1) As a stream, or 2) For stateless decoding using DecodeAll. Only a single stream can be decoded concurrently, but the same decoder can run multipl
(r io.Reader, opts ...DOption)
| 82 | // The Reset function can be used to initiate a new stream, which will considerably |
| 83 | // reduce the allocations normally caused by NewReader. |
| 84 | func NewReader(r io.Reader, opts ...DOption) (*Decoder, error) { |
| 85 | initPredefined() |
| 86 | var d Decoder |
| 87 | d.o.setDefault() |
| 88 | for _, o := range opts { |
| 89 | err := o(&d.o) |
| 90 | if err != nil { |
| 91 | return nil, err |
| 92 | } |
| 93 | } |
| 94 | d.current.crc = xxhash.New() |
| 95 | d.current.flushed = true |
| 96 | |
| 97 | if r == nil { |
| 98 | d.current.err = ErrDecoderNilInput |
| 99 | } |
| 100 | |
| 101 | // Initialize dict map if needed. |
| 102 | if d.o.dicts == nil { |
| 103 | d.o.dicts = make(map[uint32]*dict) |
| 104 | } |
| 105 | |
| 106 | // Create decoders |
| 107 | d.decoders = make(chan *blockDec, d.o.concurrent) |
| 108 | for i := 0; i < d.o.concurrent; i++ { |
| 109 | dec := newBlockDec(d.o.lowMem) |
| 110 | dec.localFrame = newFrameDec(d.o) |
| 111 | d.decoders <- dec |
| 112 | } |
| 113 | |
| 114 | if r == nil { |
| 115 | return &d, nil |
| 116 | } |
| 117 | return &d, d.Reset(r) |
| 118 | } |
| 119 | |
| 120 | // Read bytes from the decompressed stream into p. |
| 121 | // Returns the number of bytes read and any error that occurred. |
searching dependent graphs…