pub struct DecodingReader<R> { /* private fields */ }encoding only.Expand description
A reader wrapper that decodes a byte stream from any supported encoding into UTF-8.
This mirrors quick_xml::encoding::DecodingReader of quick-xml 0.41, but is
backed by the Pure-Rust oxixml-encoding crate instead of
encoding_rs.
The Reader scans for ASCII delimiters in the raw
byte stream and therefore only understands ASCII-compatible encodings. Wrap
the input in this reader to parse a document in an encoding that is not
ASCII-compatible, such as UTF-16.
On the first access the encoding is detected from the byte-order mark or from
the byte pattern of the XML declaration (see detect_encoding); it defaults
to UTF-8. Once the declaration has been parsed, the encoding it names can be
installed with set_encoding.
§Note on the declared encoding
What this reader hands to the parser is already UTF-8, but the encoding
pseudo-attribute of the transcoded XML declaration still says what the
original bytes were. With the encoding feature enabled the
Reader picks that name up for its own
Decoder, exactly as quick-xml 0.41 does, and would then decode the
event payloads a second time. Documents encoded in UTF-16 normally rely on
the byte-order mark and carry no encoding pseudo-attribute, in which case
nothing special is needed; if the declaration does name the original
encoding, either build the reader without the encoding feature — the
payloads are UTF-8 by then anyway — or take the payload bytes of the events
as-is instead of running them through the reader’s Decoder again.
§Examples
UTF-8 input passes through unchanged:
use std::io::Read;
use oxixml_xml::encoding::DecodingReader;
let data = b"Hello, World!";
let mut reader = DecodingReader::new(&data[..]);
let mut buf = Vec::new();
reader.read_to_end(&mut buf).expect("read");
assert_eq!(buf, data);A UTF-16 document becomes readable by the Reader:
use oxixml_xml::encoding::DecodingReader;
use oxixml_xml::events::Event;
use oxixml_xml::reader::Reader;
let mut xml = vec![0xFF, 0xFE]; // UTF-16 LE BOM
for unit in "<?xml version='1.0' encoding='UTF-16'?><element/>".encode_utf16() {
xml.extend_from_slice(&unit.to_le_bytes());
}
let mut reader = Reader::from_reader(DecodingReader::new(xml.as_slice()));
let mut buf = Vec::new();
assert!(matches!(reader.read_event_into(&mut buf).expect("decl"), Event::Decl(_)));
buf.clear();
assert!(matches!(reader.read_event_into(&mut buf).expect("element"), Event::Empty(_)));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 around inner.
The encoding is auto-detected from the byte-order mark or from the XML declaration pattern on the first access, defaulting to UTF-8.
Sourcepub const fn get_mut(&mut self) -> &mut R
pub const fn get_mut(&mut self) -> &mut R
Returns a mutable reference to the underlying reader.
Sourcepub fn into_inner(self) -> R
pub fn into_inner(self) -> R
Consumes this reader and returns the underlying reader.
Sourcepub const fn encoding(&self) -> Encoding
pub const fn encoding(&self) -> Encoding
Returns the encoding the decoder currently uses.
Before the first read this is always UTF-8; afterwards it reflects the detected — or explicitly installed — encoding.
Sourcepub fn set_encoding(&mut self, encoding: Encoding) -> bool
pub fn set_encoding(&mut self, encoding: Encoding) -> bool
Replaces the decoder with one for encoding, keeping the position in the
stream.
Call this right after the XML declaration has been parsed, that is while the upfront-read prefix is still being served. Once the prefix is drained the encoding can no longer be changed and the call is ignored, because the bytes that follow have already been fed to the current decoder.
Returns whether the encoding was installed. Setting the encoding that is already in use always succeeds and changes nothing (replacing the decoder would discard the partial sequence it may hold).
Trait Implementations§
Source§impl<R: BufRead> BufRead for DecodingReader<R>
impl<R: BufRead> BufRead for DecodingReader<R>
Source§fn consume(&mut self, amount: usize)
fn consume(&mut self, amount: 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 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<'_, u8>) -> Result<(), Error>
fn read_buf(&mut self, buf: BorrowedCursor<'_, u8>) -> Result<(), Error>
read_buf)Source§fn read_buf_exact(
&mut self,
cursor: BorrowedCursor<'_, u8>,
) -> Result<(), Error>
fn read_buf_exact( &mut self, cursor: BorrowedCursor<'_, u8>, ) -> 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 more1.0.0 · Source§fn chain<R>(self, next: R) -> Chain<Self, R> ⓘ
fn chain<R>(self, next: R) -> Chain<Self, R> ⓘ
1.0.0 · Source§fn take(self, limit: u64) -> Take<Self> ⓘwhere
Self: Sized,
fn take(self, limit: u64) -> Take<Self> ⓘwhere
Self: Sized,
limit bytes from it. Read moreSource§fn read_array<const N: usize>(&mut self) -> Result<[u8; N], Error>where
Self: Sized,
fn read_array<const N: usize>(&mut self) -> Result<[u8; N], Error>where
Self: Sized,
read_array)