forked from rust-lang/flate2-rs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a
bufread
module for buffered types
Right now all stream types in `read` have an unconditional buffer between the underlying stream and what we're passing down to the compression layer. This buffer, however, is redundant for types that implement the `BufRead` trait. This commit adds explicit support for `BufRead` types which have their own local buffer, and then all other types are built on top of these types. A local implementation of `BufReader` is vendored for the `reset()` method.
- Loading branch information
1 parent
bb5d7c9
commit b91748a
Showing
5 changed files
with
391 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT | ||
// file at the top-level directory of this distribution and at | ||
// http://rust-lang.org/COPYRIGHT. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
use std::cmp; | ||
use std::io; | ||
use std::io::prelude::*; | ||
use std::mem; | ||
|
||
pub struct BufReader<R> { | ||
inner: R, | ||
buf: Box<[u8]>, | ||
pos: usize, | ||
cap: usize, | ||
} | ||
|
||
impl<R: Read> BufReader<R> { | ||
pub fn new(inner: R) -> BufReader<R> { | ||
BufReader::with_buf(vec![0; 32 * 1024], inner) | ||
} | ||
|
||
pub fn with_buf(buf: Vec<u8>, inner: R) -> BufReader<R> { | ||
BufReader { | ||
inner: inner, | ||
buf: buf.into_boxed_slice(), | ||
pos: 0, | ||
cap: 0, | ||
} | ||
} | ||
|
||
pub fn get_ref(&self) -> &R { | ||
&self.inner | ||
} | ||
|
||
pub fn into_inner(self) -> R { | ||
self.inner | ||
} | ||
|
||
pub fn reset(&mut self, inner: R) -> R { | ||
self.pos = 0; | ||
self.cap = 0; | ||
mem::replace(&mut self.inner, inner) | ||
} | ||
} | ||
|
||
impl<R: Read> Read for BufReader<R> { | ||
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() { | ||
return self.inner.read(buf); | ||
} | ||
let nread = { | ||
let mut rem = try!(self.fill_buf()); | ||
try!(rem.read(buf)) | ||
}; | ||
self.consume(nread); | ||
Ok(nread) | ||
} | ||
} | ||
|
||
impl<R: Read> BufRead for BufReader<R> { | ||
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. | ||
if self.pos == self.cap { | ||
self.cap = try!(self.inner.read(&mut self.buf)); | ||
self.pos = 0; | ||
} | ||
Ok(&self.buf[self.pos..self.cap]) | ||
} | ||
|
||
fn consume(&mut self, amt: usize) { | ||
self.pos = cmp::min(self.pos + amt, self.cap); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
use std::io; | ||
use std::io::BufRead; | ||
|
||
use {Decompress, Compress, Status, Flush, DataError}; | ||
|
||
pub trait ReadData { | ||
fn total_in(&self) -> u64; | ||
fn total_out(&self) -> u64; | ||
fn run(&mut self, input: &[u8], output: &mut [u8], flush: Flush) | ||
-> Result<Status, DataError>; | ||
} | ||
|
||
impl ReadData for Compress { | ||
fn total_in(&self) -> u64 { self.total_in() } | ||
fn total_out(&self) -> u64 { self.total_out() } | ||
fn run(&mut self, input: &[u8], output: &mut [u8], flush: Flush) | ||
-> Result<Status, DataError> { | ||
Ok(self.compress(input, output, flush)) | ||
} | ||
} | ||
|
||
impl ReadData for Decompress { | ||
fn total_in(&self) -> u64 { self.total_in() } | ||
fn total_out(&self) -> u64 { self.total_out() } | ||
fn run(&mut self, input: &[u8], output: &mut [u8], flush: Flush) | ||
-> Result<Status, DataError> { | ||
self.decompress(input, output, flush) | ||
} | ||
} | ||
|
||
pub fn read<R, D>(obj: &mut R, data: &mut D, dst: &mut [u8]) -> io::Result<usize> | ||
where R: BufRead, D: ReadData | ||
{ | ||
loop { | ||
let (read, consumed, ret, eof); | ||
{ | ||
let input = try!(obj.fill_buf()); | ||
eof = input.is_empty(); | ||
let before_out = data.total_out(); | ||
let before_in = data.total_in(); | ||
let flush = if eof {Flush::Finish} else {Flush::None}; | ||
ret = data.run(input, dst, flush); | ||
read = (data.total_out() - before_out) as usize; | ||
consumed = (data.total_in() - before_in) as usize; | ||
} | ||
obj.consume(consumed); | ||
|
||
match ret { | ||
// If we haven't ready any data and we haven't hit EOF yet, | ||
// then we need to keep asking for more data because if we | ||
// return that 0 bytes of data have been read then it will | ||
// be interpreted as EOF. | ||
Ok(Status::Ok) | | ||
Ok(Status::BufError) if read == 0 && !eof && dst.len() > 0 => { | ||
continue | ||
} | ||
Ok(Status::Ok) | | ||
Ok(Status::BufError) | | ||
Ok(Status::StreamEnd) => return Ok(read), | ||
|
||
Err(..) => return Err(io::Error::new(io::ErrorKind::InvalidInput, | ||
"corrupt deflate stream")) | ||
} | ||
} | ||
} |
Oops, something went wrong.