zstd/stream/zio/writer.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
use std::io::{self, Write};
use crate::stream::raw::{InBuffer, Operation, OutBuffer};
// input -> [ zstd -> buffer -> writer ]
/// Implements the [`Write`] API around an [`Operation`].
///
/// This can be used to wrap a raw in-memory operation in a write-focused API.
///
/// It can be used with either compression or decompression, and forwards the
/// output to a wrapped `Write`.
pub struct Writer<W, D> {
/// Either an encoder or a decoder.
operation: D,
/// Where we send the output of the operation.
writer: W,
/// Offset into the buffer
///
/// Only things after this matter. Things before have already been sent to the writer.
offset: usize,
/// Output buffer
///
/// Where the operation writes, before it gets flushed to the writer
buffer: Vec<u8>,
// When `true`, indicates that nothing should be added to the buffer.
// All that's left if to empty the buffer.
finished: bool,
/// When `true`, the operation just finished a frame.
///
/// Only happens when decompressing.
/// The context needs to be re-initialized to process the next frame.
finished_frame: bool,
}
impl<W, D> Writer<W, D>
where
W: Write,
D: Operation,
{
/// Creates a new `Writer` with a fixed buffer capacity of 32KB
///
/// All output from the given operation will be forwarded to `writer`.
pub fn new(writer: W, operation: D) -> Self {
// 32KB buffer? That's what flate2 uses
Self::new_with_capacity(writer, operation, 32 * 1024)
}
/// Creates a new `Writer` with user defined capacity.
///
/// All output from the given operation will be forwarded to `writer`.
pub fn new_with_capacity(
writer: W,
operation: D,
capacity: usize,
) -> Self {
Self::with_output_buffer(
Vec::with_capacity(capacity),
writer,
operation,
)
}
/// Creates a new `Writer` using the given output buffer.
///
/// The output buffer _must_ have pre-allocated capacity (its capacity will not be changed after).
///
/// Usually you would use `Vec::with_capacity(desired_buffer_size)`.
pub fn with_output_buffer(
output_buffer: Vec<u8>,
writer: W,
operation: D,
) -> Self {
Writer {
writer,
operation,
offset: 0,
// 32KB buffer? That's what flate2 uses
buffer: output_buffer,
finished: false,
finished_frame: false,
}
}
/// Ends the stream.
///
/// This *must* be called after all data has been written to finish the
/// stream.
///
/// If you forget to call this and just drop the `Writer`, you *will* have
/// an incomplete output.
///
/// Keep calling it until it returns `Ok(())`, then don't call it again.
pub fn finish(&mut self) -> io::Result<()> {
loop {
// Keep trying until we're really done.
self.write_from_offset()?;
// At this point the buffer has been fully written out.
if self.finished {
return Ok(());
}
// Let's fill this buffer again!
let finished_frame = self.finished_frame;
let hint =
self.with_buffer(|dst, op| op.finish(dst, finished_frame));
self.offset = 0;
// println!("Hint: {:?}\nOut:{:?}", hint, &self.buffer);
// We return here if zstd had a problem.
// Could happen with invalid data, ...
let hint = hint?;
if hint != 0 && self.buffer.is_empty() {
// This happens if we are decoding an incomplete frame.
return Err(io::Error::new(
io::ErrorKind::UnexpectedEof,
"incomplete frame",
));
}
// println!("Finishing {}, {}", bytes_written, hint);
self.finished = hint == 0;
}
}
/// Run the given closure on `self.buffer`.
///
/// The buffer will be cleared, and made available wrapped in an `OutBuffer`.
fn with_buffer<F, T>(&mut self, f: F) -> T
where
F: FnOnce(&mut OutBuffer<'_, Vec<u8>>, &mut D) -> T,
{
self.buffer.clear();
let mut output = OutBuffer::around(&mut self.buffer);
// eprintln!("Output: {:?}", output);
f(&mut output, &mut self.operation)
}
/// Attempt to write `self.buffer` to the wrapped writer.
///
/// Returns `Ok(())` once all the buffer has been written.
fn write_from_offset(&mut self) -> io::Result<()> {
// The code looks a lot like `write_all`, but keeps track of what has
// been written in case we're interrupted.
while self.offset < self.buffer.len() {
match self.writer.write(&self.buffer[self.offset..]) {
Ok(0) => {
return Err(io::Error::new(
io::ErrorKind::WriteZero,
"writer will not accept any more data",
))
}
Ok(n) => self.offset += n,
Err(ref e) if e.kind() == io::ErrorKind::Interrupted => (),
Err(e) => return Err(e),
}
}
Ok(())
}
/// Return the wrapped `Writer` and `Operation`.
///
/// Careful: if you call this before calling [`Writer::finish()`], the
/// output may be incomplete.
pub fn into_inner(self) -> (W, D) {
(self.writer, self.operation)
}
/// Gives a reference to the inner writer.
pub fn writer(&self) -> &W {
&self.writer
}
/// Gives a mutable reference to the inner writer.
pub fn writer_mut(&mut self) -> &mut W {
&mut self.writer
}
/// Gives a reference to the inner operation.
pub fn operation(&self) -> &D {
&self.operation
}
/// Gives a mutable reference to the inner operation.
pub fn operation_mut(&mut self) -> &mut D {
&mut self.operation
}
/// Returns the offset in the current buffer. Only useful for debugging.
#[cfg(test)]
pub fn offset(&self) -> usize {
self.offset
}
/// Returns the current buffer. Only useful for debugging.
#[cfg(test)]
pub fn buffer(&self) -> &[u8] {
&self.buffer
}
}
impl<W, D> Write for Writer<W, D>
where
W: Write,
D: Operation,
{
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
if self.finished {
return Err(io::Error::new(
io::ErrorKind::Other,
"encoder is finished",
));
}
// Keep trying until _something_ has been consumed.
// As soon as some input has been taken, we cannot afford
// to take any chance: if an error occurs, the user couldn't know
// that some data _was_ successfully written.
loop {
// First, write any pending data from `self.buffer`.
self.write_from_offset()?;
// At this point `self.buffer` can safely be discarded.
// Support writing concatenated frames by re-initializing the
// context.
if self.finished_frame {
self.operation.reinit()?;
self.finished_frame = false;
}
let mut src = InBuffer::around(buf);
let hint = self.with_buffer(|dst, op| op.run(&mut src, dst));
let bytes_read = src.pos;
// eprintln!(
// "Write Hint: {:?}\n src: {:?}\n dst: {:?}",
// hint, src, self.buffer
// );
self.offset = 0;
let hint = hint?;
if hint == 0 {
self.finished_frame = true;
}
// As we said, as soon as we've consumed something, return.
if bytes_read > 0 || buf.is_empty() {
// println!("Returning {}", bytes_read);
return Ok(bytes_read);
}
}
}
fn flush(&mut self) -> io::Result<()> {
let mut finished = self.finished;
loop {
// If the output is blocked or has an error, return now.
self.write_from_offset()?;
if finished {
break;
}
let hint = self.with_buffer(|dst, op| op.flush(dst));
self.offset = 0;
let hint = hint?;
finished = hint == 0;
}
self.writer.flush()
}
}
#[cfg(test)]
mod tests {
use super::Writer;
use std::io::Write;
#[test]
fn test_noop() {
use crate::stream::raw::NoOp;
let input = b"AbcdefghAbcdefgh.";
// Test writer
let mut output = Vec::new();
{
let mut writer = Writer::new(&mut output, NoOp);
writer.write_all(input).unwrap();
writer.finish().unwrap();
}
assert_eq!(&output, input);
}
#[test]
fn test_compress() {
use crate::stream::raw::Encoder;
let input = b"AbcdefghAbcdefgh.";
// Test writer
let mut output = Vec::new();
{
let mut writer =
Writer::new(&mut output, Encoder::new(1).unwrap());
writer.write_all(input).unwrap();
writer.finish().unwrap();
}
// println!("Output: {:?}", output);
let decoded = crate::decode_all(&output[..]).unwrap();
assert_eq!(&decoded, input);
}
#[test]
fn test_compress_with_capacity() {
use crate::stream::raw::Encoder;
let input = b"AbcdefghAbcdefgh.";
// Test writer
let mut output = Vec::new();
{
let mut writer = Writer::new_with_capacity(
&mut output,
Encoder::new(1).unwrap(),
64,
);
assert_eq!(writer.buffer.capacity(), 64);
writer.write_all(input).unwrap();
writer.finish().unwrap();
}
let decoded = crate::decode_all(&output[..]).unwrap();
assert_eq!(&decoded, input);
}
#[test]
fn test_decompress() {
use crate::stream::raw::Decoder;
let input = b"AbcdefghAbcdefgh.";
let compressed = crate::encode_all(&input[..], 1).unwrap();
// Test writer
let mut output = Vec::new();
{
let mut writer = Writer::new(&mut output, Decoder::new().unwrap());
writer.write_all(&compressed).unwrap();
writer.finish().unwrap();
}
// println!("Output: {:?}", output);
assert_eq!(&output, input);
}
#[test]
fn test_decompress_with_capacity() {
use crate::stream::raw::Decoder;
let input = b"AbcdefghAbcdefgh.";
let compressed = crate::encode_all(&input[..], 1).unwrap();
// Test writer
let mut output = Vec::new();
{
let mut writer = Writer::new_with_capacity(
&mut output,
Decoder::new().unwrap(),
64,
);
assert_eq!(writer.buffer.capacity(), 64);
writer.write_all(&compressed).unwrap();
writer.finish().unwrap();
}
assert_eq!(&output, input);
}
}