compress/
compress.rs

1extern crate divans;
2#[cfg(feature="no-stdlib")]
3fn main() {
4    panic!("For no-stdlib examples please see the tests")
5}
6#[cfg(not(feature="no-stdlib"))]
7fn main() {
8    let example_opts = divans::DivansCompressorOptions::default();
9    use std::io;
10    let stdout = &mut io::stdout();
11    {
12        use std::io::{Read, Write};
13        let mut writer = divans::DivansBrotliHybridCompressorWriter::new(
14            stdout,
15            divans::DivansCompressorOptions{
16                brotli_literal_byte_score:example_opts.brotli_literal_byte_score,
17                force_literal_context_mode:example_opts.force_literal_context_mode,
18                literal_adaptation:example_opts.literal_adaptation, // should we override how fast the cdfs converge for literals?
19                window_size:example_opts.window_size, // log 2 of the window size
20                lgblock:example_opts.lgblock, // should we override how often metablocks are created in brotli
21                quality:example_opts.quality, // the quality of brotli commands
22                q9_5:example_opts.q9_5,
23                dynamic_context_mixing:example_opts.dynamic_context_mixing, // if we want to mix together the stride prediction and the context map
24                prior_depth:example_opts.prior_depth,
25                use_brotli:example_opts.use_brotli, // ignored
26                use_context_map:example_opts.use_context_map, // whether we should use the brotli context map in addition to the last 8 bits of each byte as a prior
27                force_stride_value: example_opts.force_stride_value, // if we should use brotli to decide on the stride
28                speed_detection_quality: example_opts.speed_detection_quality,
29                stride_detection_quality: example_opts.stride_detection_quality,
30                prior_bitmask_detection: example_opts.prior_bitmask_detection,
31                divans_ir_optimizer:example_opts.divans_ir_optimizer,
32            },
33            4096, // internal buffer size
34        );
35        let mut buf = [0u8; 4096];
36        loop {
37            match io::stdin().read(&mut buf[..]) {
38                Err(e) => {
39                    if let io::ErrorKind::Interrupted = e.kind() {
40                        continue;
41                    }
42                    panic!(e);
43                }
44                Ok(size) => {
45                    if size == 0 {
46                        match writer.flush() {
47                            Err(e) => {
48                                if let io::ErrorKind::Interrupted = e.kind() {
49                                    continue;
50                                }
51                                panic!(e)
52                            }
53                            Ok(_) => break,
54                        }
55                    }
56                    match writer.write_all(&buf[..size]) {
57                        Err(e) => panic!(e),
58                        Ok(_) => {},
59                    }
60                }
61            }
62        }
63    }
64}