Read until buffer is full or EOF

In a loop, I want to read from rouille::Request.data() (the body of the incoming HTTP request) and pass the chunks to an encryption function.

Is there a builtin function that can read from a Read until the buffer is full, except at the end? From reading the documentation Read::read() doesn't seem to guarantee that the buffer is full and read_exact() fails at the last chunk.

Read is designed to be implementable for streaming sources, so read has the option to not completely fill the buffer, instead giving you the bytes that have already arrived in a non-blocking manner.

If you want to work with a buffered reader, look at BufReader. It looks like careful use of BufRead::fill_buf() will probably work for you.

2 Likes

Oh, yeah, this ruins your goal:

If this function encounters an “end of file” before completely filling the buffer, it returns an error of the kind ErrorKind::UnexpectedEof. The contents of buf are unspecified in this case.

I guess the problem is that read_exact doesn't have a way to communicate how much of the buffer was actually initialized, so it can't really do anything else.

You might consider raising this in the tracking issue for Read::read_buf_exact, since it could probably do better, as the cursor tracks which parts are initialized.

1 Like

That is already in Nightly, isn't it? I am using Nightly, but I don't see how that BorrowedCursor thing can help me.

Instead of read_exact use:

vec.clear();
reader.by_ref().take(max_bytes_to_read).read_to_end(&mut vec);

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.