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 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
//! Buffering wrappers for I/O traits //! This is copied from https://doc.rust-lang.org/src/std/io/buffered.rs.html //! This `BufReader` has some modifications for reading CP437 and converting //! them to UTF-8. It is also modified to allow progress indicators like progress bars. use std::io::prelude::*; use std::cmp; use std::fmt; use std::io::{self, IoSliceMut, SeekFrom}; // Rust default is 8kb, default here at 20kb const DEFAULT_BUF_SIZE: usize = 20 * 1024; pub struct BufReader<R, T> { inner: R, buf: Box<[u8]>, dec_buf: Box<[u8]>, pos: usize, cap: usize, progress: Progress<T>, } pub struct Progress<T> { pub updater: T, } pub trait ProgressUpdater { fn update(&mut self, value: u64); } impl<R: Read, T> BufReader<R, T> { /// Creates a new `BufReader<R>` with a default buffer capacity. The default is currently 8 KB, /// but may change in the future. /// /// # Examples /// /// ```no_run /// use std::io::BufReader; /// use std::fs::File; /// /// fn main() -> std::io::Result<()> { /// let f = File::open("log.txt")?; /// let reader = BufReader::new(f); /// Ok(()) /// } /// ``` pub fn new(inner: R, progress: Progress<T>) -> BufReader<R, T> { BufReader::with_capacity(DEFAULT_BUF_SIZE, inner, progress) } pub fn with_capacity(capacity: usize, inner: R, progress: Progress<T>) -> BufReader<R, T> { let mut buffer = Vec::with_capacity(capacity); let mut decoded_buffer = Vec::with_capacity(capacity * 3); buffer.resize(capacity, 0x00); decoded_buffer.resize(capacity * 3, 0x00); // inner.initializer().initialize(&mut buffer); BufReader { inner, buf: buffer.into_boxed_slice(), dec_buf: decoded_buffer.into_boxed_slice(), pos: 0, cap: 0, progress, } } } impl<R, T> BufReader<R, T> { /// Gets a reference to the underlying reader. /// /// It is inadvisable to directly read from the underlying reader. /// /// # Examples /// /// ```no_run /// use std::io::BufReader; /// use std::fs::File; /// /// fn main() -> std::io::Result<()> { /// let f1 = File::open("log.txt")?; /// let reader = BufReader::new(f1); /// /// let f2 = reader.get_ref(); /// Ok(()) /// } /// ``` pub fn get_ref(&self) -> &R { &self.inner } /// Gets a mutable reference to the underlying reader. /// /// It is inadvisable to directly read from the underlying reader. /// /// # Examples /// /// ```no_run /// use std::io::BufReader; /// use std::fs::File; /// /// fn main() -> std::io::Result<()> { /// let f1 = File::open("log.txt")?; /// let mut reader = BufReader::new(f1); /// /// let f2 = reader.get_mut(); /// Ok(()) /// } /// ``` pub fn get_mut(&mut self) -> &mut R { &mut self.inner } /// Returns a reference to the internally buffered data. /// /// Unlike `fill_buf`, this will not attempt to fill the buffer if it is empty. /// /// # Examples /// /// ```no_run /// use std::io::{BufReader, BufRead}; /// use std::fs::File; /// /// fn main() -> std::io::Result<()> { /// let f = File::open("log.txt")?; /// let mut reader = BufReader::new(f); /// assert!(reader.buffer().is_empty()); /// /// if reader.fill_buf()?.len() > 0 { /// assert!(!reader.buffer().is_empty()); /// } /// Ok(()) /// } /// ``` pub fn buffer(&self) -> &[u8] { &self.buf[self.pos..self.cap] } /// Returns the number of bytes the internal buffer can hold at once. /// /// # Examples /// /// ```no_run /// #![feature(buffered_io_capacity)] /// use std::io::{BufReader, BufRead}; /// use std::fs::File; /// /// fn main() -> std::io::Result<()> { /// let f = File::open("log.txt")?; /// let mut reader = BufReader::new(f); /// /// let capacity = reader.capacity(); /// let buffer = reader.fill_buf()?; /// assert!(buffer.len() <= capacity); /// Ok(()) /// } /// ``` pub fn capacity(&self) -> usize { self.buf.len() } /// Unwraps this `BufReader<R>`, returning the underlying reader. /// /// Note that any leftover data in the internal buffer is lost. Therefore, /// a following read from the underlying reader may lead to data loss. /// /// # Examples /// /// ```no_run /// use std::io::BufReader; /// use std::fs::File; /// /// fn main() -> std::io::Result<()> { /// let f1 = File::open("log.txt")?; /// let reader = BufReader::new(f1); /// /// let f2 = reader.into_inner(); /// Ok(()) /// } /// ``` pub fn into_inner(self) -> R { self.inner } /// Invalidates all data in the internal buffer. #[inline] fn discard_buffer(&mut self) { self.pos = 0; self.cap = 0; } } // impl<R: Seek, T> BufReader<R, T> { // // Not Implemented // } impl<R: Read, T: ProgressUpdater> Read for BufReader<R, T> { fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> { // If we don't have any buffered data and we're doing a massive read // (larger than our internal buffer), bypass our internal buffer // entirely. if self.pos == self.cap && buf.len() >= self.buf.len() { self.discard_buffer(); let result = self.inner.read(buf); return result; } let nread = { let mut rem = self.fill_buf()?; rem.read(buf)? }; self.consume(nread); Ok(nread) } fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> { let total_len = bufs.iter().map(|b| b.len()).sum::<usize>(); if self.pos == self.cap && total_len >= self.buf.len() { self.discard_buffer(); let result = self.inner.read_vectored(bufs); return result; } let nread = { let mut rem = self.fill_buf()?; rem.read_vectored(bufs)? }; self.consume(nread); Ok(nread) } // // we can't skip unconditionally because of the large buffer case in read. // unsafe fn initializer(&self) -> Initializer { // self.inner.initializer() // } } impl<R: Read, T: ProgressUpdater> BufRead for BufReader<R, T> { fn fill_buf(&mut self) -> io::Result<&[u8]> { // If we've reached the end of our internal buffer then we need to fetch // some more data from the underlying reader. // Branch using `>=` instead of the more correct `==` // to tell the compiler that the pos..cap slice is always valid. if self.pos >= self.cap { debug_assert!(self.pos == self.cap); self.cap = self.inner.read(&mut self.buf)?; self.pos = 0; let mut dec_pos = 0; // Convert all bytes from CP437 to UTF-8 for i in 0..self.cap { let newchar = crate::convert_cp437_byte_to_utf8_bytes(&self.buf[i]); if newchar[0] != 0x00 { self.dec_buf[dec_pos] = newchar[0]; dec_pos += 1; } if newchar[1] != 0x00 { self.dec_buf[dec_pos] = newchar[1]; dec_pos += 1; } self.dec_buf[dec_pos] = newchar[2]; dec_pos += 1; } // Set new capacity as it can be larger then the value before self.cap = dec_pos; self.progress.updater.update(dec_pos as u64); } Ok(&self.dec_buf[self.pos..self.cap]) } fn consume(&mut self, amt: usize) { self.pos = cmp::min(self.pos + amt, self.cap); } } impl<R, T> fmt::Debug for BufReader<R, T> where R: fmt::Debug, { fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { fmt.debug_struct("BufReader (modified for CP437)") .field("reader", &self.inner) .field( "buffer", &format_args!("{}/{}", self.cap - self.pos, self.buf.len()), ) .finish() } } impl<R: Seek, T> Seek for BufReader<R, T> { /// Seek to an offset, in bytes, in the underlying reader. /// /// The position used for seeking with `SeekFrom::Current(_)` is the /// position the underlying reader would be at if the `BufReader<R>` had no /// internal buffer. /// /// Seeking always discards the internal buffer, even if the seek position /// would otherwise fall within it. This guarantees that calling /// `.into_inner()` immediately after a seek yields the underlying reader /// at the same position. /// /// To seek without discarding the internal buffer, use [`BufReader::seek_relative`]. /// /// See [`std::io::Seek`] for more details. /// /// Note: In the edge case where you're seeking with `SeekFrom::Current(n)` /// where `n` minus the internal buffer length overflows an `i64`, two /// seeks will be performed instead of one. If the second seek returns /// `Err`, the underlying reader will be left at the same position it would /// have if you called `seek` with `SeekFrom::Current(0)`. /// /// [`BufReader::seek_relative`]: struct.BufReader.html#method.seek_relative /// [`std::io::Seek`]: trait.Seek.html fn seek(&mut self, pos: SeekFrom) -> io::Result<u64> { let result: u64; if let SeekFrom::Current(n) = pos { let remainder = (self.cap - self.pos) as i64; // it should be safe to assume that remainder fits within an i64 as the alternative // means we managed to allocate 8 exbibytes and that's absurd. // But it's not out of the realm of possibility for some weird underlying reader to // support seeking by i64::min_value() so we need to handle underflow when subtracting // remainder. if let Some(offset) = n.checked_sub(remainder) { result = self.inner.seek(SeekFrom::Current(offset))?; } else { // seek backwards by our remainder, and then by the offset self.inner.seek(SeekFrom::Current(-remainder))?; self.discard_buffer(); result = self.inner.seek(SeekFrom::Current(n))?; } } else { // Seeking with Start/End doesn't care about our buffer length. result = self.inner.seek(pos)?; } self.discard_buffer(); Ok(result) } }