BufferedReader

Struct BufferedReader 

Source
pub struct BufferedReader<'a> { /* private fields */ }

Implementations§

Source§

impl<'a> BufferedReader<'a>

Source

pub fn new(data: &'a [u8]) -> Self

Examples found in repository?
examples/decoding.rs (line 7)
5fn main() {
6    let input = include_bytes!("./testdata/org/example/Simple.class");
7    let mut buffer = BufferedReader::new(input);
8    let mut constant_pool = ConstantPool::new();
9    let cf = read_classfile(&mut buffer, &mut constant_pool).unwrap();
10
11    cf.methods.iter().for_each(|method| {
12        dbg!(&method.descriptor);
13    })
14}
More examples
Hide additional examples
examples/instructions.rs (line 9)
7fn main() {
8    let input = include_bytes!("./testdata/org/example/Simple.class");
9    let mut buffer = BufferedReader::new(input);
10    let mut constant_pool = ConstantPool::new();
11    let cf = read_classfile(&mut buffer, &mut constant_pool).unwrap();
12
13    println!("Class: {}", constant_pool.text_of(cf.this_class).unwrap());
14    // print all methods and their instructions
15    cf.methods.iter().for_each(|method| {
16        let attr = method
17            .attributes
18            .iter()
19            .find(|attr| matches!(attr, Attribute::Code(_)));
20        println!("Name: {}()", method.name);
21
22        let code = if let Attribute::Code(code) = attr.unwrap() {
23            code
24        } else {
25            panic!("Code attribute not found");
26        };
27
28        // create a new buffer that contains just the code and nothing else...
29        let mut code_reader = BufferedReader::new(&code.code);
30        while !code_reader.has_remaining_data() {
31            let opcode = code_reader.take::<u8>().unwrap();
32            let instr =
33                parse_instruction(opcode, &mut code_reader).expect("instruction should be parsed");
34            println!("  0x{opcode:02x}: {}", instr);
35        }
36        println!();
37    });
38}
examples/printelements.rs (line 7)
5fn main() {
6    let input = include_bytes!("./testdata/org/example/Simple.class");
7    let mut buffer = BufferedReader::new(input);
8    let mut cp = ConstantPool::new();
9    let cf = read_classfile(&mut buffer, &mut cp).unwrap();
10
11    println!("Class name: {}", cp.text_of(cf.this_class).unwrap());
12
13    cf.fields.iter().for_each(|field| {
14        println!("Field {} - {}", field.name, field.descriptor);
15    });
16
17    // cf.methods.iter().for_each(|method| {
18    //     let parameters: Vec<Descriptor> = method
19    //         .descriptor
20    //         .clone()
21    //         .into_iter()
22    //         .filter(|desc| desc.kind == DescriptorKind::Parameter)
23    //         .collect();
24    //     let return_ty: Descriptor = method
25    //         .descriptor
26    //         .clone()
27    //         .into_iter()
28    //         .find(|desc| desc.kind == DescriptorKind::Return)
29    //         .unwrap_or(Descriptor {
30    //             kind: DescriptorKind::Return,
31    //             ty: FieldType::Base(BaseType::Void),
32    //         });
33    //
34    //     println!(
35    //         "{} {}({})",
36    //         return_ty,
37    //         method.name,
38    //         parameters
39    //             .iter()
40    //             .map(|p| p.ty.to_string())
41    //             .collect::<Vec<String>>()
42    //             .join(", ")
43    //     );
44    // });
45}
Source

pub fn take<T>(&mut self) -> Result<T, BytecodeError>
where T: FromBytes,

Examples found in repository?
examples/instructions.rs (line 31)
7fn main() {
8    let input = include_bytes!("./testdata/org/example/Simple.class");
9    let mut buffer = BufferedReader::new(input);
10    let mut constant_pool = ConstantPool::new();
11    let cf = read_classfile(&mut buffer, &mut constant_pool).unwrap();
12
13    println!("Class: {}", constant_pool.text_of(cf.this_class).unwrap());
14    // print all methods and their instructions
15    cf.methods.iter().for_each(|method| {
16        let attr = method
17            .attributes
18            .iter()
19            .find(|attr| matches!(attr, Attribute::Code(_)));
20        println!("Name: {}()", method.name);
21
22        let code = if let Attribute::Code(code) = attr.unwrap() {
23            code
24        } else {
25            panic!("Code attribute not found");
26        };
27
28        // create a new buffer that contains just the code and nothing else...
29        let mut code_reader = BufferedReader::new(&code.code);
30        while !code_reader.has_remaining_data() {
31            let opcode = code_reader.take::<u8>().unwrap();
32            let instr =
33                parse_instruction(opcode, &mut code_reader).expect("instruction should be parsed");
34            println!("  0x{opcode:02x}: {}", instr);
35        }
36        println!();
37    });
38}
Source

pub fn peek_bytes<T>(&self) -> Result<T, BytecodeError>
where T: FromBytes,

Source

pub fn take_bytes(&mut self, length: usize) -> Result<&'a [u8], BytecodeError>

Source

pub fn size(&self) -> usize

Returns the size of BufferedReader’s data in bytes.

Source

pub fn position(&self) -> usize

Returns the current position of BufferedReader in bytes.

Source

pub fn has_remaining_data(&self) -> bool

Indicates whether the reader has remaining data to be read.

Examples found in repository?
examples/instructions.rs (line 30)
7fn main() {
8    let input = include_bytes!("./testdata/org/example/Simple.class");
9    let mut buffer = BufferedReader::new(input);
10    let mut constant_pool = ConstantPool::new();
11    let cf = read_classfile(&mut buffer, &mut constant_pool).unwrap();
12
13    println!("Class: {}", constant_pool.text_of(cf.this_class).unwrap());
14    // print all methods and their instructions
15    cf.methods.iter().for_each(|method| {
16        let attr = method
17            .attributes
18            .iter()
19            .find(|attr| matches!(attr, Attribute::Code(_)));
20        println!("Name: {}()", method.name);
21
22        let code = if let Attribute::Code(code) = attr.unwrap() {
23            code
24        } else {
25            panic!("Code attribute not found");
26        };
27
28        // create a new buffer that contains just the code and nothing else...
29        let mut code_reader = BufferedReader::new(&code.code);
30        while !code_reader.has_remaining_data() {
31            let opcode = code_reader.take::<u8>().unwrap();
32            let instr =
33                parse_instruction(opcode, &mut code_reader).expect("instruction should be parsed");
34            println!("  0x{opcode:02x}: {}", instr);
35        }
36        println!();
37    });
38}

Trait Implementations§

Source§

impl<'a> Clone for BufferedReader<'a>

Source§

fn clone(&self) -> BufferedReader<'a>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'a> Debug for BufferedReader<'a>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a> Freeze for BufferedReader<'a>

§

impl<'a> RefUnwindSafe for BufferedReader<'a>

§

impl<'a> Send for BufferedReader<'a>

§

impl<'a> Sync for BufferedReader<'a>

§

impl<'a> Unpin for BufferedReader<'a>

§

impl<'a> UnwindSafe for BufferedReader<'a>

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.