Skip to content

Commit

Permalink
tidy code
Browse files Browse the repository at this point in the history
  • Loading branch information
bczhc committed Jun 13, 2023
1 parent 9de4568 commit a2d5181
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 15 deletions.
6 changes: 4 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
//! use std::io::Read;
//! use bzip3::read::{Bz3Decoder, Bz3Encoder};
//! let data = "hello, world".as_bytes();
//! let mut compressor = Bz3Encoder::new(data, 100 * 1024 /* 100 kiB */).unwrap();
//! let mut decompressor = Bz3Decoder::new(&mut compressor).unwrap();
//! let mut compressor = Bz3Encoder::new(data, 100 * 1024 /* 100 kiB */).unwrap();
//! let mut decompressor = Bz3Decoder::new(&mut compressor).unwrap();
//!
//! let mut contents = String::new();
//! decompressor.read_to_string(&mut contents).unwrap();
Expand Down Expand Up @@ -49,6 +49,8 @@ pub(crate) trait TryReadExact {
///
/// When reaching EOF, the return value will be less than the size of the given buffer,
/// or just zero.
///
/// This simulates C function `fread`.
fn try_read_exact(&mut self, buf: &mut [u8]) -> std::io::Result<usize>;
}

Expand Down
16 changes: 7 additions & 9 deletions src/read.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Read-based BZip3 compressor and decompressor.

use std::io::{Cursor, ErrorKind, Read, Write};
use std::io::{ErrorKind, Read, Write};
use std::{io, slice};

use byteorder::{ReadBytesExt, WriteBytesExt, LE};
Expand Down Expand Up @@ -43,19 +43,17 @@ where
let buffer_size = block_size + block_size / 50 + 32 + MAGIC_NUMBER.len() + 4;
let mut buffer = vec![0_u8; buffer_size];

let mut header = Cursor::new(Vec::new());
let mut header = Vec::new();
header.write_all(MAGIC_NUMBER).unwrap();
header.write_i32::<LE>(block_size as i32).unwrap();
for x in header.get_ref().iter().enumerate() {
buffer[x.0] = *x.1;
}
buffer[..header.len()].copy_from_slice(&header);

Ok(Self {
state,
reader,
buffer,
buffer_pos: 0,
buffer_len: header.get_ref().len(), /* default buffer holds the header */
buffer_len: header.len(), /* default buffer holds the header */
block_size,
eof: false,
})
Expand Down Expand Up @@ -83,9 +81,9 @@ where
}

// go back and fill new_size and read_size
let mut cursor = Cursor::new(buffer);
cursor.write_i32::<LE>(new_size)?;
cursor.write_i32::<LE>(read_size as i32)?;
use byteorder::ByteOrder;
LE::write_i32(buffer, new_size);
LE::write_i32(&mut buffer[4..], read_size as i32);

self.buffer_len = 4 + 4 + new_size as usize;
Ok(read_size)
Expand Down
7 changes: 3 additions & 4 deletions src/write.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
//! Write-based BZip3 compressor and decompressor.

use std::io::{Cursor, Read, Write};
use std::mem::size_of;
use std::{io, mem};

use byteorder::{ReadBytesExt, WriteBytesExt, LE};
Expand Down Expand Up @@ -35,7 +34,7 @@ where
let state = Bz3State::new(block_size)?;
let block_size = block_size as i32;

let mut header = Cursor::new([0_u8; MAGIC_NUMBER.len() + size_of::<i32>()]);
let mut header = Cursor::new([0_u8; MAGIC_NUMBER.len() + 4 /* i32 */]);
header.write_all(MAGIC_NUMBER).unwrap();
header.write_i32::<LE>(block_size).unwrap();
writer.write_all(header.get_ref())?;
Expand Down Expand Up @@ -116,7 +115,7 @@ where
}
}

const BLOCK_HEADER_SIZE: usize = 2 * size_of::<i32>();
const BLOCK_HEADER_SIZE: usize = 2 * 4 /* i32 */;

pub struct Bz3Decoder<W>
where
Expand Down Expand Up @@ -155,7 +154,7 @@ where
W: Write,
{
pub fn new(writer: W) -> Self {
let header_len = MAGIC_NUMBER.len() + size_of::<i32>();
let header_len = MAGIC_NUMBER.len() + 4 /* i32 */;
Self {
state: None, /* here can't get the block size */
writer,
Expand Down

0 comments on commit a2d5181

Please sign in to comment.