Risiko94

Message.java

Oct 19th, 2021
506
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package DataTypes;
  2. import DataTypes.Type;
  3.  
  4. public class Message {
  5.     private static int counter;
  6.     final private int bytenumber;
  7.     final private int number;
  8.     final private short length;
  9.     final private short datalength;
  10.     final private Type type;
  11.     final private byte[] data;
  12.  
  13.     public String toString() {
  14.         String output = toStringNoData()
  15.                 .concat("\nData:" +getDataAsHex());
  16.         return output;
  17.     }
  18.  
  19.     public String toStringNoData() {
  20.         String output = ("Message#: " + getNumber())
  21.                 .concat("\tByte: " + getBytenumber())
  22.                 .concat("  \tLength: " + getLength())
  23.                 .concat("\tDatalength: " + getDatalength())
  24.                 .concat("\tType: " + getType().toString());
  25.         return output;
  26.     }
  27.    
  28.     public static void resetCounter() {
  29.         counter = 1;
  30.     }
  31.     public int getBytenumber(){
  32.         return this.bytenumber;
  33.     }
  34.     public int getNumber() {
  35.         return this.number;
  36.     }
  37.  
  38.     public short getLength() {
  39.         return this.length;
  40.     }
  41.  
  42.     public short getDatalength() {
  43.         return this.datalength;
  44.     }
  45.  
  46.     public Type getType() {
  47.         return this.type;
  48.     }
  49.  
  50.     public byte[] getData() {
  51.         return this.data;
  52.     }
  53.  
  54.     public byte getData(int i) {
  55.         return this.data[i];
  56.     }
  57.  
  58.     public String getDataAsText() {
  59.         return (new String(getData(), 0, getDatalength()));
  60.     }
  61.  
  62.     public String getDataAsHex() {
  63.         String output = "";
  64.         for (int i = 0; i < getDatalength(); i++) {
  65.             output=output.concat(String.format("%02x",getData(i)));
  66.         }
  67.         return output;
  68.     }
  69.    
  70.     public String getDataAsHex(int i) {
  71.         return String.format("%#04x",getData(i));
  72.     }
  73.  
  74.     public Message(int bytenumber, short length, byte type, byte[] data) {
  75.         this.number = counter;
  76.         this.bytenumber = bytenumber;
  77.         this.length = length;
  78.         this.type = new Type (type);
  79.         this.data = data;
  80.         this.datalength = (short) (length-2);
  81.         counter++;
  82.     }
  83.  
  84.     public Message(int bytenumber, byte[] message) {
  85.         this.number = counter;
  86.         this.bytenumber = bytenumber;
  87.         this.length = (short) ((message[1]<<8 | message[0] & 0xFF)+2);
  88.         this.datalength = (short) ((message[1]<<8 | message[0] & 0xFF)-1);
  89.         this.type = new Type (message[2]);
  90.         this.data = new byte[this.datalength];
  91.         for (int i = 0;i < this.datalength;i++) {
  92.             this.data[i] = message[i+3];
  93.         }
  94.         counter++;     
  95.     }  
  96. }
RAW Paste Data