Rename to Decoder.

Implement BufDecoder.
Added methods to get stream_len and stream_position.
This commit is contained in:
2020-08-21 11:58:33 +02:00
parent f8c94a3b61
commit b1aec7e18b

View File

@@ -1,35 +1,135 @@
use std::fs::File; pub mod read {
use std::io::Read; use std::fs::File;
use std::path::Path; use std::io::{Read, Seek, SeekFrom};
use std::path::Path;
use bzip2::read::BzDecoder; use bzip2::read::BzDecoder;
use flate2::read::GzDecoder; use flate2::read::GzDecoder;
pub enum FileDecompressed { pub enum Decoder {
Raw(File), Raw(File),
Gz(GzDecoder<File>), Gz(GzDecoder<File>),
Bz(BzDecoder<File>), Bz(BzDecoder<File>),
} }
impl Read for FileDecompressed { impl Decoder {
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> { pub fn open<P>(path: P) -> Result<Decoder, std::io::Error>
match self { where
FileDecompressed::Raw(file) => file.read(buf), P: AsRef<Path>,
FileDecompressed::Gz(file) => file.read(buf), {
FileDecompressed::Bz(file) => file.read(buf), let file = File::open(&path)?;
Ok(match mime_guess::from_path(path).first_raw() {
Some("application/x-gzip") => Decoder::Gz(GzDecoder::new(file)),
Some("application/x-bzip2") => Decoder::Bz(BzDecoder::new(file)),
_ => Decoder::Raw(file),
})
}
pub fn stream_len(&mut self) -> std::io::Result<u64> {
let file = match self {
Decoder::Raw(file) => file,
Decoder::Gz(file) => file.get_mut(),
Decoder::Bz(file) => file.get_mut(),
};
let old_pos = file.seek(SeekFrom::Current(0))?;
let len = file.seek(SeekFrom::End(0))?;
if old_pos != len {
file.seek(SeekFrom::Start(old_pos))?;
}
Ok(len)
}
pub fn stream_position(&mut self) -> std::io::Result<u64> {
let file = match self {
Decoder::Raw(file) => file,
Decoder::Gz(file) => file.get_mut(),
Decoder::Bz(file) => file.get_mut(),
};
file.seek(SeekFrom::Current(0))
}
}
impl Read for Decoder {
#[inline]
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
match self {
Decoder::Raw(file) => file.read(buf),
Decoder::Gz(file) => file.read(buf),
Decoder::Bz(file) => file.read(buf),
}
} }
} }
} }
pub fn open<P>(path: P) -> Result<FileDecompressed, std::io::Error> pub mod bufread {
where use std::fs::File;
P: AsRef<Path>, use std::io::{BufReader, Read, Seek, SeekFrom};
{ use std::path::Path;
let file = File::open(&path)?;
Ok(match mime_guess::from_path(path).first_raw() { use bzip2::bufread::BzDecoder;
Some("application/x-gzip") => FileDecompressed::Gz(GzDecoder::new(file)), use flate2::bufread::GzDecoder;
Some("application/x-bzip2") => FileDecompressed::Bz(BzDecoder::new(file)),
_ => FileDecompressed::Raw(file), pub enum BufDecoder {
}) Raw(BufReader<File>),
Gz(GzDecoder<BufReader<File>>),
Bz(BzDecoder<BufReader<File>>),
}
impl BufDecoder {
pub fn open<P>(path: P) -> Result<BufDecoder, std::io::Error>
where
P: AsRef<Path>,
{
let file = File::open(&path).map(BufReader::new)?;
Ok(match mime_guess::from_path(path).first_raw() {
Some("application/x-gzip") => BufDecoder::Gz(GzDecoder::new(file)),
Some("application/x-bzip2") => BufDecoder::Bz(BzDecoder::new(file)),
_ => BufDecoder::Raw(file),
})
}
pub fn stream_len(&mut self) -> std::io::Result<u64> {
let file = match self {
BufDecoder::Raw(file) => file,
BufDecoder::Gz(file) => file.get_mut(),
BufDecoder::Bz(file) => file.get_mut(),
};
let old_pos = file.seek(SeekFrom::Current(0))?;
let len = file.seek(SeekFrom::End(0))?;
if old_pos != len {
file.seek(SeekFrom::Start(old_pos))?;
}
Ok(len)
}
pub fn stream_position(&mut self) -> std::io::Result<u64> {
let file = match self {
BufDecoder::Raw(file) => file,
BufDecoder::Gz(file) => file.get_mut(),
BufDecoder::Bz(file) => file.get_mut(),
};
file.seek(SeekFrom::Current(0))
}
}
impl Read for BufDecoder {
#[inline]
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
match self {
BufDecoder::Raw(file) => file.read(buf),
BufDecoder::Gz(file) => file.read(buf),
BufDecoder::Bz(file) => file.read(buf),
}
}
}
} }