pub struct DecodingReader<R> { /* private fields */ }encoding only.Expand description
A reader wrapper that decodes a byte stream from any encoding into UTF-8.
This reader wraps a BufRead source and uses encoding_rs::Decoder to
transcode the input into valid UTF-8. On first access, it detects the encoding
from BOM or XML declaration byte patterns and configures the appropriate decoder.
For UTF-8 input, this acts as a validating passthrough. For UTF-16 or other encodings, the bytes are transcoded into UTF-8 in an internal buffer.
§Examples
use std::io::Read;
use quick_xml::encoding::DecodingReader;
// UTF-8 input passes through:
let data = b"Hello, World!";
let mut reader = DecodingReader::new(&data[..]);
let mut buf = Vec::new();
reader.read_to_end(&mut buf).unwrap();
assert_eq!(buf, data);The example below shows how you can read documents using DecodingReader:
use quick_xml::encoding::DecodingReader;
use quick_xml::events::Event;
use quick_xml::reader::Reader;
let xml = to_utf16le_with_bom("<?xml encoding='UTF-16'?><element/>");
let mut decoder = DecodingReader::new(xml.as_ref());
let mut reader = Reader::from_reader(decoder);
let mut buf = Vec::new();
loop {
buf.clear();
match reader.read_event_into(&mut buf).unwrap() {
Event::Decl(e) => {
// If XML declaration contains unknown encoding name, None is returned
match e.encoder() {
Some(encoding) => reader.get_mut().set_encoding(encoding),
None => panic!("Unsupported encoding {:?}", e.encoding()),
}
}
Event::Eof => break,
_ => {}
}
}Implementations§
Source§impl<R> DecodingReader<R>
impl<R> DecodingReader<R>
Sourcepub fn new(inner: R) -> Self
pub fn new(inner: R) -> Self
Creates a new decoding reader.
The encoding is auto-detected from BOM or XML declaration patterns on first access. Defaults to UTF-8 if no pattern is recognized.
Sourcepub fn into_inner(self) -> R
pub fn into_inner(self) -> R
Consumes this reader and returns the underlying reader
Sourcepub fn encoding(&self) -> &'static Encoding
pub fn encoding(&self) -> &'static Encoding
Returns the encoding currently used by the decoder.
Before the first read, this is always UTF-8. After encoding detection it reflects the detected (or overridden) encoding.
Sourcepub fn set_encoding(&mut self, encoding: &'static Encoding)
pub fn set_encoding(&mut self, encoding: &'static Encoding)
Replaces the decoder with one for the given encoding. The encoding must be ASCII-compatible (the parser cannot read the declaration otherwise).
§Panics
Panics if the prefix buffer has already been drained. Must be called before the prefix is exhausted — in practice, right after parsing the XML declaration.
Trait Implementations§
Source§impl<R: BufRead> BufRead for DecodingReader<R>
impl<R: BufRead> BufRead for DecodingReader<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> Debug for DecodingReader<R>
impl<R: Debug> Debug for DecodingReader<R>
Source§impl<R: BufRead> Read for DecodingReader<R>
impl<R: BufRead> Read for DecodingReader<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