Read from the file - for details see io.Reader
(p []byte)
| 41 | |
| 42 | // Read from the file - for details see io.Reader |
| 43 | func (cr *sequential) Read(p []byte) (n int, err error) { |
| 44 | cr.mu.Lock() |
| 45 | defer cr.mu.Unlock() |
| 46 | |
| 47 | if cr.closed { |
| 48 | return 0, ErrorFileClosed |
| 49 | } |
| 50 | |
| 51 | for reqSize := int64(len(p)); reqSize > 0; reqSize = int64(len(p)) { |
| 52 | // the current chunk boundary. valid only when chunkSize > 0 |
| 53 | chunkEnd := cr.chunkOffset + cr.chunkSize |
| 54 | |
| 55 | fs.Debugf(cr.o, "ChunkedReader.Read at %d length %d chunkOffset %d chunkSize %d", cr.offset, reqSize, cr.chunkOffset, cr.chunkSize) |
| 56 | |
| 57 | switch { |
| 58 | case cr.chunkSize > 0 && cr.offset == chunkEnd: // last chunk read completely |
| 59 | cr.chunkOffset = cr.offset |
| 60 | if cr.customChunkSize { // last chunkSize was set by RangeSeek |
| 61 | cr.customChunkSize = false |
| 62 | cr.chunkSize = cr.initialChunkSize |
| 63 | } else { |
| 64 | cr.chunkSize *= 2 |
| 65 | if cr.chunkSize > cr.maxChunkSize && cr.maxChunkSize != -1 { |
| 66 | cr.chunkSize = cr.maxChunkSize |
| 67 | } |
| 68 | } |
| 69 | // recalculate the chunk boundary. valid only when chunkSize > 0 |
| 70 | chunkEnd = cr.chunkOffset + cr.chunkSize |
| 71 | fallthrough |
| 72 | case cr.offset == -1: // first Read or Read after RangeSeek |
| 73 | err = cr.openRange() |
| 74 | if err != nil { |
| 75 | return |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | var buf []byte |
| 80 | chunkRest := chunkEnd - cr.offset |
| 81 | // limit read to chunk boundaries if chunkSize > 0 |
| 82 | if reqSize > chunkRest && cr.chunkSize > 0 { |
| 83 | buf, p = p[0:chunkRest], p[chunkRest:] |
| 84 | } else { |
| 85 | buf, p = p, nil |
| 86 | } |
| 87 | var rn int |
| 88 | rn, err = io.ReadFull(cr.rc, buf) |
| 89 | n += rn |
| 90 | cr.offset += int64(rn) |
| 91 | if err != nil { |
| 92 | if err == io.ErrUnexpectedEOF { |
| 93 | err = io.EOF |
| 94 | } |
| 95 | return |
| 96 | } |
| 97 | } |
| 98 | return n, nil |
| 99 | } |
| 100 |