How to idiomatically / efficiently pipe data from Read+Seek to Write?

I want to take data from random locations in input file, and output them sequentially to output file. Preferably, with no unnecessary allocations.

This is one kind of solution I have figured out:

use std::io::{ self, SeekFrom, Cursor, Read, Write, Seek };

#[test]
fn read_write() {
    // let's say this is input file
    let mut input_file = Cursor::new(b"worldhello");
    // and this is output file
    let mut output_file = Vec::<u8>::new();

    assemble(&mut input_file, &mut output_file).unwrap();

    assert_eq!(b"helloworld", &output_file[..]);
}

// I want to take data from random locations in input file
// and output them sequentially to output file
pub fn assemble<I, O>(input: &mut I, output: &mut O) -> Result<(), io::Error> 
    where I: Read + Seek, O: Write 
{
    // first seek and output "hello"
    try!(input.seek(SeekFrom::Start(5)));
    let mut hello_buf = [0u8; 5];
    try!(input.take(5).read(&mut hello_buf));
    try!(output.write(&hello_buf));

    // then output "world"
    try!(input.seek(SeekFrom::Start(0)));
    let mut world_buf = [0u8; 5];
    try!(input.take(5).read(&mut world_buf));
    try!(output.write(&world_buf));

    Ok(())
}

Let's not worry about I/O latency here.

Questions:

  • Does the stable Rust have some helper to take x bytes from one stream and push them to another stream? Or do I have to roll my own?
  • If I have to roll my own, maybe there is a better way?

  • You are looking for io::copy :

    pub fn assemble<I, O>(input: &mut I, output: &mut O) -> Result<(), io::Error> 
        where I: Read + Seek, O: Write 
    {
        // first seek and output "hello"
        try!(input.seek(SeekFrom::Start(5)));
        try!(io::copy(&mut input.take(5), output));
    
        // then output "world"
        try!(input.seek(SeekFrom::Start(0)));
        try!(io::copy(&mut input.take(5), output));
    
        Ok(())
    }
    

    If you look at the implementation of io::copy , you can see that it's similar to your code. However, it takes care to handle more error cases:

  • write does not always write everything you ask it to!
  • An "interrupted" write isn't usually fatal.
  • It also uses a larger buffer size but still stack-allocates it.

    链接地址: http://www.djcxy.com/p/88570.html

    上一篇: 如何将Groovy脚本添加到具有经典Maven结构的Java项目中?

    下一篇: 如何从Read + Seek到地道/有效地管道数据写入?