pub struct BufReaderEncoding<R> { /* private fields */ }
Expand description
Modificate std::io::BufReader
Implementations§
Source§impl<R: Read> BufReaderEncoding<R>
impl<R: Read> BufReaderEncoding<R>
pub fn new(inner: R, encoder: EncodingRef) -> BufReaderEncoding<R> ⓘ
pub fn with_capacity( cap: usize, inner: R, encoder: EncodingRef, ) -> BufReaderEncoding<R> ⓘ
Sourcepub fn lines(self) -> Lines<R> ⓘ
pub fn lines(self) -> Lines<R> ⓘ
Returns an iterator over the lines of this reader.
The iterator returned from this function will yield instances of
io::Result
<
String
>
. Each string returned will not have a newline
byte (the 0xA byte) or CRLF (0xD, 0xA bytes) at the end.
§Examples
std::io::Cursor
is a type that implements BufRead
. In
this example, we use Cursor
to iterate over all the lines in a byte
slice.
use encodingbufreader::{BufReaderEncoding};
use encoding::all::UTF_8;
let bytes = "This string\nwill be read".as_bytes();
let mut lines_iter = BufReaderEncoding::new(bytes,UTF_8).map(|l| l.unwrap());
assert_eq!(lines_iter.next(), Some(String::from("This string")));
assert_eq!(lines_iter.next(), Some(String::from("will be read")));
assert_eq!(lines_iter.next(), None);
§Errors
Each line of the iterator has the same error semantics as BufRead::read_line
.
Sourcepub fn read_line(&mut self, buf: &mut String) -> Result<usize>
pub fn read_line(&mut self, buf: &mut String) -> Result<usize>
Read all bytes until a newline (the 0xA byte) is reached, and append them to the provided buffer.
This function will read bytes from the underlying stream until the
newline delimiter (the 0xA byte) or EOF is found. Once found, all bytes
up to, and including, the delimiter (if found) will be appended to
buf
.
If successful, this function will return the total number of bytes read.
If this function returns Ok(0)
, the stream has reached EOF.
§Errors
This function has the same error semantics as [std::io::Read::read_until
] and will
also return an error if the read bytes are not valid encoding. If an I/O
error is encountered then buf
may contain some bytes already read in
the event that all data read so far was valid encoding.
§Examples
use encodingbufreader::{BufReaderEncoding};
use encoding::all::GB18030;
let bytes: &[u8] = &[
213, 226, 202, 199, 210, 187, 184, 246, 215, 214, 183, 251, 180, 174, 10, 189, 171,
187, 225, 177, 187, 182, 193, 200, 161,
];
let mut bufreader = BufReaderEncoding::new(bytes, GB18030);
let mut buf = String::new();
let num_bytes = bufreader
.read_line(&mut buf)
.expect("reading from bytes won't fail");
assert_eq!(num_bytes, 15);
assert_eq!(buf, "这是一个字符串\n");
pub fn set_encoder(&mut self, encoder: EncodingRef)
Trait Implementations§
Source§impl<R: Read> BufRead for BufReaderEncoding<R>
impl<R: Read> BufRead for BufReaderEncoding<R>
Source§fn fill_buf(&mut self) -> Result<&[u8]>
fn fill_buf(&mut self) -> Result<&[u8]>
Read
methods, if empty. Read moreSource§fn consume(&mut self, amt: usize)
fn consume(&mut self, amt: usize)
amount
of additional bytes from the internal buffer as having been read.
Subsequent calls to read
only return bytes that have not been marked as read. Read moreSource§fn has_data_left(&mut self) -> Result<bool, Error>
fn has_data_left(&mut self) -> Result<bool, Error>
buf_read_has_data_left
)read
. Read more1.83.0 · Source§fn skip_until(&mut self, byte: u8) -> Result<usize, Error>
fn skip_until(&mut self, byte: u8) -> Result<usize, Error>
byte
or EOF is reached. Read more1.0.0 · Source§fn read_line(&mut self, buf: &mut String) -> Result<usize, Error>
fn read_line(&mut self, buf: &mut String) -> Result<usize, Error>
0xA
byte) is reached, and append
them to the provided String
buffer. Read moreSource§impl<R> Debug for BufReaderEncoding<R>where
R: Debug,
impl<R> Debug for BufReaderEncoding<R>where
R: Debug,
Source§impl<R: Read> Read for BufReaderEncoding<R>
impl<R: Read> Read for BufReaderEncoding<R>
Source§fn read(&mut self, buf: &mut [u8]) -> Result<usize>
fn read(&mut self, buf: &mut [u8]) -> Result<usize>
1.36.0 · Source§fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>
fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>
read
, except that it reads into a slice of buffers. Read moreSource§fn is_read_vectored(&self) -> bool
fn is_read_vectored(&self) -> bool
can_vector
)1.0.0 · Source§fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, Error>
fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, Error>
buf
. Read more1.0.0 · Source§fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
buf
. Read more1.6.0 · Source§fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
buf
. Read moreSource§fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> Result<(), Error>
fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> Result<(), Error>
read_buf
)Source§fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> Result<(), Error>
fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> Result<(), Error>
read_buf
)cursor
. Read more1.0.0 · Source§fn by_ref(&mut self) -> &mut Selfwhere
Self: Sized,
fn by_ref(&mut self) -> &mut Selfwhere
Self: Sized,
Read
. Read more