rs_bytebuffer/byte_buf/
byte_reader.rs1use byteorder::{BigEndian, ByteOrder, LittleEndian};
21
22use crate::{get_number, read_number};
23use crate::byte_buf::{ByteBufResult, ErrorType};
24
25pub struct ByteReader {
26 byte_array: Vec<u8>,
27 capacity: usize,
28 read_index: usize,
29 read_mark: isize,
30}
31
32
33impl ByteReader {
34 pub fn new_from(src: &[u8]) -> Self {
35 ByteReader {
36 byte_array: Vec::from(src),
37 capacity: src.len(),
38 read_index: 0,
39 read_mark: -1,
40 }
41 }
42
43 pub fn capacity(&self) -> usize {
44 self.capacity
45 }
46
47 pub fn readable(&self) -> usize {
48 self.capacity - self.read_index
49 }
50
51 pub fn mark_read_index(&mut self) -> &mut Self {
52 self.read_mark = self.read_index as isize;
53 self
54 }
55
56 pub fn reset_read_index(&mut self) {
57 if self.read_mark == -1 {
58 return;
59 }
60 self.read_index = self.read_mark as usize;
61 self.read_mark = -1;
62 }
63
64 pub fn get_bytes_of_write(&self, target: &mut [u8]) -> ByteBufResult<()> {
65 if self.readable() < target.len() {
66 return Err(ErrorType::ReadableShortage);
67 }
68
69 for i in self.read_index..target.len() {
70 target[i] = self.byte_array[i]
71 }
72
73 Ok(())
74 }
75
76 pub fn get_bytes_of_length(&self, length: usize) -> ByteBufResult<Vec<u8>> {
77 if self.readable() < length {
78 return Err(ErrorType::ReadableShortage);
79 }
80
81 let bytes = &self.byte_array.as_slice()[self.read_index..self.read_index + length];
82
83 Ok(Vec::from(bytes))
84 }
85
86 pub fn get_u8(&self) -> ByteBufResult<u8> {
87 return Ok(self.byte_array[self.read_index]);
88 }
89
90 pub fn get_i8(&self) -> ByteBufResult<i8> {
91 return Ok(self.byte_array[self.read_index] as i8);
92 }
93
94 pub fn get_u16_be(&self) -> ByteBufResult<u16> {
95 get_number!(self, read_u16, 2, true)
96 }
97
98 pub fn get_u16_le(&self) -> ByteBufResult<u16> {
99 get_number!(self, read_u16, 2, false)
100 }
101
102 pub fn get_i16_be(&self) -> ByteBufResult<i16> {
103 get_number!(self, read_i16, 2, true)
104 }
105
106 pub fn get_i16_le(&self) -> ByteBufResult<i16> {
107 get_number!(self, read_i16, 2, true)
108 }
109
110 pub fn get_u32_be(&self) -> ByteBufResult<u32> {
111 get_number!(self, read_u32, 2, true)
112 }
113
114 pub fn get_u32_le(&self) -> ByteBufResult<u32> {
115 get_number!(self, read_u32, 4, false)
116 }
117
118 pub fn get_i32_be(&self) -> ByteBufResult<i32> {
119 get_number!(self, read_i32, 4, true)
120 }
121
122 pub fn get_i32_le(&self) -> ByteBufResult<i32> {
123 get_number!(self, read_i32, 4, false)
124 }
125
126 pub fn get_u64_be(&self) -> ByteBufResult<u64> {
127 get_number!(self, read_u64, 8, true)
128 }
129
130 pub fn get_u64_le(&self) -> ByteBufResult<u64> {
131 get_number!(self, read_u64, 8, false)
132 }
133
134 pub fn get_i64_be(&self) -> ByteBufResult<i64> {
135 get_number!(self, read_i64, 8, true)
136 }
137
138 pub fn get_i64_le(&self) -> ByteBufResult<i64> {
139 get_number!(self, read_i64, 8, false)
140 }
141
142 pub fn read_bytes_of_write(&mut self, target: &mut [u8]) -> ByteBufResult<()> {
143 return match self.get_bytes_of_write(target) {
144 Ok(_) => {
145 self.read_index += target.len();
146 Ok(())
147 }
148 Err(error_type) => { Err(error_type) }
149 };
150 }
151
152 pub fn read_bytes_of_length(&mut self, length: usize) -> ByteBufResult<Vec<u8>> {
153 return match self.get_bytes_of_length(length) {
154 Ok(vec) => {
155 self.read_index += length;
156 Ok(vec)
157 }
158 Err(error_type) => {
159 Err(error_type)
160 }
161 };
162 }
163
164 pub fn read_u8(&mut self) -> ByteBufResult<u8> {
165 read_number!(self, get_u8, 1)
166 }
167
168 pub fn read_i8(&mut self) -> ByteBufResult<i8> {
169 read_number!(self, get_i8, 1)
170 }
171
172 pub fn read_u16_be(&mut self) -> ByteBufResult<u16> {
173 read_number!(self, get_u16_be, 2)
174 }
175
176 pub fn read_u16_le(&mut self) -> ByteBufResult<u16> {
177 read_number!(self, get_u16_le, 2)
178 }
179
180 pub fn read_i16_be(&mut self) -> ByteBufResult<i16> {
181 read_number!(self, get_i16_be, 2)
182 }
183
184 pub fn read_i16_le(&mut self) -> ByteBufResult<i16> {
185 read_number!(self, get_i16_le, 2)
186 }
187
188 pub fn read_u32_be(&mut self) -> ByteBufResult<u32> {
189 read_number!(self, get_u32_be, 2)
190 }
191
192 pub fn read_u32_le(&mut self) -> ByteBufResult<u32> {
193 read_number!(self, get_u32_le, 2)
194 }
195
196 pub fn read_i32_be(&mut self) -> ByteBufResult<i32> {
197 read_number!(self, get_i32_be, 2)
198 }
199
200 pub fn read_i32_le(&mut self) -> ByteBufResult<i32> {
201 read_number!(self, get_i32_le, 2)
202 }
203
204 pub fn read_u64_be(&mut self) -> ByteBufResult<u64> {
205 read_number!(self, get_u64_be, 2)
206 }
207
208 pub fn read_u64_le(&mut self) -> ByteBufResult<u64> {
209 read_number!(self, get_u64_le, 2)
210 }
211
212 pub fn read_i64_be(&mut self) -> ByteBufResult<i64> {
213 read_number!(self, get_i64_be, 2)
214 }
215
216 pub fn read_i64_le(&mut self) -> ByteBufResult<i64> {
217 read_number!(self, get_i64_le, 2)
218 }
219}