std.net.http.server.BodyReader
type pub BodyReaderA Read type for reading bytes from a Body.
When reading from a Body.File with one or more ranges, BodyReader.read
ignores these ranges and reads from the file directly.
Instance methods
read
Show source codeHide source code
fn pub mut read(into: mut ByteArray, size: Int) -> Result[Int, IoError] {
match @body {
case None -> Result.Ok(0)
case String(v) -> Result.Ok(read_bytes(v, into, size))
case Bytes(v) -> Result.Ok(read_bytes(v, into, size))
case File(f) -> f.data.read(into, size)
case Stream(_) -> Result.Ok(0)
}
}fn pub mut read(into: mut ByteArray, size: Int) -> Result[Int, Error]Reads up to size bytes from self into the given ByteArray, returning
the number of bytes read.
The into argument is the ByteArray to read the bytes into. The capacity
of this ByteArray is increased automatically if necessary.
The size argument specifies how many bytes are to be read.
The return value is the number of bytes read.
The number of bytes read may be less than size. This can happen for
different reasons, such as when all input is consumed or not enough data is
available (yet).
read_all
Show source codeHide source code
fn pub mut read_all(bytes: mut ByteArray) -> Result[Int, E] {
let mut total = 0
let mut read_size = INITIAL_READ_ALL_SIZE
loop {
match read(into: bytes, size: read_size) {
case Ok(0) -> return Result.Ok(total)
case Ok(n) -> {
total += n
# To reduce the number of calls to `Reader.read` when there's lots of
# input to consume, we increase the read size if deemed beneficial.
if read_size < MAX_READ_ALL_SIZE and n == read_size { read_size *= 2 }
}
case Error(e) -> throw e
}
}
}fn pub mut read_all(bytes: mut ByteArray) -> Result[Int, E]Reads from self into the given ByteArray, returning when all input is
consumed.
The return value is the number of bytes read.
Errors
This method returns an Error if the underlying call to Read.read returns
an Error.
read_exact
Show source codeHide source code
fn pub mut read_exact(
into: mut ByteArray,
size: Int,
) -> Result[Nil, ReadExactError[E]] {
let mut pending = size
while pending > 0 {
match read(into, pending) {
case Ok(0) if pending > 0 -> throw ReadExactError.EndOfInput
case Ok(n) -> pending -= n
case Error(e) -> throw ReadExactError.Read(e)
}
}
Result.Ok(nil)
}fn pub mut read_exact(into: mut ByteArray, size: Int) -> Result[Nil, ReadExactError[E]]Reads exactly size bytes into into.
Whereas Read.read might return early if fewer bytes are available in the
input stream, Read.read_exact continues reading until the desired amount
of bytes is read.
Errors
If the end of the input stream is encountered before filling the buffer, an
Error.EndOfInput error is returned.
If an error is returned, no assumption can be made about the state of the
into buffer, i.e. there's no guarantee data read so far is in the buffer
in the event of an error.
Implemented traits
Read
impl Read[Error] for BodyReader