Skip to content

Commit

Permalink
Avoid an intermediary buffer in LZMA decoder
Browse files Browse the repository at this point in the history
  • Loading branch information
a1phyr committed Jul 16, 2024
1 parent e9b1312 commit 2e67999
Showing 1 changed file with 5 additions and 9 deletions.
14 changes: 5 additions & 9 deletions src/read/lzma.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use lzma_rs::decompress::{Options, Stream, UnpackedSize};
use std::collections::VecDeque;
use std::io::{Read, Result, Write};

const COMPRESSED_BYTES_TO_BUFFER: usize = 4096;
use std::io::{BufRead, Read, Result, Write};

const OPTIONS: Options = Options {
unpacked_size: UnpackedSize::ReadFromHeader,
Expand All @@ -29,17 +27,15 @@ impl<R: Read> LzmaDecoder<R> {
}
}

impl<R: Read> Read for LzmaDecoder<R> {
impl<R: BufRead> Read for LzmaDecoder<R> {
fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
let mut bytes_read = self.stream.get_output_mut().unwrap().read(buf)?;
while bytes_read < buf.len() {
let mut next_compressed = [0u8; COMPRESSED_BYTES_TO_BUFFER];
let compressed_bytes_read = self.compressed_reader.read(&mut next_compressed)?;
if compressed_bytes_read == 0 {
let compressed_bytes = self.compressed_reader.fill_buf()?;
if compressed_bytes.is_empty() {
break;
}
self.stream
.write_all(&next_compressed[..compressed_bytes_read])?;
self.stream.write_all(compressed_bytes)?;
bytes_read += self
.stream
.get_output_mut()
Expand Down

0 comments on commit 2e67999

Please sign in to comment.