pub fn encode<R, W>(&self, input: R, output: W) -> io::Result<()>
    where
        R: Read,
        W: Write,
    {
        let mut input = BufReader::new(input);
        let mut output = BufWriter::new(output);

        self.encode_header(&mut output)?;

        // shift in the current byte being built
        let mut bit_shift = 0;
        // current byte being built, it is 128-bit long to
        // allow concatenation of a 64-bit long representation
        // without overflow
        let mut byte_buffer: u128 = 0;

        loop {
            // read bytes from the input
            let buf = input.fill_buf()?;
            let length = buf.len();
            if length == 0 {
                break;
            }

            for byte in buf {
                let SymbolInfo { repr, nbits, .. } = self.hbook[byte];
                byte_buffer += repr << bit_shift;
                bit_shift += nbits;
                while bit_shift >= 8 {
                    output.write_all(&[(byte_buffer % 256) as u8])?;
                    byte_buffer >>= 8;
                    bit_shift -= 8;
                }
            }

            input.consume(length);
        }
        // dump extraneous bits in the output
        output.write_all(&[byte_buffer as u8])?;
        output.flush()?;

        Ok(())
    }
/// Encode a stream using the canonical Huffman code.
///
/// This assumes input is the same as the one used to build the handbook in
/// the first place!  The lib crate should only expose an API that takes a
/// filename to do on disk, reading twice, OR takes a stream and keep it in
/// memory.
Encode a stream using the canonical Huffman code.
This assumes input is the same as the one used to build the handbook in
the first place.  The lib crate should only expose an API that takes a
filename to do on disk, reading twice, OR takes a stream and keep it in
memory.
Encode a stream using the canonical Huffman code . This assumes input is the same as the one used to build the handbook in the first place . The lib crate should only expose an API that takes a filename to do on disk reading twice OR takes a stream and keep it in memory .
pub fn encode<R, W>(&self, input: R, output: W) -> io::Result<()>
    where
        R: Read,
        W: Write,
    {
        let mut input = BufReader::new(input);
        let mut output = BufWriter::new(output);
        self.encode_header(&mut output)?;
        // shift in the current byte being built
        let mut bit_shift = 0;
        // current byte being built, it is 128-bit long to
        // allow concatenation of a 64-bit long representation
        // without overflow
        let mut byte_buffer: u128 = 0;
        loop {
            // read bytes from the input
            let buf = input.fill_buf()?;
            let length = buf.len();
            if length == 0 {
                break;
            }
            for byte in buf {
                let SymbolInfo { repr, nbits, .. } = self.hbook[byte];
                byte_buffer += repr << bit_shift;
                bit_shift += nbits;
                while bit_shift >= 8 {
                    output.write_all(&[(byte_buffer % 256) as u8])?;
                    byte_buffer >>= 8;
                    bit_shift -= 8;
                }
            }
            input.consume(length);
        }
        // dump extraneous bits in the output
        output.write_all(&[byte_buffer as u8])?;
        output.flush()?;
        Ok(())
    }
pub fn encode < R , W > ( & self , input : R , output : W ) -> io :: Result < ( ) > where R : Read , W : Write , { let mut input = BufReader :: new ( input ) ; let mut output = BufWriter :: new ( output ) ; self . encode_header ( & mut output ) ? ; // shift in the current byte being built let mut bit_shift = 0 ; // current byte being built, it is 128-bit long to // allow concatenation of a 64-bit long representation // without overflow let mut byte_buffer : u128 = 0 ; loop { // read bytes from the input let buf = input . fill_buf ( ) ? ; let length = buf . len ( ) ; if length == 0 { break ; } for byte in buf { let SymbolInfo { repr , nbits , .. } = self . hbook [ byte ] ; byte_buffer += repr << bit_shift ; bit_shift += nbits ; while bit_shift >= 8 { output . write_all ( & [ ( byte_buffer % 256 ) as u8 ] ) ? ; byte_buffer >>= 8 ; bit_shift -= 8 ; } } input . consume ( length ) ; } // dump extraneous bits in the output output . write_all ( & [ byte_buffer as u8 ] ) ? ; output . flush ( ) ? ; Ok ( ( ) ) }
Encode a stream using the canonical Huffman code.
Encode a stream using the canonical Huffman code .
&self
input
R
output
W
&self
input
R
output
W