| 1 | mod deflate_stream { |
| 2 | use std::{ |
| 3 | io, |
| 4 | io::{Read, Write}, |
| 5 | }; |
| 6 | |
| 7 | use bstr::ByteSlice; |
| 8 | |
| 9 | use crate::zlib::stream::deflate; |
| 10 | use crate::zlib::Decompress; |
| 11 | |
| 12 | /// Provide streaming decompression using the `std::io::Read` trait. |
| 13 | /// If `std::io::BufReader` is used, an allocation for the input buffer will be performed. |
| 14 | struct InflateReader<R> { |
| 15 | inner: R, |
| 16 | decompressor: Decompress, |
| 17 | } |
| 18 | |
| 19 | impl<R> InflateReader<R> |
| 20 | where |
| 21 | R: io::BufRead, |
| 22 | { |
| 23 | pub fn from_read(read: R) -> InflateReader<R> { |
| 24 | InflateReader { |
| 25 | decompressor: Decompress::new(), |
| 26 | inner: read, |
| 27 | } |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | impl<R> io::Read for InflateReader<R> |
| 32 | where |
| 33 | R: io::BufRead, |
| 34 | { |
| 35 | fn read(&mut self, into: &mut [u8]) -> io::Result<usize> { |
| 36 | crate::zlib::stream::inflate::read(&mut self.inner, &mut self.decompressor, into) |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | #[test] |
| 41 | fn small_file_decompress() -> Result<(), Box<dyn std::error::Error>> { |
| 42 | fn fixture_path(path: &str) -> std::path::PathBuf { |
| 43 | std::path::PathBuf::from("tests/fixtures").join(path) |
| 44 | } |
| 45 | let r = InflateReader::from_read(io::BufReader::new(std::fs::File::open(fixture_path( |
| 46 | "objects/37/d4e6c5c48ba0d245164c4e10d5f41140cab980", |
| 47 | ))?)); |
| 48 | #[allow(clippy::unbuffered_bytes)] |
| 49 | let mut bytes = r.bytes(); |
| 50 | let content = bytes.by_ref().take(16).collect::<Result<Vec<_>, _>>()?; |
| 51 | assert_eq!(content.as_slice().as_bstr(), b"blob 9\0hi there\n".as_bstr()); |
| 52 | assert!(bytes.next().is_none()); |
| 53 | Ok(()) |
| 54 | } |
| 55 | |
| 56 | #[test] |
| 57 | fn all_at_once() -> Result<(), Box<dyn std::error::Error>> { |
| 58 | let mut w = deflate::Write::new(Vec::new()); |
| 59 | assert_eq!(w.write(b"hello")?, 5); |
| 60 | w.flush()?; |
| 61 | |
| 62 | let out = w.inner; |
| 63 | assert!(out.len() == 12 || out.len() == 13); |
| 64 | |
| 65 | assert_deflate_buffer(out, b"hello") |
| 66 | } |
| 67 | |
| 68 | fn assert_deflate_buffer(out: Vec<u8>, expected: &[u8]) -> Result<(), Box<dyn std::error::Error>> { |
| 69 | let mut actual = Vec::new(); |
| 70 | InflateReader::from_read(out.as_slice()).read_to_end(&mut actual)?; |
| 71 | assert_eq!(actual, expected); |
| 72 | Ok(()) |
| 73 | } |
| 74 | |
| 75 | #[test] |
| 76 | fn big_file_small_writes() -> Result<(), Box<dyn std::error::Error>> { |
| 77 | let mut w = deflate::Write::new(Vec::new()); |
| 78 | let bytes = include_bytes!( |
| 79 | "../../../../../gix-odb/tests/fixtures/objects/pack/pack-11fdfa9e156ab73caae3b6da867192221f2089c2.pack" |
| 80 | ); |
| 81 | for chunk in bytes.chunks(2) { |
| 82 | assert_eq!(w.write(chunk)?, chunk.len()); |
| 83 | } |
| 84 | w.flush()?; |
| 85 | |
| 86 | assert_deflate_buffer(w.inner, bytes) |
| 87 | } |
| 88 | |
| 89 | #[test] |
| 90 | fn big_file_a_few_big_writes() -> Result<(), Box<dyn std::error::Error>> { |
| 91 | let mut w = deflate::Write::new(Vec::new()); |
| 92 | let bytes = include_bytes!( |
| 93 | "../../../../../gix-odb/tests/fixtures/objects/pack/pack-11fdfa9e156ab73caae3b6da867192221f2089c2.pack" |
| 94 | ); |
| 95 | for chunk in bytes.chunks(4096 * 9) { |
| 96 | assert_eq!(w.write(chunk)?, chunk.len()); |
| 97 | } |
| 98 | w.flush()?; |
| 99 | |
| 100 | assert_deflate_buffer(w.inner, bytes) |
| 101 | } |
| 102 | } |