diff --git a/src/services/item_service.rs b/src/services/item_service.rs index 1de2083..d5038d9 100644 --- a/src/services/item_service.rs +++ b/src/services/item_service.rs @@ -50,30 +50,22 @@ impl Read for FilteringReader { self.buffer.clear(); self.buffer_pos = 0; - // Read from the original reader + // Read from the original reader into a temporary buffer let mut temp_buf = vec![0; buf.len()]; let bytes_read = self.reader.read(&mut temp_buf)?; if bytes_read == 0 { - // If we're at EOF, process any remaining data in the filter chain - if let Some(chain) = &mut self.filter_chain { - let finished_data = chain.finish()?; - if !finished_data.is_empty() { - self.buffer = finished_data; - let bytes_to_copy = std::cmp::min(buf.len(), self.buffer.len()); - buf[..bytes_to_copy].copy_from_slice(&self.buffer[..bytes_to_copy]); - self.buffer_pos = bytes_to_copy; - return Ok(bytes_to_copy); - } - } return Ok(0); } // Process through the filter chain if it exists if let Some(chain) = &mut self.filter_chain { - let processed_data = chain.process(&temp_buf[..bytes_read])?; - if !processed_data.is_empty() { - self.buffer = processed_data; + // Use a cursor to read the input data + let mut input_cursor = std::io::Cursor::new(&temp_buf[..bytes_read]); + // Write filtered output to our buffer + chain.filter(&mut input_cursor, &mut self.buffer)?; + + if !self.buffer.is_empty() { let bytes_to_copy = std::cmp::min(buf.len(), self.buffer.len()); buf[..bytes_to_copy].copy_from_slice(&self.buffer[..bytes_to_copy]); self.buffer_pos = bytes_to_copy;