Skip to content

Commit

Permalink
Update read_skippable_frame to take a Write parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
antoyo committed Feb 10, 2023
1 parent 54a3fe1 commit 3dd6aad
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 9 deletions.
10 changes: 3 additions & 7 deletions src/stream/read/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#[cfg(feature = "experimental")]
use std::cmp::min;
#[cfg(feature = "experimental")]
use std::io::{SeekFrom, Seek};
use std::io::{SeekFrom, Seek, Write};
use std::io::{self, BufRead, BufReader, Read};
#[cfg(feature = "experimental")]
use std::mem::size_of;
Expand Down Expand Up @@ -106,7 +106,7 @@ impl<'a, R: Read + Seek> Decoder<'a, BufReader<R>> {

/// Attempt to read a skippable frame and write its content to `dest`.
/// If it cannot read a skippable frame, the reader will be back to its starting position.
pub fn read_skippable_frame(&mut self, dest: &mut [u8]) -> io::Result<(usize, MagicVariant)> {
pub fn read_skippable_frame<W: Write>(&mut self, dest: &mut W) -> io::Result<(usize, MagicVariant)> {
let magic_buffer = self.reader.peek_4bytes()?;
let op = self.reader.operation();
if !op.is_skippable_frame(&magic_buffer) {
Expand All @@ -121,12 +121,8 @@ impl<'a, R: Read + Seek> Decoder<'a, BufReader<R>> {
self.reader.reader_mut().read_exact(&mut buffer)?;
let content_size = u32::from_le_bytes(buffer) as usize;

if content_size > dest.len() {
return Err(io::Error::new(io::ErrorKind::Other, "Destination buffer is too small"));
}

if content_size > 0 {
self.reader.reader_mut().read_exact(&mut dest[..content_size])?;
std::io::copy(&mut self.reader.reader_mut().take(content_size as u64), dest)?;
}

let magic_variant = magic_number - MAGIC_SKIPPABLE_START;
Expand Down
7 changes: 5 additions & 2 deletions src/stream/zio/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ mod tests {
// Decompress and skip a frame.
let mut decoder = Decoder::new(data).unwrap().single_frame();

let mut frame = vec![0; 20];
let mut frame = vec![];
let (size, _) = decoder.read_skippable_frame(&mut frame).unwrap();
assert_eq!("test content", String::from_utf8_lossy(&frame[..size]));

Expand All @@ -370,6 +370,7 @@ mod tests {
.single_frame()
};

let mut frame = vec![];
let (size, _) = decoder.read_skippable_frame(&mut frame).unwrap();
assert_eq!("SKIP", String::from_utf8_lossy(&frame[..size]));

Expand All @@ -384,6 +385,7 @@ mod tests {
.single_frame()
};

let mut frame = vec![];
let (size, _) = decoder.read_skippable_frame(&mut frame).unwrap();
assert_eq!("end", String::from_utf8_lossy(&frame[..size]));

Expand Down Expand Up @@ -432,10 +434,11 @@ mod tests {
.single_frame()
};

let mut frame = vec![0; 20];
let mut frame = vec![];
let (size, _) = decoder.read_skippable_frame(&mut frame).unwrap();
assert_eq!("SKIP", String::from_utf8_lossy(&frame[..size]));

let mut frame = vec![];
let (size, _) = decoder.read_skippable_frame(&mut frame).unwrap();
assert_eq!("second skip frame", String::from_utf8_lossy(&frame[..size]));
}
Expand Down

0 comments on commit 3dd6aad

Please sign in to comment.