Skip to main content

fhp_encoding/
stream.rs

1//! Streaming decoder for chunk-based processing.
2//!
3//! [`DecodingReader`] wraps a [`Read`](std::io::Read) source and decodes
4//! bytes on-the-fly into UTF-8. This is useful for large inputs where
5//! loading the entire document into memory is undesirable.
6
7use std::io::{self, Read};
8
9use encoding_rs::{Decoder, Encoding};
10
11/// A streaming decoder that wraps a byte source and produces UTF-8 output.
12///
13/// Reads chunks from the inner reader, decodes them using the configured
14/// encoding, and writes UTF-8 bytes into the caller's buffer.
15///
16/// # Example
17///
18/// ```
19/// use fhp_encoding::DecodingReader;
20/// use std::io::Read;
21///
22/// let data = b"Hello, world!";
23/// let mut reader = DecodingReader::new(&data[..], encoding_rs::UTF_8);
24/// let mut output = String::new();
25/// reader.read_to_string(&mut output).unwrap();
26/// assert_eq!(output, "Hello, world!");
27/// ```
28pub struct DecodingReader<R> {
29    inner: R,
30    decoder: Decoder,
31    /// Raw bytes read from `inner` but not yet consumed by the decoder.
32    raw_buf: Vec<u8>,
33    /// Number of valid bytes in `raw_buf`.
34    raw_len: usize,
35    /// Decoded UTF-8 bytes ready to be returned to the caller.
36    decoded_buf: Vec<u8>,
37    /// Read cursor into `decoded_buf`.
38    decoded_pos: usize,
39    /// Number of valid decoded bytes in `decoded_buf`.
40    decoded_len: usize,
41    /// Whether the inner reader has reached EOF.
42    eof: bool,
43}
44
45/// Default chunk size for reading from the inner source (8 KB).
46const CHUNK_SIZE: usize = 8192;
47
48impl<R: Read> DecodingReader<R> {
49    /// Create a new streaming decoder with the given encoding.
50    pub fn new(inner: R, encoding: &'static Encoding) -> Self {
51        Self {
52            inner,
53            decoder: encoding.new_decoder(),
54            raw_buf: vec![0u8; CHUNK_SIZE],
55            raw_len: 0,
56            decoded_buf: vec![0u8; CHUNK_SIZE * 4], // worst case: 4 bytes per input byte
57            decoded_pos: 0,
58            decoded_len: 0,
59            eof: false,
60        }
61    }
62
63    /// Fill the decoded buffer by reading from the inner source and decoding.
64    fn fill_decoded(&mut self) -> io::Result<()> {
65        // If there's still data in the decoded buffer, don't refill.
66        if self.decoded_pos < self.decoded_len {
67            return Ok(());
68        }
69
70        // Reset decoded buffer.
71        self.decoded_pos = 0;
72        self.decoded_len = 0;
73
74        // Decode buffered bytes, reading more whenever a pass produces no
75        // output. A single pass can yield zero bytes when the only buffered
76        // bytes are an incomplete multi-byte sequence straddling a read
77        // boundary (the decoder buffers them internally and emits nothing); in
78        // that case we must read more from `inner` and decode again rather than
79        // reporting a premature EOF.
80        loop {
81            let (_result, read, written, _had_errors) = self.decoder.decode_to_utf8(
82                &self.raw_buf[..self.raw_len],
83                &mut self.decoded_buf,
84                self.eof,
85            );
86
87            // Shift any unconsumed trailing bytes (e.g. an output-full
88            // remainder) to the front of the raw buffer.
89            if read < self.raw_len {
90                self.raw_buf.copy_within(read..self.raw_len, 0);
91                self.raw_len -= read;
92            } else {
93                self.raw_len = 0;
94            }
95
96            if written > 0 {
97                self.decoded_len = written;
98                return Ok(());
99            }
100
101            // No output this pass. If the input is exhausted, we are done.
102            if self.eof {
103                return Ok(());
104            }
105
106            // Read more, appending after any leftover partial sequence so it
107            // can be completed by the following bytes.
108            if self.raw_len == self.raw_buf.len() {
109                // Buffer full without a complete sequence (pathological): grow
110                // it so the next read can make progress.
111                self.raw_buf.resize(self.raw_buf.len() * 2, 0);
112            }
113            let n = self.inner.read(&mut self.raw_buf[self.raw_len..])?;
114            if n == 0 {
115                self.eof = true;
116            } else {
117                self.raw_len += n;
118            }
119        }
120    }
121}
122
123impl<R: Read> Read for DecodingReader<R> {
124    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
125        if self.decoded_pos >= self.decoded_len {
126            self.fill_decoded()?;
127        }
128
129        if self.decoded_pos >= self.decoded_len {
130            return Ok(0); // EOF
131        }
132
133        let available = self.decoded_len - self.decoded_pos;
134        let to_copy = available.min(buf.len());
135        buf[..to_copy]
136            .copy_from_slice(&self.decoded_buf[self.decoded_pos..self.decoded_pos + to_copy]);
137        self.decoded_pos += to_copy;
138        Ok(to_copy)
139    }
140}
141
142#[cfg(test)]
143mod tests {
144    use super::*;
145
146    #[test]
147    fn stream_utf8() {
148        let data = b"Hello, world!";
149        let mut reader = DecodingReader::new(&data[..], encoding_rs::UTF_8);
150        let mut output = String::new();
151        reader.read_to_string(&mut output).unwrap();
152        assert_eq!(output, "Hello, world!");
153    }
154
155    #[test]
156    fn stream_windows_1254_turkish() {
157        // ş=0xFE, ğ=0xF0, ı=0xFD in Windows-1254
158        let data: &[u8] = &[0xFE, 0xF0, 0xFD];
159        let mut reader = DecodingReader::new(data, encoding_rs::WINDOWS_1254);
160        let mut output = String::new();
161        reader.read_to_string(&mut output).unwrap();
162        assert_eq!(output, "\u{015F}\u{011F}\u{0131}"); // ş, ğ, ı
163    }
164
165    #[test]
166    fn stream_empty() {
167        let data: &[u8] = b"";
168        let mut reader = DecodingReader::new(data, encoding_rs::UTF_8);
169        let mut output = String::new();
170        reader.read_to_string(&mut output).unwrap();
171        assert_eq!(output, "");
172    }
173
174    #[test]
175    fn stream_large_input() {
176        // Create input larger than CHUNK_SIZE to test multi-chunk decoding.
177        let data = "abcdefgh".repeat(2000); // 16KB
178        let mut reader = DecodingReader::new(data.as_bytes(), encoding_rs::UTF_8);
179        let mut output = String::new();
180        reader.read_to_string(&mut output).unwrap();
181        assert_eq!(output, data);
182    }
183
184    #[test]
185    fn stream_small_read_buf() {
186        let data = b"Hello, world!";
187        let mut reader = DecodingReader::new(&data[..], encoding_rs::UTF_8);
188        let mut output = Vec::new();
189        let mut buf = [0u8; 3]; // Read 3 bytes at a time.
190        loop {
191            let n = reader.read(&mut buf).unwrap();
192            if n == 0 {
193                break;
194            }
195            output.extend_from_slice(&buf[..n]);
196        }
197        assert_eq!(String::from_utf8(output).unwrap(), "Hello, world!");
198    }
199
200    /// Inner reader that yields a single byte per `read` call, forcing
201    /// multi-byte sequences to straddle read boundaries.
202    struct OneByteReader<'a> {
203        data: &'a [u8],
204        pos: usize,
205    }
206
207    impl Read for OneByteReader<'_> {
208        fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
209            if self.pos >= self.data.len() || buf.is_empty() {
210                return Ok(0);
211            }
212            buf[0] = self.data[self.pos];
213            self.pos += 1;
214            Ok(1)
215        }
216    }
217
218    #[test]
219    fn stream_multibyte_split_across_reads() {
220        // Multi-byte UTF-8 chars whose bytes arrive one at a time. A lead byte
221        // leaves a partial sequence buffered; the reader must read more rather
222        // than report a premature EOF.
223        let text = "cafe\u{301} gu\u{308}naydin nai\u{308}ve \u{1F600}";
224        let reader = OneByteReader {
225            data: text.as_bytes(),
226            pos: 0,
227        };
228        let mut out = String::new();
229        DecodingReader::new(reader, encoding_rs::UTF_8)
230            .read_to_string(&mut out)
231            .unwrap();
232        assert_eq!(out, text);
233    }
234
235    #[test]
236    fn stream_multibyte_across_chunk_boundary() {
237        // A multi-byte char positioned so its bytes straddle the 8 KB raw-read
238        // boundary must not be dropped.
239        let mut text = "a".repeat(CHUNK_SIZE - 1);
240        text.push('\u{20AC}'); // euro sign: 3 UTF-8 bytes, splits across 8192
241        text.push_str(&"b".repeat(100));
242        let mut out = String::new();
243        DecodingReader::new(text.as_bytes(), encoding_rs::UTF_8)
244            .read_to_string(&mut out)
245            .unwrap();
246        assert_eq!(out, text);
247    }
248}