///|
pub fn read_u16_be(input : Bytes, offset : Int) -> Result[Int, ProxyError] {
  if offset < 0 || offset + 2 > input.length() {
    Err(proxy_error(NeedMoreData, offset, "u16 needs two bytes"))
  } else {
    Ok((input[offset].to_int() << 8) | input[offset + 1].to_int())
  }
}

///|
pub fn read_u32_be(input : Bytes, offset : Int) -> Result[UInt, ProxyError] {
  if offset < 0 || offset + 4 > input.length() {
    Err(proxy_error(NeedMoreData, offset, "u32 needs four bytes"))
  } else {
    Ok(
      (input[offset].to_uint() << 24) |
      (input[offset + 1].to_uint() << 16) |
      (input[offset + 2].to_uint() << 8) |
      input[offset + 3].to_uint(),
    )
  }
}

///|
pub fn write_u16_be(value : Int) -> Bytes {
  Bytes::from_array([(value >> 8).to_byte(), value.to_byte()])
}

///|
pub fn write_u32_be(value : UInt) -> Bytes {
  Bytes::from_array([
    (value >> 24).to_byte(),
    (value >> 16).to_byte(),
    (value >> 8).to_byte(),
    value.to_byte(),
  ])
}