| 277 | } |
| 278 | |
| 279 | func (this *FileReader) ReadBodyRange(buf []byte, start int64, end int64, callback ReaderFunc) error { |
| 280 | var isOk = false |
| 281 | |
| 282 | defer func() { |
| 283 | if !isOk { |
| 284 | _ = this.discard() |
| 285 | } |
| 286 | }() |
| 287 | |
| 288 | var offset = start |
| 289 | if start < 0 { |
| 290 | offset = this.bodyOffset + this.bodySize + end |
| 291 | end = this.bodyOffset + this.bodySize - 1 |
| 292 | } else if end < 0 { |
| 293 | offset = this.bodyOffset + start |
| 294 | end = this.bodyOffset + this.bodySize - 1 |
| 295 | } else { |
| 296 | offset = this.bodyOffset + start |
| 297 | end = this.bodyOffset + end |
| 298 | } |
| 299 | if offset < 0 || end < 0 || offset > end { |
| 300 | isOk = true |
| 301 | return ErrInvalidRange |
| 302 | } |
| 303 | _, err := this.fp.Seek(offset, io.SeekStart) |
| 304 | if err != nil { |
| 305 | return err |
| 306 | } |
| 307 | |
| 308 | for { |
| 309 | var n int |
| 310 | n, err = this.fp.Read(buf) |
| 311 | if n > 0 { |
| 312 | var n2 = int(end-offset) + 1 |
| 313 | if n2 <= n { |
| 314 | _, e := callback(n2) |
| 315 | if e != nil { |
| 316 | isOk = true |
| 317 | return e |
| 318 | } |
| 319 | break |
| 320 | } else { |
| 321 | goNext, e := callback(n) |
| 322 | if e != nil { |
| 323 | isOk = true |
| 324 | return e |
| 325 | } |
| 326 | if !goNext { |
| 327 | break |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | offset += int64(n) |
| 332 | if offset > end { |
| 333 | break |
| 334 | } |
| 335 | } |
| 336 | if err != nil { |