13
|
1 |
/** |
|
2 |
* Java RTP Library (jlibrtp) |
|
3 |
* Copyright (C) 2006 Arne Kepp |
|
4 |
* |
|
5 |
* This library is free software; you can redistribute it and/or |
|
6 |
* modify it under the terms of the GNU Lesser General Public |
|
7 |
* License as published by the Free Software Foundation; either |
|
8 |
* version 2.1 of the License, or (at your option) any later version. |
|
9 |
* |
|
10 |
* This library is distributed in the hope that it will be useful, |
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
13 |
* Lesser General Public License for more details. |
|
14 |
* |
|
15 |
* You should have received a copy of the GNU Lesser General Public |
|
16 |
* License along with this library; if not, write to the Free Software |
|
17 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
|
18 |
*/ |
|
19 |
package jlibrtp; |
|
20 |
|
|
21 |
/** |
|
22 |
* Generic functions for converting between unsigned integers and byte[]s. |
|
23 |
* |
|
24 |
* @author Arne Kepp |
|
25 |
*/ |
|
26 |
public class StaticProcs { |
|
27 |
|
|
28 |
/** |
|
29 |
* Converts an integer into an array of bytes. |
|
30 |
* Primarily used for 16 bit unsigned integers, ignore the first two octets. |
|
31 |
* |
|
32 |
* @param i a 16 bit unsigned integer in an int |
|
33 |
* @return byte[2] representing the integer as unsigned, most significant bit first. |
|
34 |
*/ |
|
35 |
public static byte[] uIntIntToByteWord(int i) { |
|
36 |
byte[] byteWord = new byte[2]; |
|
37 |
byteWord[0] = (byte) ((i >> 8) & 0x000000FF); |
|
38 |
byteWord[1] = (byte) (i & 0x00FF); |
|
39 |
return byteWord; |
|
40 |
} |
|
41 |
|
|
42 |
/** |
|
43 |
* Converts an unsigned 32 bit integer, stored in a long, into an array of bytes. |
|
44 |
* |
|
45 |
* @param j a long |
|
46 |
* @return byte[4] representing the unsigned integer, most significant bit first. |
|
47 |
*/ |
|
48 |
public static byte[] uIntLongToByteWord(long j) { |
|
49 |
int i = (int) j; |
|
50 |
byte[] byteWord = new byte[4]; |
|
51 |
byteWord[0] = (byte) ((i >>> 24) & 0x000000FF); |
|
52 |
byteWord[1] = (byte) ((i >> 16) & 0x000000FF); |
|
53 |
byteWord[2] = (byte) ((i >> 8) & 0x000000FF); |
|
54 |
byteWord[3] = (byte) (i & 0x00FF); |
|
55 |
return byteWord; |
|
56 |
} |
|
57 |
|
|
58 |
/** |
|
59 |
* Combines two bytes (most significant bit first) into a 16 bit unsigned integer. |
|
60 |
* |
|
61 |
* @param index of most significant byte |
|
62 |
* @return int with the 16 bit unsigned integer |
|
63 |
*/ |
|
64 |
public static int bytesToUIntInt(byte[] bytes, int index) { |
|
65 |
int accum = 0; |
|
66 |
int i = 1; |
|
67 |
for (int shiftBy = 0; shiftBy < 16; shiftBy += 8 ) { |
|
68 |
accum |= ( (long)( bytes[index + i] & 0xff ) ) << shiftBy; |
|
69 |
i--; |
|
70 |
} |
|
71 |
return accum; |
|
72 |
} |
|
73 |
|
|
74 |
/** |
|
75 |
* Combines four bytes (most significant bit first) into a 32 bit unsigned integer. |
|
76 |
* |
|
77 |
* @param bytes |
|
78 |
* @param index of most significant byte |
|
79 |
* @return long with the 32 bit unsigned integer |
|
80 |
*/ |
|
81 |
public static long bytesToUIntLong(byte[] bytes, int index) { |
|
82 |
long accum = 0; |
|
83 |
int i = 3; |
|
84 |
for (int shiftBy = 0; shiftBy < 32; shiftBy += 8 ) { |
|
85 |
accum |= ( (long)( bytes[index + i] & 0xff ) ) << shiftBy; |
|
86 |
i--; |
|
87 |
} |
|
88 |
return accum; |
|
89 |
} |
|
90 |
|
|
91 |
/** |
|
92 |
* Converts an arbitrary number of bytes, assumed to represent an unsigned integer, |
|
93 |
* to a Java long |
|
94 |
*/ |
|
95 |
/*public static long bytesToUintLong(byte[] bytes, int firstByte, int lastByte) { |
|
96 |
long accum = 0; |
|
97 |
int i = lastByte - firstByte; |
|
98 |
if(i > 7) { |
|
99 |
System.out.println("!!!! StaticProcs.bytesToUintLong() Can't convert more than 63 bits!"); |
|
100 |
return -1; |
|
101 |
} |
|
102 |
int stop = (i+1)*8; |
|
103 |
|
|
104 |
for (int shiftBy = 0; shiftBy < stop; shiftBy += 8 ) { |
|
105 |
accum |= ( (long)( bytes[firstByte + i] & 0xff ) ) << shiftBy; |
|
106 |
i--; |
|
107 |
} |
|
108 |
return accum; |
|
109 |
}*/ |
|
110 |
|
|
111 |
/** |
|
112 |
* Converts an arbitrary number of bytes, assumed to represent an unsigned integer, |
|
113 |
* to a Java int |
|
114 |
*/ |
|
115 |
/* public static int bytesToUintInt(byte[] bytes, int firstByte, int lastByte) { |
|
116 |
int accum = 0; |
|
117 |
int i = lastByte - firstByte; |
|
118 |
if(i > 3) { |
|
119 |
System.out.println("!!!! StaticProcs.bytesToUintLong() Can't convert more than 31 bits!"); |
|
120 |
return -1; |
|
121 |
} |
|
122 |
int stop = (i+1)*8; |
|
123 |
|
|
124 |
for (int shiftBy = 0; shiftBy < stop; shiftBy += 8 ) { |
|
125 |
accum |= ( (long)( bytes[firstByte + i] & 0xff ) ) << shiftBy; |
|
126 |
i--; |
|
127 |
} |
|
128 |
return accum; |
|
129 |
}*/ |
|
130 |
|
|
131 |
/** |
|
132 |
* Recreates a UNIX timestamp based on the NTP representation used |
|
133 |
* in RTCP SR packets |
|
134 |
* |
|
135 |
* @param ntpTs1 from RTCP SR packet |
|
136 |
* @param ntpTs2 from RTCP SR packet |
|
137 |
* @return the UNIX timestamp |
|
138 |
*/ |
|
139 |
public static long undoNtpMess(long ntpTs1, long ntpTs2) { |
|
140 |
long timeVal = (ntpTs1 - 2208988800L)*1000; |
|
141 |
|
|
142 |
double tmp = (1000.0*(double)ntpTs2)/((double)4294967295L); |
|
143 |
long ms = (long) tmp; |
|
144 |
//System.out.println(" timeVal: " +Long.toString(timeVal)+ " ms " + Long.toString(ms)); |
|
145 |
timeVal += ms; |
|
146 |
|
|
147 |
return timeVal; |
|
148 |
} |
|
149 |
|
|
150 |
|
|
151 |
|
|
152 |
/** |
|
153 |
* Get the bits of a byte |
|
154 |
* |
|
155 |
* @param aByte the byte you wish to convert |
|
156 |
* @return a String of 1's and 0's |
|
157 |
*/ |
|
158 |
public static String bitsOfByte(byte aByte) { |
|
159 |
int temp; |
|
160 |
String out = ""; |
|
161 |
for(int i=7; i>=0; i--) { |
|
162 |
temp = (aByte >>> i); |
|
163 |
temp &= 0x0001; |
|
164 |
out += (""+temp); |
|
165 |
} |
|
166 |
return out; |
|
167 |
} |
|
168 |
|
|
169 |
/** |
|
170 |
* Get the hex representation of a byte |
|
171 |
* |
|
172 |
* @param aByte the byte you wish to convert |
|
173 |
* @return a String of two chars 0-1,A-F |
|
174 |
*/ |
|
175 |
public static String hexOfByte(byte aByte) { |
|
176 |
String out = ""; |
|
177 |
|
|
178 |
for(int i=0; i<2; i++) { |
|
179 |
int temp = (int) aByte; |
|
180 |
if(temp < 0) { |
|
181 |
temp +=256; |
|
182 |
} |
|
183 |
if(i == 0) { |
|
184 |
temp = temp/16; |
|
185 |
} else { |
|
186 |
temp = temp%16; |
|
187 |
} |
|
188 |
|
|
189 |
if( temp > 9) { |
|
190 |
switch(temp) { |
|
191 |
case 10: out += "A"; break; |
|
192 |
case 11: out += "B"; break; |
|
193 |
case 12: out += "C"; break; |
|
194 |
case 13: out += "D"; break; |
|
195 |
case 14: out += "E"; break; |
|
196 |
case 15: out += "F"; break; |
|
197 |
} |
|
198 |
} else { |
|
199 |
out += Integer.toString(temp); |
|
200 |
} |
|
201 |
} |
|
202 |
return out; |
|
203 |
} |
|
204 |
|
|
205 |
/** |
|
206 |
* Get the hex representation of a byte |
|
207 |
* |
|
208 |
* @param hex 4 bytes the byte you wish to convert |
|
209 |
* @return a String of two chars 0-1,A-F |
|
210 |
*/ |
|
211 |
public static byte byteOfHex(byte[] hex) { |
|
212 |
byte retByte = 0; |
|
213 |
Byte tmp; |
|
214 |
int val = 0; |
|
215 |
|
|
216 |
// First 4 bits |
|
217 |
tmp = hex[0]; |
|
218 |
val = tmp.intValue(); |
|
219 |
if(val > 64) { |
|
220 |
// Letter |
|
221 |
val -= 55; |
|
222 |
} else { |
|
223 |
// Number |
|
224 |
val -= 48; |
|
225 |
} |
|
226 |
retByte = ((byte) (val << 4)); |
|
227 |
|
|
228 |
// Last 4 bits |
|
229 |
tmp = hex[1]; |
|
230 |
val = tmp.intValue(); |
|
231 |
if(val > 64) { |
|
232 |
// Letter |
|
233 |
val -= 55; |
|
234 |
} else { |
|
235 |
// Number |
|
236 |
val -= 48; |
|
237 |
} |
|
238 |
retByte |= ((byte) val); |
|
239 |
|
|
240 |
return retByte; |
|
241 |
} |
|
242 |
|
|
243 |
|
|
244 |
/** |
|
245 |
* Print the bits of a byte to standard out. For debugging. |
|
246 |
* |
|
247 |
* @param aByte the byte you wish to print out. |
|
248 |
*/ |
|
249 |
public static void printBits(byte aByte) { |
|
250 |
int temp; |
|
251 |
for(int i=7; i>=0; i--) { |
|
252 |
temp = (aByte >>> i); |
|
253 |
temp &= 0x0001; |
|
254 |
System.out.print(""+temp); |
|
255 |
} |
|
256 |
System.out.println(); |
|
257 |
} |
|
258 |
|
|
259 |
public static String bitsOfBytes(byte[] bytes) { |
|
260 |
String str = ""; |
|
261 |
//Expensive, but who cares |
|
262 |
for(int i=0; i<bytes.length; i++ ) { |
|
263 |
str += bitsOfByte(bytes[i]) + " "; |
|
264 |
if((i + 1) % 4 == 0) |
|
265 |
str += "\n"; |
|
266 |
} |
|
267 |
|
|
268 |
return str; |
|
269 |
} |
|
270 |
} |