use super::EncodingFormat;
use anyhow::Result;
pub struct Utf16;
impl EncodingFormat for Utf16 {
fn into_java(str: String) -> Result<Vec<u8>> {
let utf16_bytes: Vec<u16> = str.encode_utf16().collect();
let mut byte_array: Vec<u8> = Vec::new();
for byte in utf16_bytes {
let [high, low] = byte.to_be_bytes();
byte_array.push(high);
byte_array.push(low);
}
Ok(byte_array)
}
fn from_java(data: Vec<u8>) -> Result<String> {
let mut utf16_bytes = Vec::new();
let mut i = 0;
while i < data.len() {
let bytes = [data[i], data[i + 1]];
let u16 = u16::from_be_bytes(bytes);
utf16_bytes.push(u16);
i += 2;
}
Ok(String::from_utf16(&utf16_bytes)?)
}
}