|
| 1 | +/* |
| 2 | + * |
| 3 | + * Copyright 2026 gRPC authors. |
| 4 | + * |
| 5 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | + * of this software and associated documentation files (the "Software"), to |
| 7 | + * deal in the Software without restriction, including without limitation the |
| 8 | + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 9 | + * sell copies of the Software, and to permit persons to whom the Software is |
| 10 | + * furnished to do so, subject to the following conditions: |
| 11 | + * |
| 12 | + * The above copyright notice and this permission notice shall be included in |
| 13 | + * all copies or substantial portions of the Software. |
| 14 | + * |
| 15 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 20 | + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 21 | + * IN THE SOFTWARE. |
| 22 | + * |
| 23 | + */ |
| 24 | + |
| 25 | +use std::io::Write; |
| 26 | +use std::io::{self}; |
| 27 | + |
| 28 | +use bytes::Buf; |
| 29 | +use bytes::BufMut; |
| 30 | +use flate2::Compression as FlateCompression; |
| 31 | +use flate2::write::ZlibDecoder; |
| 32 | +use flate2::write::ZlibEncoder; |
| 33 | + |
| 34 | +use crate::codec::compression::Compressor; |
| 35 | +use crate::codec::compression::Decompressor; |
| 36 | + |
| 37 | +/// A deflate compression implementation. |
| 38 | +#[derive(Debug, Clone, Copy)] |
| 39 | +pub struct Deflate { |
| 40 | + level: FlateCompression, |
| 41 | +} |
| 42 | + |
| 43 | +impl Deflate { |
| 44 | + /// Creates a new deflate compression implementation. |
| 45 | + pub fn new() -> Self { |
| 46 | + Self { |
| 47 | + level: FlateCompression::new(6), |
| 48 | + } |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +impl Default for Deflate { |
| 53 | + fn default() -> Self { |
| 54 | + Self::new() |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +impl Compressor for Deflate { |
| 59 | + fn name(&self) -> &'static str { |
| 60 | + "deflate" |
| 61 | + } |
| 62 | + |
| 63 | + fn compress(&self, source: &mut dyn Buf, destination: &mut dyn BufMut) -> Result<(), String> { |
| 64 | + let mut encoder = ZlibEncoder::new(destination.writer(), self.level); |
| 65 | + while source.has_remaining() { |
| 66 | + let chunk = source.chunk(); |
| 67 | + encoder.write_all(chunk).map_err(|e| e.to_string())?; |
| 68 | + source.advance(chunk.len()); |
| 69 | + } |
| 70 | + encoder.finish().map_err(|e| e.to_string())?; |
| 71 | + Ok(()) |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +impl Decompressor for Deflate { |
| 76 | + fn name(&self) -> &'static str { |
| 77 | + "deflate" |
| 78 | + } |
| 79 | + |
| 80 | + fn decompress(&self, source: &mut dyn Buf, destination: &mut dyn BufMut) -> Result<(), String> { |
| 81 | + let mut decoder = ZlibDecoder::new(destination.writer()); |
| 82 | + while source.has_remaining() { |
| 83 | + let chunk = source.chunk(); |
| 84 | + decoder.write_all(chunk).map_err(|e| e.to_string())?; |
| 85 | + source.advance(chunk.len()); |
| 86 | + } |
| 87 | + decoder.finish().map_err(|e| e.to_string())?; |
| 88 | + Ok(()) |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +#[cfg(test)] |
| 93 | +mod tests { |
| 94 | + use bytes::Bytes; |
| 95 | + |
| 96 | + use super::*; |
| 97 | + |
| 98 | + #[test] |
| 99 | + fn deflate_compress_decompress() { |
| 100 | + let compressor = Deflate::new(); |
| 101 | + let data = Bytes::from_static(b"hello world"); |
| 102 | + let mut compressed = Vec::new(); |
| 103 | + compressor |
| 104 | + .compress(&mut data.clone(), &mut compressed) |
| 105 | + .unwrap(); |
| 106 | + |
| 107 | + assert_ne!(compressed.as_slice(), data); |
| 108 | + let mut decompressed = Vec::new(); |
| 109 | + compressor |
| 110 | + .decompress(&mut compressed.as_slice(), &mut decompressed) |
| 111 | + .unwrap(); |
| 112 | + assert_eq!(data, decompressed.as_slice()); |
| 113 | + } |
| 114 | +} |
0 commit comments