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
18 System.out.println($CLASSname+" v"+$CLASSvers);
\r
21 // ######################################
\r
22 // # BYTE-CALCULATION #
\r
23 // ######################################
\r
24 public static byte[] int2ba(int i){
\r
25 byte b[] = {(byte) (i >> 0), (byte) (i >> 8), (byte) (i >> 16), (byte) (i >> 24)};
\r
29 public static int ba2int(byte[] b){
\r
30 int i = ((b[0] & 0xff) << 0) + ((b[1] &0xff) << 8) + ((b[2] & 0xff) << 16) + ((b[3] & 0xff) << 24);
\r
35 public static byte[] long2ba(long i){
\r
36 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
40 public static long ba2long(byte[] b){
\r
41 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
45 // Used to change endian.
\r
46 public static byte[] revba(byte data[]){
\r
47 byte rev[] = new byte[data.length];
\r
48 for(int i = 0; i < data.length; i++){
\r
49 rev[i] = data[(data.length -1 - i)];
\r
54 // ######################################
\r
55 // # NUMERIC STUFF #
\r
56 // ######################################
\r
57 static public String leadingZero2(int $in){
\r
58 if($in < 10)return "0"+$in;
\r
62 static public int parseDuration(String $in){
\r
63 String[] $data = $in.split(":");
\r
64 int $ret = Integer.parseInt($data[0])*3600 + Integer.parseInt($data[1])*60 + Integer.parseInt($data[2]);
\r
68 // ######################################
\r
70 // ######################################
\r
71 static public byte[] parseIP(String $in){
\r
72 String[] $data = $in.split("\\.");
\r
73 byte[] $ret = new byte[4];
\r
74 $ret[0] = (byte)Integer.parseInt($data[0]);
\r
75 $ret[1] = (byte)Integer.parseInt($data[1]);
\r
76 $ret[2] = (byte)Integer.parseInt($data[2]);
\r
77 $ret[3] = (byte)Integer.parseInt($data[3]);
\r
81 static public String ba2ips(byte[] $data){
\r
82 return ((int)($data[0] &0xff))+"."+((int)($data[1] &0xff))+"."+((int)($data[2] &0xff))+"."+((int)($data[3] &0xff));
\r
85 //should block until String-data are received!
\r
86 static public String getLine(InputStream $in)throws Exception{
\r
89 int $read = $in.read();
\r
90 if($read == -1){Thread.sleep(1000); continue;}
\r
91 if(($read == 10 || $read == 13) && $ret.length() > 0)break;
\r
92 if($read == 10 || $read == 13)continue;
\r
93 $ret=$ret+((char)($read));
\r
98 //should block until String-data are received!
\r
99 static public byte[] readBA(InputStream $in, int $len)throws Exception{
\r
100 byte[] $arr = new byte[$len];
\r
101 for(int $i=0; $i < $len; $i++){
\r
102 int $read = $in.read();
\r
103 if($read == -1){Thread.sleep(200); $i--; continue;}
\r
104 $arr[$i]=(byte)$read;
\r
109 //should block until one byte is read!
\r
110 static public int readByte(InputStream $in)throws Exception{
\r
114 if($ret != -1)break;
\r