1use crate::ReadOctetStream;
6use std::io::{Cursor, Read, Result};
7
8pub struct InOctetStream {
9 pub cursor: Cursor<Vec<u8>>,
10}
11
12impl InOctetStream {
13 #[must_use]
14 pub fn new(data: &[u8]) -> Self {
15 Self {
16 cursor: Cursor::new(data.to_vec()),
17 }
18 }
19
20 #[must_use]
21 pub fn new_from_cursor(cursor: Cursor<Vec<u8>>) -> Self {
22 Self { cursor }
23 }
24}
25
26impl ReadOctetStream for InOctetStream {
27 fn read(&mut self, v: &mut [u8]) -> Result<()> {
28 self.cursor.read_exact(v)?;
29 Ok(())
30 }
31
32 fn read_u64(&mut self) -> Result<u64> {
33 let mut buf = [0; 8];
34 self.cursor.read_exact(&mut buf)?;
35 Ok(u64::from_be_bytes(buf))
36 }
37
38 fn read_i64(&mut self) -> Result<i64> {
39 let mut buf = [0; 8];
40 self.cursor.read_exact(&mut buf)?;
41 Ok(i64::from_be_bytes(buf))
42 }
43
44 fn read_u32(&mut self) -> Result<u32> {
45 let mut buf = [0; 4];
46 self.cursor.read_exact(&mut buf)?;
47 Ok(u32::from_be_bytes(buf))
48 }
49 fn read_i32(&mut self) -> Result<i32> {
50 let mut buf = [0; 4];
51 self.cursor.read_exact(&mut buf)?;
52 Ok(i32::from_be_bytes(buf))
53 }
54
55 fn read_u16(&mut self) -> Result<u16> {
56 let mut buf = [0; 2];
57 self.cursor.read_exact(&mut buf)?;
58 Ok(u16::from_be_bytes(buf))
59 }
60
61 fn read_i16(&mut self) -> Result<i16> {
62 let mut buf = [0; 2];
63 self.cursor.read_exact(&mut buf)?;
64 Ok(i16::from_be_bytes(buf))
65 }
66
67 fn read_u8(&mut self) -> Result<u8> {
68 let mut buf = [0; 1];
69 self.cursor.read_exact(&mut buf)?;
70 Ok(buf[0])
71 }
72
73 fn read_i8(&mut self) -> Result<i8> {
74 let mut buf = [0; 1];
75 self.cursor.read_exact(&mut buf)?;
76 Ok(buf[0] as i8)
77 }
78
79 #[must_use]
80 fn has_reached_end(&mut self) -> bool {
81 self.cursor.position() as usize == self.cursor.get_ref().len()
82 }
83}