Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main;
- import java.io.File;
- import java.util.Arrays;
- import DataTypes.Message;
- public class DemoUtils {
- public static short ByteToShort(byte byte1, byte byte2) {
- return (short) (byte2<<8 | byte1 & 0xFF);
- }
- public static float ByteToFloat(byte byte1, byte byte2, byte byte3, byte byte4) {
- return Float.intBitsToFloat(byte4<<24 | (byte3&0xFF)<<16 | (byte2&0xFF)<<8 | byte1 & 0xFF);
- }
- public static float ByteToFloat(byte[] bytes, int start) {
- return ByteToFloat(bytes[start], bytes[start+1], bytes[start+2], bytes[start+3]);
- }
- public static int ByteToInt(byte byte1, byte byte2, byte byte3, byte byte4) {
- return (byte4<<24 | (byte3&0xFF)<<16 | (byte2&0xFF)<<8 | byte1 & 0xFF);
- }
- public static int ByteToInt(byte[] bytes, int start) {
- return ByteToInt(bytes[start], bytes[start+1], bytes[start+2], bytes[start+3]);
- }
- public static String ByteToHex (byte[] bytes) {
- String output = "";
- for (int i = 0; i < bytes.length; i++) {
- output=output.concat(String.format("%02x",bytes[i]));
- }
- return output;
- }
- public static String ByteToHex (byte bytes) {
- return String.format("%02x",bytes);
- }
- public static String ByteToLongHex (byte bytes) {
- return String.format("%#04x",bytes);
- }
- //get a message from byte[] at bytenumber
- public static Message getMessage(byte[] demo, int bytenumber) {
- short messagelength = (short)(DemoUtils.ByteToShort(demo[bytenumber], demo[bytenumber+1]) + 2);
- Message message = new Message(bytenumber, Arrays.copyOfRange(demo, bytenumber, bytenumber+messagelength));
- return message;
- }
- //give the location of the first instance of BYTE in data. Starts searching at data[start] to skip unwanted instances of BYTE.
- public static int findByte(byte[] data, int start, byte BYTE) {
- try {
- for (int i = 0; i + start < data.length; i++) {
- if (data[i + start] == BYTE) {
- return (i + start);
- }
- }
- throw new Exception ("Couldn't find X"+ BYTE + "X");
- } catch (Exception e) {
- e.printStackTrace();
- return 0;
- }
- }
- //give the location of the first instance of a zerobyte in data[].Starts searching at data[start] to skip unwanted zerobytes.
- public static int findZeroByte (byte[] data, int start) {
- return findByte (data, start, (byte) 0x00);
- }
- //get Bytes from data[start] to data[end]
- public static byte[] getBytes(byte[] data, int start, int end) {
- byte[] output = new byte[end - start];
- for (int i = 0; i < output.length;i++) {
- output[i] = data[i+start];
- }
- return output;
- }
- //get Bytes from data[start] until it finds BYTE
- public static byte[] getBytes(byte[] data, int start, byte BYTE) {
- return getBytes(data, start, findByte(data, start, BYTE));
- }
- //get Bytes from data[0] until it finds BYTE
- public static byte[] getBytes(byte[] data, byte BYTE) {
- return getBytes(data, 0, BYTE);
- }
- //get Bytes from data[0] until it finds a zerobyte 0x00
- public static byte[] getBytesBeforeZeroByte(byte[] data, int start) {
- return getBytes(data, start, findByte(data, start, (byte) 0x00));
- }
- //get Bytes from data[0] until it finds a zerobyte 0x00 and return it as string
- public static String getStringBeforeZeroByte(byte[] data, int start) {
- return new String(getBytesBeforeZeroByte(data, start));
- }
- //check if a File ends with .PRdemo
- public static boolean isCorrectFile(File demofile) {
- int i = demofile.toString().lastIndexOf(".");
- return (i > 0 && demofile.toString().substring(i).equals(".PRdemo"));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement