Skip to content

Commit

Permalink
simplify GzDecoder::write impl
Browse files Browse the repository at this point in the history
  • Loading branch information
fafhrd91 committed Jul 30, 2018
1 parent cf45702 commit 6cb7bad
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 30 deletions.
49 changes: 22 additions & 27 deletions src/gz/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ impl<W: Write> Drop for GzEncoder<W> {
/// fn decode_writer(bytes: Vec<u8>) -> io::Result<String> {
/// let mut writer = Vec::new();
/// let mut decoder = GzDecoder::new(writer);
/// decoder.write(&bytes[..])?;
/// decoder.write_all(&bytes[..])?;
/// writer = decoder.finish()?;
/// let return_string = String::from_utf8(writer).expect("String parsing error");
/// Ok(return_string)
Expand Down Expand Up @@ -320,20 +320,6 @@ impl<W: Write> GzDecoder<W> {
}
Ok(())
}

fn write_buf(&mut self, buf: &[u8]) -> io::Result<usize> {
let (n, status) = self.inner.write_with_status(buf)?;

if status == Status::StreamEnd {
if n < buf.len() && self.crc_bytes.len() < 8 {
let remaining = buf.len() - n;
let crc_bytes = cmp::min(remaining, CRC_BYTES_LEN - self.crc_bytes.len());
self.crc_bytes.extend(&buf[n..n + crc_bytes]);
return Ok(n + crc_bytes);
}
}
Ok(n)
}
}

struct Wrapper<'a> {
Expand Down Expand Up @@ -376,11 +362,7 @@ impl<W: Write> Write for GzDecoder<W> {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
if self.header.is_none() {
// trying to avoid buffer usage
let (res, pos) = if self.header_buf.is_empty() {
let mut cur = io::Cursor::new(buf);
let res = read_gz_header(&mut cur);
(res, cur.position() as usize)
} else {
let (res, pos) = {
let mut wrp = Wrapper {
buf: buf,
header_buf: &mut self.header_buf,
Expand All @@ -403,12 +385,21 @@ impl<W: Write> Write for GzDecoder<W> {
Ok(header) => {
self.header = Some(header);
self.header_buf.truncate(0);
let n = self.write_buf(&buf[pos..])?;
Ok(n + pos)
Ok(pos)
}
}
} else {
self.write_buf(buf)
let (n, status) = self.inner.write_with_status(buf)?;

if status == Status::StreamEnd {
if n < buf.len() && self.crc_bytes.len() < 8 {
let remaining = buf.len() - n;
let crc_bytes = cmp::min(remaining, CRC_BYTES_LEN - self.crc_bytes.len());
self.crc_bytes.extend(&buf[n..n + crc_bytes]);
return Ok(n + crc_bytes);
}
}
Ok(n)
}
}

Expand Down Expand Up @@ -452,7 +443,8 @@ mod tests {

let mut writer = Vec::new();
let mut decoder = GzDecoder::new(writer);
decoder.write(&bytes[..]).unwrap();
let n = decoder.write(&bytes[..]).unwrap();
decoder.write(&bytes[n..]).unwrap();
decoder.try_finish().unwrap();
writer = decoder.finish().unwrap();
let return_string = String::from_utf8(writer).expect("String parsing error");
Expand All @@ -468,7 +460,10 @@ mod tests {
let mut writer = Vec::new();
let mut decoder = GzDecoder::new(writer);
assert_eq!(decoder.write(&bytes[..5]).unwrap(), 5);
decoder.write(&bytes[5..]).unwrap();
let n = decoder.write(&bytes[5..]).unwrap();
if n < bytes.len() - 5 {
decoder.write(&bytes[n + 5..]).unwrap();
}
writer = decoder.finish().unwrap();
let return_string = String::from_utf8(writer).expect("String parsing error");
assert_eq!(return_string, STR);
Expand Down Expand Up @@ -498,8 +493,8 @@ mod tests {
let mut writer = Vec::new();
let mut decoder = GzDecoder::new(writer);
let l = bytes.len() - 5;
decoder.write(&bytes[..l]).unwrap();
decoder.write(&bytes[l..]).unwrap();
let n = decoder.write(&bytes[..l]).unwrap();
decoder.write(&bytes[n..]).unwrap();
writer = decoder.finish().unwrap();
let return_string = String::from_utf8(writer).expect("String parsing error");
assert_eq!(return_string, STR);
Expand Down
4 changes: 2 additions & 2 deletions src/mem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,14 +137,14 @@ pub enum FlushDecompress {
}

/// The inner state for an error when decompressing
#[derive(Debug, Default, PartialEq)]
#[derive(Debug, Default)]
struct DecompressErrorInner {
needs_dictionary: Option<u32>,
}

/// Error returned when a decompression object finds that the input stream of
/// bytes was not a valid input stream of bytes.
#[derive(Debug, PartialEq)]
#[derive(Debug)]
pub struct DecompressError(DecompressErrorInner);

impl DecompressError {
Expand Down
7 changes: 6 additions & 1 deletion src/zio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,12 @@ impl<W: Write, D: Ops> Writer<W, D> {
let ret = self.data.run_vec(buf, &mut self.buf, D::Flush::none());
let written = (self.data.total_in() - before_in) as usize;

if buf.len() > 0 && written == 0 && ret.is_ok() && ret != Ok(Status::StreamEnd) {
let is_stream_end = match ret {
Ok(Status::StreamEnd) => true,
_ => false,
};

if buf.len() > 0 && written == 0 && ret.is_ok() && !is_stream_end {
continue;
}
return match ret {
Expand Down

0 comments on commit 6cb7bad

Please sign in to comment.