ReadFrom reads data from r until EOF or error. The return value n is the number of bytes read. Any error except io.EOF encountered during the read is also returned. The Copy function uses ReaderFrom if available.
(r io.Reader)
| 442 | // |
| 443 | // The Copy function uses ReaderFrom if available. |
| 444 | func (e *Encoder) ReadFrom(r io.Reader) (n int64, err error) { |
| 445 | if debugEncoder { |
| 446 | println("Using ReadFrom") |
| 447 | } |
| 448 | |
| 449 | if e.o.concurrentBlocks { |
| 450 | return e.readFromJobs(r) |
| 451 | } |
| 452 | |
| 453 | // Flush any current writes. |
| 454 | if len(e.state.filling) > 0 { |
| 455 | if err := e.nextBlock(false); err != nil { |
| 456 | return 0, err |
| 457 | } |
| 458 | } |
| 459 | e.state.filling = e.state.filling[:e.o.blockSize] |
| 460 | src := e.state.filling |
| 461 | for { |
| 462 | n2, err := r.Read(src) |
| 463 | if e.o.crc { |
| 464 | _, _ = e.state.encoder.CRC().Write(src[:n2]) |
| 465 | } |
| 466 | src = src[n2:] |
| 467 | n += int64(n2) |
| 468 | switch err { |
| 469 | case io.EOF: |
| 470 | e.state.filling = e.state.filling[:len(e.state.filling)-len(src)] |
| 471 | if debugEncoder { |
| 472 | println("ReadFrom: got EOF final block:", len(e.state.filling)) |
| 473 | } |
| 474 | return n, nil |
| 475 | case nil: |
| 476 | default: |
| 477 | if debugEncoder { |
| 478 | println("ReadFrom: got error:", err) |
| 479 | } |
| 480 | e.state.err = err |
| 481 | return n, err |
| 482 | } |
| 483 | if len(src) > 0 { |
| 484 | if debugEncoder { |
| 485 | println("ReadFrom: got space left in source:", len(src)) |
| 486 | } |
| 487 | continue |
| 488 | } |
| 489 | err = e.nextBlock(false) |
| 490 | if err != nil { |
| 491 | return n, err |
| 492 | } |
| 493 | e.state.filling = e.state.filling[:e.o.blockSize] |
| 494 | src = e.state.filling |
| 495 | } |
| 496 | } |
| 497 | |
| 498 | func (e *Encoder) readFromJobs(r io.Reader) (n int64, err error) { |
| 499 | js := &e.state.jobs |