MCPcopy
hub / github.com/klauspost/compress / Read

Method Read

s2/reader.go:244–400  ·  view source on GitHub ↗

Read satisfies the io.Reader interface.

(p []byte)

Source from the content-addressed store, hash-verified

242
243// Read satisfies the io.Reader interface.
244func (r *Reader) Read(p []byte) (int, error) {
245 if r.err != nil {
246 return 0, r.err
247 }
248 for {
249 if r.i < r.j {
250 n := copy(p, r.decoded[r.i:r.j])
251 r.i += n
252 return n, nil
253 }
254 if !r.readFull(r.buf[:4], true) {
255 return 0, r.err
256 }
257 chunkType := r.buf[0]
258 if !r.readHeader {
259 if chunkType != chunkTypeStreamIdentifier {
260 r.err = ErrCorrupt
261 return 0, r.err
262 }
263 r.readHeader = true
264 }
265 chunkLen := int(r.buf[1]) | int(r.buf[2])<<8 | int(r.buf[3])<<16
266
267 // The chunk types are specified at
268 // https://github.com/google/snappy/blob/master/framing_format.txt
269 switch chunkType {
270 case chunkTypeCompressedData:
271 r.blockStart += int64(r.j)
272 // Section 4.2. Compressed data (chunk type 0x00).
273 if chunkLen < checksumSize {
274 r.err = ErrCorrupt
275 return 0, r.err
276 }
277 if !r.ensureBufferSize(chunkLen) {
278 if r.err == nil {
279 r.err = ErrUnsupported
280 }
281 return 0, r.err
282 }
283 buf := r.buf[:chunkLen]
284 if !r.readFull(buf, false) {
285 return 0, r.err
286 }
287 checksum := uint32(buf[0]) | uint32(buf[1])<<8 | uint32(buf[2])<<16 | uint32(buf[3])<<24
288 buf = buf[checksumSize:]
289
290 n, err := DecodedLen(buf)
291 if err != nil {
292 r.err = err
293 return 0, r.err
294 }
295 if r.snappyFrame && n > maxSnappyBlockSize {
296 r.err = ErrCorrupt
297 return 0, r.err
298 }
299
300 if n > len(r.decoded) {
301 if n > r.maxBlock {

Callers 13

testSyncFunction · 0.95
TestNewReaderReadFunction · 0.95
TestNewDecoderFlushedFunction · 0.95
TestReaderResetFunction · 0.95
ReadByteMethod · 0.95
TestReaderResetFunction · 0.95
BenchmarkEstimateFunction · 0.45
SeekMethod · 0.45
ReadAtMethod · 0.45
ExampleIndex_LoadFunction · 0.45

Calls 6

readFullMethod · 0.95
ensureBufferSizeMethod · 0.95
skippableMethod · 0.95
DecodedLenFunction · 0.70
DecodeFunction · 0.70
crcFunction · 0.70

Tested by 10

testSyncFunction · 0.76
TestNewReaderReadFunction · 0.76
TestNewDecoderFlushedFunction · 0.76
TestReaderResetFunction · 0.76
TestReaderResetFunction · 0.76
BenchmarkEstimateFunction · 0.36
ExampleIndex_LoadFunction · 0.36
ExampleIndexStreamFunction · 0.36