use flate2::read::ZlibEncoder; use flate2::Compression; use std::io::{Read, Result}; #[derive(Clone, Debug)] pub struct ByteArrayCache { inner: Vec, position: usize, } impl ByteArrayCache { /// Creates a new byte array cache pub fn new() -> Self { Self { inner: Vec::new(), position: 0, } } /// Creates a new byte array cache with capacity pub fn with_capacity(capacity: usize) -> Self { Self { inner: Vec::with_capacity(capacity), position: 0, } } /// Write the data to the inner buffer by using compression pub fn write(&mut self, reader: R) -> Result<()> { let mut encoder = ZlibEncoder::new(reader, Compression::default()); let mut buffer = Vec::new(); encoder.read_to_end(&mut buffer)?; self.inner.append(&mut buffer); Ok(()) } } impl Read for ByteArrayCache { fn read(&mut self, buf: &mut [u8]) -> Result { let read = (&self.inner[self.position..]).read(buf)?; self.position += read; Ok(read) } }