1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
use napi_derive::napi;
use napi::bindgen_prelude::*;
use napi::{ Result, Status::GenericFailure };
#[napi]
pub struct BinaryStream {
/**
* **binary**
*
* The binary data of the stream.
*/
pub binary: Vec<u8>,
/**
* **offset**
*
* The current offset of the stream.
*/
pub offset: u32,
}
#[napi]
impl BinaryStream {
/**
* **BinaryStream**
*
* Creates a new BinaryStream with an optional JavaScript Buffer.
*/
#[napi(constructor)]
pub fn new(buffer: Option<Buffer>, offset: Option<u32>) -> Self {
// Match the buffer, if none is provided, create a new buffer.
let bin = match buffer {
Some(buffer) => buffer,
None => Buffer::from(vec![]),
};
// Match the offset, if none is provided, set it to 0.
let offset = match offset {
Some(offset) => offset,
None => 0,
};
// Return the new BinaryStream.
BinaryStream {
binary: bin.to_vec(),
offset,
}
}
/**
* **from**
*
* Creates a new BinaryStream from a binary vector.
*/
#[napi(factory)]
pub fn from(binary: Vec<u8>, offset: Option<u32>) -> Self {
// Match the offset, if none is provided, set it to 0.
let offset = match offset {
Some(offset) => offset,
None => 0,
};
// Return the new BinaryStream.
BinaryStream {
binary,
offset,
}
}
/**
* **fromBuffer**
*
* Creates a new BinaryStream from a JavaScript Buffer.
*/
#[napi(factory)]
pub fn from_buffer(buffer: Buffer, offset: Option<u32>) -> Self {
// Match the offset, if none is provided, set it to 0.
let offset = match offset {
Some(offset) => offset,
None => 0,
};
// Return the new BinaryStream.
BinaryStream {
binary: buffer.to_vec(),
offset,
}
}
/**
* **read**
*
* Reads a number of bytes from the stream.
*/
#[napi]
pub fn read(&mut self, length: u32) -> Result<Vec<u8>> {
// Check if the length is greater than the remaining bytes.
if length > self.binary.len() as u32 {
return Err(
Error::new(
GenericFailure,
"Length is greater than the remaining bytes.".to_string()
)
)
}
// Get the start and end of the bytes.
let start = self.offset as usize;
let end = (self.offset + length) as usize;
self.offset += length;
Ok(self.binary[start..end].to_vec())
}
/**
* **readBuffer**
*
* Reads a number of bytes from the stream and returns a JavaScript Buffer.
*/
#[napi]
pub fn read_buffer(&mut self, length: u32) -> Result<Buffer> {
let bytes = match self.read(length) {
Ok(bytes) => bytes,
Err(err) => return Err(err)
};
Ok(Buffer::from(bytes))
}
/**
* **write**
*
* Writes a number of bytes to the stream.
*/
#[napi]
pub fn write(&mut self, data: Vec<u8>) {
self.binary.extend(data);
}
/**
* ***writeBuffer*
*
* Writes a JavaScript Buffer to the stream.
*/
#[napi]
pub fn write_buffer(&mut self, data: Buffer) {
data.to_vec();
}
/**
* **readRemaining**
*
* Reads the remaining bytes from the stream.
*/
#[napi]
pub fn read_remaining(&mut self) -> Vec<u8> {
let start = self.offset as usize;
let end = self.binary.len();
self.offset = end as u32;
self.binary[start..end].to_vec()
}
/**
* **readRemainingBuffer**
*
* Reads the remaining bytes from the stream and returns a JavaScript Buffer.
*/
#[napi]
pub fn read_remaining_buffer(&mut self) -> Buffer {
let bytes = self.read_remaining();
Buffer::from(bytes)
}
/**
* **skip**
*
* Skips a number of bytes from the stream.
*/
#[napi]
pub fn skip(&mut self, length: u32) {
self.offset += length;
}
/**
* **cursorAtEnd**
*
* Checks if the cursor is at the end of the stream.
*/
#[napi]
pub fn cursor_at_end(&self) -> bool {
self.offset == self.binary.len() as u32
}
/**
* **cursorAtStart**
*
* Checks if the cursor is at the start of the stream.
*/
#[napi]
pub fn cursor_at_start(&self) -> bool {
self.offset == 0
}
/**
* **getBuffer**
*
* Gets the binary as a JavaScript Buffer.
*/
#[napi]
pub fn get_buffer(&self) -> Buffer {
Buffer::from(self.binary.clone())
}
}