3 import java.io.InputStream;
\r
10 * Misc functions, that are used more than once, but dont fit into a category/class
\r
12 public class Helper{
\r
13 final static String $CLASSname = "Helper";
\r
14 final static String $CLASSvers = "0.06";
\r
17 System.out.println($CLASSname+" v"+$CLASSvers);
\r
20 // ######################################
\r
21 // # BYTE-CALCULATION #
\r
22 // ######################################
\r
23 public static byte[] int2ba(int i){
\r
24 byte b[] = {(byte) (i >> 0), (byte) (i >> 8), (byte) (i >> 16), (byte) (i >> 24)};
\r
28 public static int ba2int(byte[] b){
\r
29 int i = ((b[0] & 0xff) << 0) + ((b[1] &0xff) << 8) + ((b[2] & 0xff) << 16) + ((b[3] & 0xff) << 24);
\r
34 public static byte[] long2ba(long i){
\r
35 byte b[] = {(byte) (i >> 0), (byte) (i >> 8), (byte) (i >> 16), (byte) (i >> 24), (byte) (i >> 32), (byte) (i >> 40), (byte) (i >> 48), (byte) (i >> 56)};
\r
39 public static long ba2long(byte[] b){
\r
40 long l = (((long)(b[0] & 0xff)) << 0) + (((long)(b[1] & 0xff)) << 8) + (((long)(b[2] & 0xff)) << 16) + (((long)(b[3] & 0xff)) << 24) + (((long)(b[4] & 0xff)) << 32) + (((long)(b[5] & 0xff)) << 40) + (((long)(b[6] & 0xff)) << 48) + (((long)(b[7] & 0xff)) << 56);
\r
44 // Used to change endian.
\r
45 public static byte[] revba(byte data[]){
\r
46 byte rev[] = new byte[data.length];
\r
47 for(int i = 0; i < data.length; i++){
\r
48 rev[i] = data[(data.length -1 - i)];
\r
53 // ######################################
\r
54 // # NUMERIC STUFF #
\r
55 // ######################################
\r
56 static public String leadingZero2(int $in){
\r
57 if($in < 10)return "0"+$in;
\r
61 static public int parseDuration(String $in){
\r
62 String[] $data = $in.split(":");
\r
63 int $ret = Integer.parseInt($data[0])*3600 + Integer.parseInt($data[1])*60 + Integer.parseInt($data[2]);
\r
67 // ######################################
\r
69 // ######################################
\r
70 static public byte[] parseIP(String $in){
\r
71 String[] $data = $in.split("\\.");
\r
72 byte[] $ret = new byte[4];
\r
73 $ret[0] = (byte)Integer.parseInt($data[0]);
\r
74 $ret[1] = (byte)Integer.parseInt($data[1]);
\r
75 $ret[2] = (byte)Integer.parseInt($data[2]);
\r
76 $ret[3] = (byte)Integer.parseInt($data[3]);
\r
80 static public String ba2ips(byte[] $data){
\r
81 return ((int)($data[0] &0xff))+"."+((int)($data[1] &0xff))+"."+((int)($data[2] &0xff))+"."+((int)($data[3] &0xff));
\r
84 //should block until String-data are received!
\r
85 static public String getLine(InputStream $in)throws Exception{
\r
88 int $read = $in.read();
\r
89 if($read == -1){Thread.sleep(1000); continue;}
\r
90 if(($read == 10 || $read == 13) && $ret.length() > 0)break;
\r
91 if($read == 10 || $read == 13)continue;
\r
92 $ret=$ret+((char)($read));
\r
97 //should block until String-data are received!
\r
98 static public byte[] readBA(InputStream $in, int $len)throws Exception{
\r
99 byte[] $arr = new byte[$len];
\r
100 for(int $i=0; $i < $len; $i++){
\r
101 int $read = $in.read();
\r
102 if($read == -1){Thread.sleep(200); $i--; continue;}
\r
103 $arr[$i]=(byte)$read;
\r
108 //should block until one byte is read!
\r
109 static public int readByte(InputStream $in)throws Exception{
\r
113 if($ret != -1)break;
\r