std::io::BufReader - Rust (original) (raw)

Struct std::io::BufReader1.0.0 [−] [src]

pub struct BufReader { /* fields omitted */ }

The BufReader struct adds buffering to any reader.

It can be excessively inefficient to work directly with a Read instance. For example, every call to read on TcpStreamresults in a system call. A BufReader performs large, infrequent reads on the underlying Read and maintains an in-memory buffer of the results.

use std::io::prelude::*; use std::io::BufReader; use std::fs::File;

let f = File::open("log.txt")?; let mut reader = BufReader::new(f);

let mut line = String::new(); let len = reader.read_line(&mut line)?; println!("First line is {} bytes long", len);Run

impl<R: [Read](../../std/io/trait.Read.html "trait std::io::Read")> [BufReader](../../std/io/struct.BufReader.html "struct std::io::BufReader")<R>[src]

pub fn [new](#method.new)(inner: R) -> [BufReader](../../std/io/struct.BufReader.html "struct std::io::BufReader")<R>[src]

Creates a new BufReader with a default buffer capacity.

use std::io::BufReader; use std::fs::File;

let f = File::open("log.txt")?; let reader = BufReader::new(f);Run

pub fn [with_capacity](#method.with%5Fcapacity)(cap: [usize](../primitive.usize.html), inner: R) -> [BufReader](../../std/io/struct.BufReader.html "struct std::io::BufReader")<R>[src]

Creates a new BufReader with the specified buffer capacity.

Creating a buffer with ten bytes of capacity:

use std::io::BufReader; use std::fs::File;

let f = File::open("log.txt")?; let reader = BufReader::with_capacity(10, f);Run

pub fn [get_ref](#method.get%5Fref)(&self) -> [&](../primitive.reference.html)R[src]

Gets a reference to the underlying reader.

It is inadvisable to directly read from the underlying reader.

use std::io::BufReader; use std::fs::File;

let f1 = File::open("log.txt")?; let reader = BufReader::new(f1);

let f2 = reader.get_ref();Run

pub fn [get_mut](#method.get%5Fmut)(&mut self) -> [&mut ](../primitive.reference.html)R[src]

Gets a mutable reference to the underlying reader.

It is inadvisable to directly read from the underlying reader.

use std::io::BufReader; use std::fs::File;

let f1 = File::open("log.txt")?; let mut reader = BufReader::new(f1);

let f2 = reader.get_mut();Run

pub fn [is_empty](#method.is%5Fempty)(&self) -> [bool](../primitive.bool.html)[src]

🔬 This is a nightly-only experimental API. (bufreader_is_empty #45323)

recently added

Returns true if there are no bytes in the internal buffer.

use std::io::BufReader; use std::io::BufRead; use std::fs::File;

let f1 = File::open("log.txt")?; let mut reader = BufReader::new(f1); assert!(reader.is_empty());

if reader.fill_buf()?.len() > 0 { assert!(!reader.is_empty()); }Run

pub fn [into_inner](#method.into%5Finner)(self) -> R[src]

Unwraps this BufReader, returning the underlying reader.

Note that any leftover data in the internal buffer is lost.

use std::io::BufReader; use std::fs::File;

let f1 = File::open("log.txt")?; let reader = BufReader::new(f1);

let f2 = reader.into_inner();Run

impl<R: [Seek](../../std/io/trait.Seek.html "trait std::io::Seek")> [BufReader](../../std/io/struct.BufReader.html "struct std::io::BufReader")<R>[src]

pub fn [seek_relative](#method.seek%5Frelative)(&mut self, offset: [i64](../primitive.i64.html)) -> [Result](../../std/io/type.Result.html "type std::io::Result")<[()](../primitive.unit.html)>[src]

🔬 This is a nightly-only experimental API. (bufreader_seek_relative #31100)

Seeks relative to the current position. If the new position lies within the buffer, the buffer will not be flushed, allowing for more efficient seeks. This method does not return the location of the underlying reader, so the caller must track this information themselves if it is required.

impl<R: [Read](../../std/io/trait.Read.html "trait std::io::Read")> [Read](../../std/io/trait.Read.html "trait std::io::Read") for [BufReader](../../std/io/struct.BufReader.html "struct std::io::BufReader")<R>[src]

fn [read](../../std/io/trait.Read.html#tymethod.read)(&mut self, buf: [&mut [](../primitive.slice.html)[u8](../primitive.u8.html)[]](../primitive.slice.html)) -> [Result](../../std/io/type.Result.html "type std::io::Result")<[usize](../primitive.usize.html)>[src]

Pull some bytes from this source into the specified buffer, returning how many bytes were read. Read more

unsafe fn [initializer](../../std/io/trait.Read.html#method.initializer)(&self) -> [Initializer](../../std/io/struct.Initializer.html "struct std::io::Initializer")[src]

🔬 This is a nightly-only experimental API. (read_initializer #42788)

Determines if this Reader can work with buffers of uninitialized memory. Read more

fn [read_to_end](../../std/io/trait.Read.html#method.read%5Fto%5Fend)(&mut self, buf: &mut [Vec](../../std/vec/struct.Vec.html "struct std::vec::Vec")<[u8](../primitive.u8.html)>) -> [Result](../../std/io/type.Result.html "type std::io::Result")<[usize](../primitive.usize.html)>[src]

Read all bytes until EOF in this source, placing them into buf. Read more

fn [read_to_string](../../std/io/trait.Read.html#method.read%5Fto%5Fstring)(&mut self, buf: &mut [String](../../std/string/struct.String.html "struct std:🧵:String")) -> [Result](../../std/io/type.Result.html "type std::io::Result")<[usize](../primitive.usize.html)>[src]

Read all bytes until EOF in this source, appending them to buf. Read more

fn [read_exact](../../std/io/trait.Read.html#method.read%5Fexact)(&mut self, buf: [&mut [](../primitive.slice.html)[u8](../primitive.u8.html)[]](../primitive.slice.html)) -> [Result](../../std/io/type.Result.html "type std::io::Result")<[()](../primitive.unit.html)>

1.6.0

[src]

Read the exact number of bytes required to fill buf. Read more

`fn by_ref(&mut self) -> &mut Self where

Self: Sized, `[src]

Creates a "by reference" adaptor for this instance of Read. Read more

ⓘImportant traits for Bytes

fn [bytes](../../std/io/trait.Read.html#method.bytes)(self) -> [Bytes](../../std/io/struct.Bytes.html "struct std::io::Bytes")<Self> where Self: [Sized](../../std/marker/trait.Sized.html "trait std:📑:Sized"), [src]

Transforms this Read instance to an [Iterator] over its bytes. Read more

ⓘImportant traits for Chars

fn [chars](../../std/io/trait.Read.html#method.chars)(self) -> [Chars](../../std/io/struct.Chars.html "struct std::io::Chars")<Self> where Self: [Sized](../../std/marker/trait.Sized.html "trait std:📑:Sized"), [src]

🔬 This is a nightly-only experimental API. (io #27802)

the semantics of a partial read/write of where errors happen is currently unclear and may change

Transforms this Read instance to an [Iterator] over [char]s. Read more

ⓘImportant traits for Chain<T, U>

fn [chain](../../std/io/trait.Read.html#method.chain)<R: [Read](../../std/io/trait.Read.html "trait std::io::Read")>(self, next: R) -> [Chain](../../std/io/struct.Chain.html "struct std::io::Chain")<Self, R> where Self: [Sized](../../std/marker/trait.Sized.html "trait std:📑:Sized"), [src]

Creates an adaptor which will chain this stream with another. Read more

ⓘImportant traits for Take

fn [take](../../std/io/trait.Read.html#method.take)(self, limit: [u64](../primitive.u64.html)) -> [Take](../../std/io/struct.Take.html "struct std::io::Take")<Self> where Self: [Sized](../../std/marker/trait.Sized.html "trait std:📑:Sized"), [src]

Creates an adaptor which will read at most limit bytes from it. Read more

impl<R: [Read](../../std/io/trait.Read.html "trait std::io::Read")> [BufRead](../../std/io/trait.BufRead.html "trait std::io::BufRead") for [BufReader](../../std/io/struct.BufReader.html "struct std::io::BufReader")<R>[src]

fn [fill_buf](../../std/io/trait.BufRead.html#tymethod.fill%5Fbuf)(&mut self) -> [Result](../../std/io/type.Result.html "type std::io::Result")<[&[](../primitive.slice.html)[u8](../primitive.u8.html)[]](../primitive.slice.html)>[src]

Fills the internal buffer of this object, returning the buffer contents. Read more

fn [consume](../../std/io/trait.BufRead.html#tymethod.consume)(&mut self, amt: [usize](../primitive.usize.html))[src]

Tells this buffer that amt bytes have been consumed from the buffer, so they should no longer be returned in calls to read. Read more

fn [read_until](../../std/io/trait.BufRead.html#method.read%5Funtil)(&mut self, byte: [u8](../primitive.u8.html), buf: &mut [Vec](../../std/vec/struct.Vec.html "struct std::vec::Vec")<[u8](../primitive.u8.html)>) -> [Result](../../std/io/type.Result.html "type std::io::Result")<[usize](../primitive.usize.html)>[src]

Read all bytes into buf until the delimiter byte or EOF is reached. Read more

fn [read_line](../../std/io/trait.BufRead.html#method.read%5Fline)(&mut self, buf: &mut [String](../../std/string/struct.String.html "struct std:🧵:String")) -> [Result](../../std/io/type.Result.html "type std::io::Result")<[usize](../primitive.usize.html)>[src]

Read all bytes until a newline (the 0xA byte) is reached, and append them to the provided buffer. Read more

ⓘImportant traits for Split

fn [split](../../std/io/trait.BufRead.html#method.split)(self, byte: [u8](../primitive.u8.html)) -> [Split](../../std/io/struct.Split.html "struct std::io::Split")<Self> where Self: [Sized](../../std/marker/trait.Sized.html "trait std:📑:Sized"), [src]

Returns an iterator over the contents of this reader split on the byte byte. Read more

ⓘImportant traits for Lines

fn [lines](../../std/io/trait.BufRead.html#method.lines)(self) -> [Lines](../../std/io/struct.Lines.html "struct std::io::Lines")<Self> where Self: [Sized](../../std/marker/trait.Sized.html "trait std:📑:Sized"), [src]

Returns an iterator over the lines of this reader. Read more

`impl Debug for BufReader where

R: Debug, `[src]

impl<R: [Seek](../../std/io/trait.Seek.html "trait std::io::Seek")> [Seek](../../std/io/trait.Seek.html "trait std::io::Seek") for [BufReader](../../std/io/struct.BufReader.html "struct std::io::BufReader")<R>[src]

fn [seek](../../std/io/trait.Seek.html#tymethod.seek)(&mut self, pos: [SeekFrom](../../std/io/enum.SeekFrom.html "enum std::io::SeekFrom")) -> [Result](../../std/io/type.Result.html "type std::io::Result")<[u64](../primitive.u64.html)>[src]

Seek to an offset, in bytes, in the underlying reader.

The position used for seeking with SeekFrom::Current(_) is the position the underlying reader would be at if the BufReader had no internal buffer.

Seeking always discards the internal buffer, even if the seek position would otherwise fall within it. This guarantees that calling.into_inner() immediately after a seek yields the underlying reader at the same position.

To seek without discarding the internal buffer, use seek_relative.

See std::io::Seek for more details.

Note: In the edge case where you're seeking with SeekFrom::Current(n)where n minus the internal buffer length overflows an i64, two seeks will be performed instead of one. If the second seek returnsErr, the underlying reader will be left at the same position it would have if you seeked to SeekFrom::Current(0).