|
13 | 13 | // XXX: Not sure how this should be structured
|
14 | 14 | // XXX: Iteration should probably be considered separately
|
15 | 15 |
|
| 16 | +use vec; |
| 17 | +use rt::io::Reader; |
| 18 | +use option::{Option, Some, None}; |
| 19 | +use unstable::finally::Finally; |
| 20 | + |
16 | 21 | pub trait ReaderUtil {
|
17 | 22 |
|
| 23 | + /// Reads a single byte. Returns `None` on EOF. |
| 24 | + /// |
| 25 | + /// # Failure |
| 26 | + /// |
| 27 | + /// Raises the same conditions as the `read` method. Returns |
| 28 | + /// `None` if the condition is handled. |
| 29 | + fn read_byte(&mut self) -> Option<u8>; |
| 30 | + |
| 31 | + /// Reads `len` bytes and appends them to a vector. |
| 32 | + /// |
| 33 | + /// May push fewer than the requested number of bytes on error |
| 34 | + /// or EOF. Returns true on success, false on EOF or error. |
| 35 | + /// |
| 36 | + /// # Failure |
| 37 | + /// |
| 38 | + /// Raises the same conditions as `read`. Returns `false` if |
| 39 | + /// the condition is handled. |
| 40 | + fn push_bytes(&mut self, buf: &mut ~[u8], len: uint) -> bool; |
| 41 | + |
18 | 42 | /// Reads `len` bytes and gives you back a new vector
|
19 | 43 | ///
|
20 | 44 | /// # Failure
|
21 | 45 | ///
|
22 |
| - /// Raises the `io_error` condition on error. Returns an empty |
23 |
| - /// vector if the condition is handled. |
| 46 | + /// Raises the same conditions as the `read` method. May return |
| 47 | + /// less than the requested number of bytes on error or EOF. |
24 | 48 | fn read_bytes(&mut self, len: uint) -> ~[u8];
|
25 | 49 |
|
26 | 50 | /// Reads all remaining bytes from the stream.
|
27 | 51 | ///
|
28 | 52 | /// # Failure
|
29 | 53 | ///
|
30 |
| - /// Raises the `io_error` condition on error. Returns an empty |
31 |
| - /// vector if the condition is handled. |
| 54 | + /// Raises the same conditions as the `read` method. |
32 | 55 | fn read_to_end(&mut self) -> ~[u8];
|
33 | 56 |
|
34 | 57 | }
|
35 | 58 |
|
| 59 | +impl<T: Reader> ReaderUtil for T { |
| 60 | + fn read_byte(&mut self) -> Option<u8> { |
| 61 | + let mut buf = [0]; |
| 62 | + match self.read(buf) { |
| 63 | + Some(nread) if nread == 0 => { |
| 64 | + debug!("read 0 bytes. trying again"); |
| 65 | + self.read_byte() |
| 66 | + } |
| 67 | + Some(nread) => Some(buf[0]), |
| 68 | + None => None |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + fn push_bytes(&mut self, buf: &mut ~[u8], len: uint) -> bool { |
| 73 | + unsafe { |
| 74 | + let start_len = buf.len(); |
| 75 | + let mut total_read = 0; |
| 76 | + let mut eof = false; |
| 77 | + |
| 78 | + vec::reserve_at_least(buf, start_len + len); |
| 79 | + vec::raw::set_len(buf, start_len + len); |
| 80 | + |
| 81 | + do (|| { |
| 82 | + while total_read < len { |
| 83 | + let slice = vec::mut_slice(*buf, start_len + total_read, buf.len()); |
| 84 | + match self.read(slice) { |
| 85 | + Some(nread) => { |
| 86 | + total_read += nread; |
| 87 | + } |
| 88 | + None => { |
| 89 | + eof = true; |
| 90 | + break; |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + }).finally { |
| 95 | + vec::raw::set_len(buf, start_len + total_read); |
| 96 | + } |
| 97 | + |
| 98 | + return !eof; |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + fn read_bytes(&mut self, len: uint) -> ~[u8] { |
| 103 | + let mut buf = vec::with_capacity(len); |
| 104 | + self.push_bytes(&mut buf, len); |
| 105 | + return buf; |
| 106 | + } |
| 107 | + |
| 108 | + fn read_to_end(&mut self) -> ~[u8] { |
| 109 | + fail!() |
| 110 | + } |
| 111 | +} |
| 112 | + |
36 | 113 | pub trait ReaderByteConversions {
|
37 | 114 | /// Reads `n` little-endian unsigned integer bytes.
|
38 | 115 | ///
|
@@ -467,3 +544,184 @@ pub trait WriterByteConversions {
|
467 | 544 | /// Raises the `io_error` condition on error.
|
468 | 545 | fn write_i8(&mut self, n: i8);
|
469 | 546 | }
|
| 547 | + |
| 548 | +#[cfg(test)] |
| 549 | +mod test { |
| 550 | + use super::*; |
| 551 | + use option::{Some, None}; |
| 552 | + use cell::Cell; |
| 553 | + use rt::io::mem::MemReader; |
| 554 | + use rt::io::mock::*; |
| 555 | + use rt::io::{io_error, placeholder_error}; |
| 556 | + |
| 557 | + #[test] |
| 558 | + fn read_byte() { |
| 559 | + let mut reader = MemReader::new(~[10]); |
| 560 | + let byte = reader.read_byte(); |
| 561 | + assert!(byte == Some(10)); |
| 562 | + } |
| 563 | + |
| 564 | + #[test] |
| 565 | + fn read_byte_0_bytes() { |
| 566 | + let mut reader = MockReader::new(); |
| 567 | + let count = Cell(0); |
| 568 | + reader.read = |buf| { |
| 569 | + do count.with_mut_ref |count| { |
| 570 | + if *count == 0 { |
| 571 | + *count = 1; |
| 572 | + Some(0) |
| 573 | + } else { |
| 574 | + buf[0] = 10; |
| 575 | + Some(1) |
| 576 | + } |
| 577 | + } |
| 578 | + }; |
| 579 | + let byte = reader.read_byte(); |
| 580 | + assert!(byte == Some(10)); |
| 581 | + } |
| 582 | + |
| 583 | + #[test] |
| 584 | + fn read_byte_eof() { |
| 585 | + let mut reader = MockReader::new(); |
| 586 | + reader.read = |_| None; |
| 587 | + let byte = reader.read_byte(); |
| 588 | + assert!(byte == None); |
| 589 | + } |
| 590 | + |
| 591 | + #[test] |
| 592 | + fn read_byte_error() { |
| 593 | + let mut reader = MockReader::new(); |
| 594 | + reader.read = |_| { |
| 595 | + io_error::cond.raise(placeholder_error()); |
| 596 | + None |
| 597 | + }; |
| 598 | + do io_error::cond.trap(|_| { |
| 599 | + }).in { |
| 600 | + let byte = reader.read_byte(); |
| 601 | + assert!(byte == None); |
| 602 | + } |
| 603 | + } |
| 604 | + |
| 605 | + #[test] |
| 606 | + fn read_bytes() { |
| 607 | + let mut reader = MemReader::new(~[10, 11, 12, 13]); |
| 608 | + let bytes = reader.read_bytes(4); |
| 609 | + assert!(bytes == ~[10, 11, 12, 13]); |
| 610 | + } |
| 611 | + |
| 612 | + #[test] |
| 613 | + fn read_bytes_partial() { |
| 614 | + let mut reader = MockReader::new(); |
| 615 | + let count = Cell(0); |
| 616 | + reader.read = |buf| { |
| 617 | + do count.with_mut_ref |count| { |
| 618 | + if *count == 0 { |
| 619 | + *count = 1; |
| 620 | + buf[0] = 10; |
| 621 | + buf[1] = 11; |
| 622 | + Some(2) |
| 623 | + } else { |
| 624 | + buf[0] = 12; |
| 625 | + buf[1] = 13; |
| 626 | + Some(2) |
| 627 | + } |
| 628 | + } |
| 629 | + }; |
| 630 | + let bytes = reader.read_bytes(4); |
| 631 | + assert!(bytes == ~[10, 11, 12, 13]); |
| 632 | + } |
| 633 | + |
| 634 | + #[test] |
| 635 | + fn push_bytes() { |
| 636 | + let mut reader = MemReader::new(~[10, 11, 12, 13]); |
| 637 | + let mut buf = ~[8, 9]; |
| 638 | + assert!(reader.push_bytes(&mut buf, 4)); |
| 639 | + assert!(buf == ~[8, 9, 10, 11, 12, 13]); |
| 640 | + } |
| 641 | + |
| 642 | + #[test] |
| 643 | + fn push_bytes_partial() { |
| 644 | + let mut reader = MockReader::new(); |
| 645 | + let count = Cell(0); |
| 646 | + reader.read = |buf| { |
| 647 | + do count.with_mut_ref |count| { |
| 648 | + if *count == 0 { |
| 649 | + *count = 1; |
| 650 | + buf[0] = 10; |
| 651 | + buf[1] = 11; |
| 652 | + Some(2) |
| 653 | + } else { |
| 654 | + buf[0] = 12; |
| 655 | + buf[1] = 13; |
| 656 | + Some(2) |
| 657 | + } |
| 658 | + } |
| 659 | + }; |
| 660 | + let mut buf = ~[8, 9]; |
| 661 | + assert!(reader.push_bytes(&mut buf, 4)); |
| 662 | + assert!(buf == ~[8, 9, 10, 11, 12, 13]); |
| 663 | + } |
| 664 | + |
| 665 | + #[test] |
| 666 | + fn push_bytes_eof() { |
| 667 | + let mut reader = MemReader::new(~[10, 11]); |
| 668 | + let mut buf = ~[8, 9]; |
| 669 | + assert!(!reader.push_bytes(&mut buf, 4)); |
| 670 | + assert!(buf == ~[8, 9, 10, 11]); |
| 671 | + } |
| 672 | + |
| 673 | + #[test] |
| 674 | + fn push_bytes_error() { |
| 675 | + let mut reader = MockReader::new(); |
| 676 | + let count = Cell(0); |
| 677 | + reader.read = |buf| { |
| 678 | + do count.with_mut_ref |count| { |
| 679 | + if *count == 0 { |
| 680 | + *count = 1; |
| 681 | + buf[0] = 10; |
| 682 | + Some(1) |
| 683 | + } else { |
| 684 | + io_error::cond.raise(placeholder_error()); |
| 685 | + None |
| 686 | + } |
| 687 | + } |
| 688 | + }; |
| 689 | + let mut buf = ~[8, 9]; |
| 690 | + do io_error::cond.trap(|_| { } ).in { |
| 691 | + assert!(!reader.push_bytes(&mut buf, 4)); |
| 692 | + } |
| 693 | + assert!(buf == ~[8, 9, 10]); |
| 694 | + } |
| 695 | + |
| 696 | + #[test] |
| 697 | + #[should_fail] |
| 698 | + #[ignore(cfg(windows))] |
| 699 | + fn push_bytes_fail_reset_len() { |
| 700 | + use unstable::finally::Finally; |
| 701 | + |
| 702 | + // push_bytes unsafely sets the vector length. This is testing that |
| 703 | + // upon failure the length is reset correctly. |
| 704 | + let mut reader = MockReader::new(); |
| 705 | + let count = Cell(0); |
| 706 | + reader.read = |buf| { |
| 707 | + do count.with_mut_ref |count| { |
| 708 | + if *count == 0 { |
| 709 | + *count = 1; |
| 710 | + buf[0] = 10; |
| 711 | + Some(1) |
| 712 | + } else { |
| 713 | + io_error::cond.raise(placeholder_error()); |
| 714 | + None |
| 715 | + } |
| 716 | + } |
| 717 | + }; |
| 718 | + let buf = @mut ~[8, 9]; |
| 719 | + do (|| { |
| 720 | + reader.push_bytes(&mut *buf, 4); |
| 721 | + }).finally { |
| 722 | + // NB: Using rtassert here to trigger abort on failure since this is a should_fail test |
| 723 | + rtassert!(*buf == ~[8, 9, 10]); |
| 724 | + } |
| 725 | + } |
| 726 | + |
| 727 | +} |
0 commit comments