oximedia-io 0.1.0

I/O layer for OxiMedia
Documentation
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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
//! Chunked-write abstraction for I/O pipelines.
//!
//! Splits a byte stream into fixed-size chunks and optionally applies a
//! per-chunk transform (e.g. checksumming, padding, alignment). Useful for
//! writing media data to disk or network in controlled units.

#![allow(dead_code)]

use std::io::{self, Write};

// ---------------------------------------------------------------------------
// Chunk metadata
// ---------------------------------------------------------------------------

/// Metadata emitted for each completed chunk.
#[derive(Debug, Clone)]
pub struct ChunkInfo {
    /// Zero-based index of this chunk.
    pub index: u64,
    /// Number of payload bytes in this chunk.
    pub payload_len: usize,
    /// Running total of payload bytes written (including this chunk).
    pub cumulative_bytes: u64,
    /// Whether this is the final (possibly short) chunk.
    pub is_final: bool,
}

// ---------------------------------------------------------------------------
// ChunkedWriter
// ---------------------------------------------------------------------------

/// A writer that buffers output into fixed-size chunks before flushing
/// each chunk to the inner writer.
///
/// When the internal buffer reaches `chunk_size`, the chunk is flushed and
/// the optional callback is invoked with [`ChunkInfo`].
pub struct ChunkedWriter<W, F> {
    /// Inner writer.
    inner: W,
    /// Per-chunk callback.
    callback: F,
    /// Buffer accumulating the current chunk.
    buffer: Vec<u8>,
    /// Target chunk size.
    chunk_size: usize,
    /// Number of chunks written.
    chunk_count: u64,
    /// Cumulative payload bytes written.
    total_bytes: u64,
}

impl<W: Write, F: FnMut(&ChunkInfo)> ChunkedWriter<W, F> {
    /// Create a new `ChunkedWriter` with the given chunk size and callback.
    ///
    /// # Panics
    ///
    /// Panics if `chunk_size` is zero.
    pub fn new(inner: W, chunk_size: usize, callback: F) -> Self {
        assert!(chunk_size > 0, "chunk_size must be > 0");
        Self {
            inner,
            callback,
            buffer: Vec::with_capacity(chunk_size),
            chunk_size,
            chunk_count: 0,
            total_bytes: 0,
        }
    }

    /// Return the number of complete chunks written so far.
    #[must_use]
    pub fn chunk_count(&self) -> u64 {
        self.chunk_count
    }

    /// Return total payload bytes written so far.
    #[must_use]
    pub fn total_bytes(&self) -> u64 {
        self.total_bytes
    }

    /// Return the configured chunk size.
    #[must_use]
    pub fn chunk_size(&self) -> usize {
        self.chunk_size
    }

    /// Return how many bytes are buffered in the current (incomplete) chunk.
    #[must_use]
    pub fn buffered_len(&self) -> usize {
        self.buffer.len()
    }

    /// Flush the current buffer as the final (possibly short) chunk.
    ///
    /// This should be called after all data has been written to ensure any
    /// remaining bytes are flushed. Calling [`Write::flush`] also triggers
    /// this behavior.
    ///
    /// # Errors
    ///
    /// Returns an I/O error if writing or flushing the inner writer fails.
    pub fn finish(&mut self) -> io::Result<()> {
        if !self.buffer.is_empty() {
            self.flush_chunk(true)?;
        }
        self.inner.flush()
    }

    /// Consume this writer and return the inner writer. Remaining buffered
    /// data is flushed first.
    ///
    /// # Errors
    ///
    /// Returns an I/O error if flushing the remaining data fails.
    pub fn into_inner(mut self) -> io::Result<W> {
        self.finish()?;
        Ok(self.inner)
    }

    /// Write the buffered chunk out.
    fn flush_chunk(&mut self, is_final: bool) -> io::Result<()> {
        let payload_len = self.buffer.len();
        self.inner.write_all(&self.buffer)?;
        self.total_bytes += payload_len as u64;

        let info = ChunkInfo {
            index: self.chunk_count,
            payload_len,
            cumulative_bytes: self.total_bytes,
            is_final,
        };
        (self.callback)(&info);

        self.chunk_count += 1;
        self.buffer.clear();
        Ok(())
    }
}

impl<W: Write, F: FnMut(&ChunkInfo)> Write for ChunkedWriter<W, F> {
    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
        let mut offset = 0;
        while offset < buf.len() {
            let remaining_cap = self.chunk_size - self.buffer.len();
            let to_copy = remaining_cap.min(buf.len() - offset);
            self.buffer
                .extend_from_slice(&buf[offset..offset + to_copy]);
            offset += to_copy;

            if self.buffer.len() >= self.chunk_size {
                self.flush_chunk(false)?;
            }
        }
        Ok(buf.len())
    }

    fn flush(&mut self) -> io::Result<()> {
        self.finish()
    }
}

// ---------------------------------------------------------------------------
// AlignedChunkWriter
// ---------------------------------------------------------------------------

/// A writer that pads each chunk to a fixed alignment before writing.
///
/// Useful for writing to block devices or formats that require aligned
/// offsets (e.g. 512-byte or 4096-byte alignment).
pub struct AlignedChunkWriter<W> {
    /// Inner writer.
    inner: W,
    /// Alignment in bytes.
    alignment: usize,
    /// Padding byte value.
    pad_byte: u8,
    /// Buffer for current chunk.
    buffer: Vec<u8>,
    /// Chunks written.
    chunks_written: u64,
}

impl<W: Write> AlignedChunkWriter<W> {
    /// Create a new aligned writer.
    ///
    /// # Panics
    ///
    /// Panics if `alignment` is zero or not a power of two.
    pub fn new(inner: W, alignment: usize) -> Self {
        assert!(
            alignment > 0 && alignment.is_power_of_two(),
            "alignment must be a power of two"
        );
        Self {
            inner,
            alignment,
            pad_byte: 0,
            buffer: Vec::with_capacity(alignment),
            chunks_written: 0,
        }
    }

    /// Set the padding byte (default 0).
    #[must_use]
    pub fn with_pad_byte(mut self, byte: u8) -> Self {
        self.pad_byte = byte;
        self
    }

    /// Return the number of aligned chunks written.
    #[must_use]
    pub fn chunks_written(&self) -> u64 {
        self.chunks_written
    }

    /// Flush the current buffer, padding to alignment.
    ///
    /// # Errors
    ///
    /// Returns an I/O error if writing or flushing the inner writer fails.
    pub fn finish(&mut self) -> io::Result<()> {
        if !self.buffer.is_empty() {
            let pad_len = self.alignment - (self.buffer.len() % self.alignment);
            if pad_len < self.alignment {
                self.buffer
                    .resize(self.buffer.len() + pad_len, self.pad_byte);
            }
            self.inner.write_all(&self.buffer)?;
            self.chunks_written += 1;
            self.buffer.clear();
        }
        self.inner.flush()
    }
}

impl<W: Write> Write for AlignedChunkWriter<W> {
    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
        let mut offset = 0;
        while offset < buf.len() {
            let remaining = self.alignment - self.buffer.len();
            let to_copy = remaining.min(buf.len() - offset);
            self.buffer
                .extend_from_slice(&buf[offset..offset + to_copy]);
            offset += to_copy;

            if self.buffer.len() >= self.alignment {
                self.inner.write_all(&self.buffer)?;
                self.chunks_written += 1;
                self.buffer.clear();
            }
        }
        Ok(buf.len())
    }

    fn flush(&mut self) -> io::Result<()> {
        self.finish()
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_chunked_basic() {
        let mut out = Vec::new();
        let mut infos = Vec::new();
        {
            let mut w = ChunkedWriter::new(&mut out, 4, |info| {
                infos.push(info.clone());
            });
            w.write_all(b"abcdefghij").unwrap();
            w.finish().unwrap();
        }
        assert_eq!(out, b"abcdefghij");
        // 4 + 4 = 2 full chunks, plus 2 remaining = 3 chunks
        assert_eq!(infos.len(), 3);
        assert_eq!(infos[0].payload_len, 4);
        assert_eq!(infos[1].payload_len, 4);
        assert_eq!(infos[2].payload_len, 2);
        assert!(infos[2].is_final);
    }

    #[test]
    fn test_chunked_exact_multiple() {
        let mut out = Vec::new();
        let mut count = 0u64;
        {
            let mut w = ChunkedWriter::new(&mut out, 5, |_| count += 1);
            w.write_all(b"12345").unwrap();
            w.finish().unwrap();
        }
        assert_eq!(out, b"12345");
        assert_eq!(count, 1); // exactly one full chunk, finish is no-op
    }

    #[test]
    fn test_chunked_empty() {
        let mut out = Vec::new();
        let mut count = 0u64;
        {
            let mut w = ChunkedWriter::new(&mut out, 10, |_| count += 1);
            w.finish().unwrap();
        }
        assert!(out.is_empty());
        assert_eq!(count, 0);
    }

    #[test]
    fn test_chunk_info_fields() {
        let mut out = Vec::new();
        let mut infos = Vec::new();
        {
            let mut w = ChunkedWriter::new(&mut out, 3, |info| infos.push(info.clone()));
            w.write_all(b"abcdef").unwrap();
            w.finish().unwrap();
        }
        assert_eq!(infos[0].index, 0);
        assert_eq!(infos[0].cumulative_bytes, 3);
        assert_eq!(infos[1].index, 1);
        assert_eq!(infos[1].cumulative_bytes, 6);
    }

    #[test]
    fn test_chunked_total_bytes() {
        let mut out = Vec::new();
        let mut w = ChunkedWriter::new(&mut out, 8, |_| {});
        w.write_all(b"hello world").unwrap();
        w.finish().unwrap();
        assert_eq!(w.total_bytes(), 11);
    }

    #[test]
    fn test_chunked_chunk_count() {
        let mut out = Vec::new();
        let mut w = ChunkedWriter::new(&mut out, 4, |_| {});
        w.write_all(b"0123456789ab").unwrap();
        w.finish().unwrap();
        assert_eq!(w.chunk_count(), 3);
    }

    #[test]
    fn test_chunked_buffered_len() {
        let mut out = Vec::new();
        let mut w = ChunkedWriter::new(&mut out, 10, |_| {});
        w.write_all(b"abc").unwrap();
        assert_eq!(w.buffered_len(), 3);
    }

    #[test]
    #[should_panic(expected = "chunk_size must be > 0")]
    fn test_chunked_zero_size_panics() {
        let mut out = Vec::new();
        let _w = ChunkedWriter::new(&mut out, 0, |_| {});
    }

    #[test]
    fn test_aligned_basic() {
        let mut out = Vec::new();
        {
            let mut w = AlignedChunkWriter::new(&mut out, 8);
            w.write_all(b"hello").unwrap();
            w.finish().unwrap();
        }
        assert_eq!(out.len(), 8); // padded to 8
        assert_eq!(&out[..5], b"hello");
        assert!(out[5..].iter().all(|&b| b == 0));
    }

    #[test]
    fn test_aligned_exact() {
        let mut out = Vec::new();
        {
            let mut w = AlignedChunkWriter::new(&mut out, 4);
            w.write_all(b"abcd").unwrap();
            w.finish().unwrap();
        }
        assert_eq!(out, b"abcd");
    }

    #[test]
    fn test_aligned_pad_byte() {
        let mut out = Vec::new();
        {
            let mut w = AlignedChunkWriter::new(&mut out, 8).with_pad_byte(0xFF);
            w.write_all(b"hi").unwrap();
            w.finish().unwrap();
        }
        assert_eq!(out.len(), 8);
        assert_eq!(&out[..2], b"hi");
        assert!(out[2..].iter().all(|&b| b == 0xFF));
    }

    #[test]
    fn test_aligned_chunks_written() {
        let mut out = Vec::new();
        let mut w = AlignedChunkWriter::new(&mut out, 4);
        w.write_all(b"12345678").unwrap(); // two full chunks
        assert_eq!(w.chunks_written(), 2);
    }

    #[test]
    #[should_panic(expected = "alignment must be a power of two")]
    fn test_aligned_non_power_of_two_panics() {
        let mut out = Vec::new();
        let _w = AlignedChunkWriter::new(&mut out, 3);
    }
}