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 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>

Source

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.

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 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.

Source

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>

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, amt: 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<'_>) -> 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<'_>) -> 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

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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.