Advertisement
Risiko94

DemoUtils

Oct 19th, 2021
1,051
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.49 KB | None | 0 0
  1. package main;
  2.  
  3. import java.io.File;
  4. import java.util.Arrays;
  5.  
  6. import DataTypes.Message;
  7.  
  8. public class DemoUtils {
  9.  
  10.     public static short ByteToShort(byte byte1, byte byte2) {
  11.         return (short) (byte2<<8 | byte1 & 0xFF);
  12.     }
  13.  
  14.     public static float ByteToFloat(byte byte1, byte byte2, byte byte3, byte byte4) {
  15.         return Float.intBitsToFloat(byte4<<24 | (byte3&0xFF)<<16 | (byte2&0xFF)<<8 | byte1 & 0xFF);
  16.     }
  17.  
  18.     public static float ByteToFloat(byte[] bytes, int start) {
  19.         return ByteToFloat(bytes[start], bytes[start+1], bytes[start+2], bytes[start+3]);
  20.     }
  21.  
  22.     public static int ByteToInt(byte byte1, byte byte2, byte byte3, byte byte4) {
  23.         return (byte4<<24 | (byte3&0xFF)<<16 | (byte2&0xFF)<<8 | byte1 & 0xFF);
  24.     }
  25.  
  26.     public static int ByteToInt(byte[] bytes, int start) {
  27.         return ByteToInt(bytes[start], bytes[start+1], bytes[start+2], bytes[start+3]);
  28.     }
  29.  
  30.     public static String ByteToHex (byte[] bytes) {
  31.         String output = "";
  32.         for (int i = 0; i < bytes.length; i++) {
  33.             output=output.concat(String.format("%02x",bytes[i]));
  34.         }
  35.         return output;
  36.     }
  37.  
  38.     public static String ByteToHex (byte bytes) {
  39.         return String.format("%02x",bytes);
  40.     }
  41.  
  42.     public static String ByteToLongHex (byte bytes) {
  43.         return String.format("%#04x",bytes);
  44.     }
  45.    
  46.     //get a message from byte[] at bytenumber
  47.     public static Message getMessage(byte[] demo, int bytenumber) {
  48.         short messagelength = (short)(DemoUtils.ByteToShort(demo[bytenumber], demo[bytenumber+1]) + 2);
  49.         Message message = new Message(bytenumber, Arrays.copyOfRange(demo, bytenumber, bytenumber+messagelength));
  50.         return message;
  51.     }
  52.  
  53.     //give the location of the first instance of BYTE in data. Starts searching at data[start] to skip unwanted instances of BYTE.
  54.     public static int findByte(byte[] data, int start, byte BYTE) {
  55.         try {
  56.             for (int i = 0; i  + start < data.length; i++) {
  57.                 if (data[i + start] == BYTE) {
  58.                     return (i + start);
  59.                 }
  60.             }
  61.             throw new Exception ("Couldn't find X"+ BYTE + "X");
  62.         } catch (Exception e) {
  63.             e.printStackTrace();
  64.             return 0;
  65.         }
  66.     }
  67.  
  68.     //give the location of the first instance of a zerobyte in data[].Starts searching at data[start] to skip unwanted zerobytes.
  69.     public static int findZeroByte (byte[] data, int start) {
  70.         return findByte (data, start, (byte) 0x00);
  71.     }
  72.  
  73.     //get Bytes from data[start] to data[end]
  74.     public static byte[] getBytes(byte[] data, int start, int end) {
  75.         byte[] output  = new byte[end - start];
  76.         for (int i = 0; i < output.length;i++) {
  77.             output[i] = data[i+start];
  78.         }
  79.         return output;
  80.     }
  81.  
  82.     //get Bytes from data[start] until it finds BYTE
  83.     public static byte[] getBytes(byte[] data, int start, byte BYTE) {
  84.         return getBytes(data, start, findByte(data, start, BYTE));
  85.     }
  86.  
  87.     //get Bytes from data[0] until it finds BYTE
  88.     public static byte[] getBytes(byte[] data, byte BYTE) {
  89.         return getBytes(data, 0, BYTE);
  90.     }
  91.  
  92.     //get Bytes from data[0] until it finds a zerobyte 0x00
  93.     public static byte[] getBytesBeforeZeroByte(byte[] data, int start) {
  94.         return getBytes(data, start, findByte(data, start, (byte) 0x00));
  95.     }
  96.  
  97.     //get Bytes from data[0] until it finds a zerobyte 0x00 and return it as string
  98.     public static String getStringBeforeZeroByte(byte[] data, int start) {
  99.         return new String(getBytesBeforeZeroByte(data, start));
  100.     }
  101.    
  102.     //check if a File ends with .PRdemo
  103.     public static boolean isCorrectFile(File demofile) {
  104.         int i = demofile.toString().lastIndexOf(".");
  105.         return (i > 0 && demofile.toString().substring(i).equals(".PRdemo"));  
  106.     }
  107.  
  108. }
  109.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement