Skip to main content

DecodingReader

Struct DecodingReader 

Source
pub struct DecodingReader<R> { /* private fields */ }
Available on crate feature 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>

Source

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.

Source

pub const fn get_ref(&self) -> &R

Returns a reference to the underlying reader.

Source

pub const fn get_mut(&mut self) -> &mut R

Returns a mutable reference to the underlying reader.

Source

pub fn into_inner(self) -> R

Consumes this reader and returns the underlying reader.

Source

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.

Source

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>

Source§

fn fill_buf(&mut self) -> Result<&[u8]>

Returns the contents of the internal buffer, filling it with more data, via Read methods, if empty. Read more
Source§

fn consume(&mut self, amount: usize)

Marks the given 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 more
Source§

fn has_data_left(&mut self) -> Result<bool, Error>

🔬This is a nightly-only experimental API. (buf_read_has_data_left)
Checks if there is any data left to be read. Read more
1.0.0 · Source§

fn read_until(&mut self, byte: u8, buf: &mut Vec<u8>) -> Result<usize, Error>

Reads all bytes into buf until the delimiter byte or EOF is reached. Read more
1.83.0 · Source§

fn skip_until(&mut self, byte: u8) -> Result<usize, Error>

Skips all bytes until the delimiter byte or EOF is reached. Read more
1.0.0 · Source§

fn read_line(&mut self, buf: &mut String) -> Result<usize, Error>

Reads all bytes until a newline (the 0xA byte) is reached, and append them to the provided String buffer. Read more
1.0.0 · Source§

fn split(self, byte: u8) -> Split<Self> ⓘ
where Self: Sized,

Returns an iterator over the contents of this reader split on the byte byte. Read more
1.0.0 · Source§

fn lines(self) -> Lines<Self> ⓘ
where Self: Sized,

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

impl<R: Debug> Debug for DecodingReader<R>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<R: BufRead> Read for DecodingReader<R>

Source§

fn read(&mut self, buf: &mut [u8]) -> Result<usize>

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

fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>

Like read, except that it reads into a slice of buffers. Read more
Source§

fn is_read_vectored(&self) -> bool

🔬This is a nightly-only experimental API. (can_vector)
Determines if this Reader has an efficient read_vectored implementation. Read more
1.0.0 · Source§

fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, Error>

Reads all bytes until EOF in this source, placing them into buf. Read more
1.0.0 · Source§

fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>

Reads all bytes until EOF in this source, appending them to buf. Read more
1.6.0 · Source§

fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>

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

fn read_buf(&mut self, buf: BorrowedCursor<'_, u8>) -> Result<(), Error>

🔬This is a nightly-only experimental API. (read_buf)
Pull some bytes from this source into the specified buffer. Read more
Source§

fn read_buf_exact( &mut self, cursor: BorrowedCursor<'_, u8>, ) -> Result<(), Error>

🔬This is a nightly-only experimental API. (read_buf)
Reads the exact number of bytes required to fill cursor. Read more
1.0.0 · Source§

fn by_ref(&mut self) -> &mut Self
where Self: Sized,

Creates a “by reference” adapter for this instance of Read. Read more
1.0.0 · Source§

fn bytes(self) -> Bytes<Self> ⓘ
where Self: Sized,

Transforms this Read instance to an Iterator over its bytes. Read more
1.0.0 · Source§

fn chain<R>(self, next: R) -> Chain<Self, R> ⓘ
where R: Read, Self: Sized,

Creates an adapter which will chain this stream with another. Read more
1.0.0 · Source§

fn take(self, limit: u64) -> Take<Self> ⓘ
where Self: Sized,

Creates an adapter which will read at most limit bytes from it. Read more
Source§

fn read_array<const N: usize>(&mut self) -> Result<[u8; N], Error>
where Self: Sized,

🔬This is a nightly-only experimental API. (read_array)
Read and return a fixed array of bytes from this source. Read more
Source§

fn read_le<T>(&mut self) -> Result<T, Error>
where T: FromEndianBytes, Self: Sized,

🔬This is a nightly-only experimental API. (read_le)
Read and return a type (e.g. an integer) in little-endian order. Read more
Source§

fn read_be<T>(&mut self) -> Result<T, Error>
where T: FromEndianBytes, Self: Sized,

🔬This is a nightly-only experimental API. (read_le)
Read and return a type (e.g. an integer) in big-endian order. Read more

Auto Trait Implementations§

§

impl<R> Freeze for DecodingReader<R>
where R: Freeze,

§

impl<R> RefUnwindSafe for DecodingReader<R>
where R: RefUnwindSafe,

§

impl<R> Send for DecodingReader<R>
where R: Send,

§

impl<R> Sync for DecodingReader<R>
where R: Sync,

§

impl<R> Unpin for DecodingReader<R>
where R: Unpin,

§

impl<R> UnsafeUnpin for DecodingReader<R>
where R: UnsafeUnpin,

§

impl<R> UnwindSafe for DecodingReader<R>
where R: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.