summaryrefslogtreecommitdiff
path: root/src/com/encrox/twitchbot/client/Utils.java
blob: dccb4ce7b4e60d0f2a394f0a0b7d5f3400b0a049 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package com.encrox.twitchbot.client;

import java.nio.ByteBuffer;
import java.util.Arrays;

public class Utils {
	
	public static int ba_to_int(byte[] arr) {
		return ByteBuffer.wrap(arr).getInt();
	}
	
	public static byte[] int_to_ba(int integer) {
		return ByteBuffer.allocate(4).putInt(integer).array();
	}
	
	public static short ba_to_short(byte[] arr) {
		return ByteBuffer.wrap(arr).getShort();
	}
	
	public static byte[] short_to_ba(short s) {
		return ByteBuffer.allocate(2).putShort(s).array();
	}
	
	public static byte[] fill(byte[] arr, int length) {
		if(length <= arr.length)
			return arr;
		byte[] output = new byte[length];
		for(int i = 0; i<arr.length; i++)
			output[i] = arr[i];
		for(int i = arr.length; i<length; i++)
			output[i] = (byte) 0x00;
		return output;
	}
	
	public static byte[] crop(byte[] arr) {
		byte[] swap = new byte[arr.length];
		for(int i = 0; i<swap.length; i++) {
			if(arr[i] != (byte) 0x00)
				swap[i] = arr[i];
			else {
				arr = Arrays.copyOfRange(swap, 0, i);
				break;
			}
		}
		return arr;
	}

}