1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
use std::io::{BufWriter, BufRead, Write};
use std::io;

pub struct ByteStreamSplitter<'a> {
    buffer: Vec<u8>,
    match_pointer: usize,
    peek_buffer: Vec<u8>,
    separator: &'a [u8],
    input: &'a mut BufRead,
    started_splitting: bool,
    end_of_stream_reached: bool,
}


pub type SplitResult<T> = Result<T,SplitError>;

#[derive(Debug)]
pub enum SplitType {
    Prefix,
    FullMatch,
    Suffix
}

#[derive(Debug)]
pub enum SplitError {
    Io(io::Error),
    Internal(String)
}

impl std::fmt::Display for SplitError {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        match *self {
            SplitError::Io(ref e) => e.fmt(f),
            SplitError::Internal(ref s) => write!(f,"{}", s)
        }
    }
}

impl std::error::Error for SplitError {
    fn description(&self) -> &str{
        match *self {
            SplitError::Io(ref e) => e.description(),
            SplitError::Internal(ref s) => s
        }
    }

    fn cause(&self) -> Option<&std::error::Error> {
        match *self {
            SplitError::Io(ref e) => Some(e),
            SplitError::Internal(_) => None
        }
    }
}

impl From<io::Error> for SplitError {
    fn from(e: io::Error) -> Self {
        SplitError::Io(e)
    }
}



impl<'a> ByteStreamSplitter<'a> {
    pub fn new(input: &'a mut BufRead, separator: &'a [u8]) -> ByteStreamSplitter<'a>{
        ByteStreamSplitter{
            input: input,
            match_pointer: 0,
            separator: separator,
            buffer: Vec::new(),
            peek_buffer:Vec::new(),
            started_splitting: false,
            end_of_stream_reached: false,
        }
    }

    pub fn read_until_next_matching_byte(&mut self) -> io::Result<usize> {
        self.input.read_until(self.separator[self.match_pointer], &mut self.buffer)
    }

    pub fn next_to_buf(&mut self, output: &mut Write) -> SplitResult<SplitType>{

        if self.end_of_stream_reached {
            return Err(SplitError::Io(io::Error::new(io::ErrorKind::InvalidInput, "Stream has no more data.")));
        }

        let mut part_result: Option<SplitType> = Option::None;
        while part_result.is_none() {
            let num_bytes = try!(self.read_until_next_matching_byte());

            part_result = match num_bytes {
                0 => {
                    self.end_of_stream_reached = true;
                    try!(output.write_all(&self.peek_buffer));
                    self.peek_buffer.clear();
                    Some(SplitType::Suffix)
                },
                1 => {
                    self.peek_buffer.push(self.buffer[0]);

                    if self.match_pointer < self.separator.len()-1 {
                        self.match_pointer +=1;
                        None
                    }else {
                        self.match_pointer = 0;
                        if self.started_splitting {
                            Some(SplitType::FullMatch)
                        } else {
                            self.started_splitting = true;
                            Some(SplitType::Prefix)
                        }
                    }
                },
                _ => {
                    try!(output.write_all(&self.peek_buffer));
                    self.peek_buffer.clear();

                    match self.match_pointer {
                        0 => {
                            self.match_pointer+=1;
                            self.peek_buffer.push(self.buffer[self.buffer.len()-1]);
                            try!(output.write_all(&self.buffer[..self.buffer.len()-1]));
                        },
                        _ => {
                            self.match_pointer=0;
                            try!(output.write_all(&self.buffer));
                        }
                    }
                    None
                }
            };
            self.buffer.clear();
        }
        part_result.ok_or(SplitError::Internal("Scan finished without succeeding or failing. This should never happen!".to_string()))
    }
}

impl <'a> Iterator for ByteStreamSplitter<'a> {
    type Item = SplitResult<Vec<u8>>;

    fn next(&mut self) -> Option<SplitResult<Vec<u8>>> {
        if self.end_of_stream_reached {
            None
        } else {
            let mut part = BufWriter::new(Vec::new());
            let result = self.next_to_buf(&mut part)
            .and(part.flush().map_err(SplitError::Io))
            .and(part.into_inner().map_err(|e|SplitError::Internal(e.to_string())));
            match result {
                Ok(inner) => Some(Ok(inner)),
                _   => None
            }
        }
   }
}


#[test]
fn test_with_prefix() {
    let separator = [0x00, 0x00];
    let mut data = io::Cursor::new(vec![
        0xAA, 0xAB,                     // Prefix
        0x00, 0x00, 0x01, 0x02, 0x03,   // FullMatch
        0x00, 0x00, 0x04, 0x05, 0x06,   // FullMatch
        0x00, 0x00, 0x07, 0x08          // Suffix
        ]);

    let mut splitter = ByteStreamSplitter::new(&mut data, &separator);
    let prefix = splitter.next().unwrap().unwrap();
    let match1 = splitter.next().unwrap().unwrap();
    let match2 = splitter.next().unwrap().unwrap();
    let suffix = splitter.next().unwrap().unwrap();

    assert_eq!(prefix, vec![0xAA, 0xAB]);
    assert_eq!(match1, vec![0x00, 0x00, 0x01, 0x02, 0x03]);
    assert_eq!(match2, vec![0x00, 0x00, 0x04, 0x05, 0x06]);
    assert_eq!(suffix, vec![0x00, 0x00, 0x07, 0x08]);
}

#[test]
fn test_without_prefix() {
    let separator = [0x00, 0x00];
    let mut data = io::Cursor::new(vec![
        0x00, 0x00, 0x01, 0x02, 0x03,   // FullMatch
        0x00, 0x00, 0x04, 0x05, 0x06,   // FullMatch
        0x00, 0x00, 0x07, 0x08          // Suffix
        ]);

    let mut splitter = ByteStreamSplitter::new(&mut data, &separator);
    let prefix = splitter.next().unwrap().unwrap();
    let match1 = splitter.next().unwrap().unwrap();
    let match2 = splitter.next().unwrap().unwrap();
    let suffix = splitter.next().unwrap().unwrap();

    assert_eq!(prefix, vec![]);
    assert_eq!(match1, vec![0x00, 0x00, 0x01, 0x02, 0x03]);
    assert_eq!(match2, vec![0x00, 0x00, 0x04, 0x05, 0x06]);
    assert_eq!(suffix, vec![0x00, 0x00, 0x07, 0x08]);
}