src/jlibrtp/StaticProcs.java
changeset 834 e8d6255306f8
parent 833 f5a5d9237d69
child 835 4e40f3481f23
equal deleted inserted replaced
833:f5a5d9237d69 834:e8d6255306f8
     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. Primarily used for 16 bit
       
    30 	 * unsigned integers, ignore the first two octets.
       
    31 	 * 
       
    32 	 * @param i
       
    33 	 *            a 16 bit unsigned integer in an int
       
    34 	 * @return byte[2] representing the integer as unsigned, most significant
       
    35 	 *         bit first.
       
    36 	 */
       
    37 	public static byte[] uIntIntToByteWord(int i) {
       
    38 		byte[] byteWord = new byte[2];
       
    39 		byteWord[0] = (byte) ((i >> 8) & 0x000000FF);
       
    40 		byteWord[1] = (byte) (i & 0x00FF);
       
    41 		return byteWord;
       
    42 	}
       
    43 	
       
    44 	public static void uIntIntToByteWord(int i, byte[] byteWord, int srcPos) {
       
    45 		byteWord[0 + srcPos] = (byte) ((i >> 8) & 0x000000FF);
       
    46 		byteWord[1 + srcPos] = (byte) (i & 0x00FF);
       
    47 	}
       
    48 
       
    49 	/**
       
    50 	 * Converts an unsigned 32 bit integer, stored in a long, into an array of
       
    51 	 * bytes.
       
    52 	 * 
       
    53 	 * @param j
       
    54 	 *            a long
       
    55 	 * @return byte[4] representing the unsigned integer, most significant bit
       
    56 	 *         first.
       
    57 	 */
       
    58 	public static byte[] uIntLongToByteWord(long j) {
       
    59 		int i = (int) j;
       
    60 		byte[] byteWord = new byte[4];
       
    61 		byteWord[0] = (byte) ((i >>> 24) & 0x000000FF);
       
    62 		byteWord[1] = (byte) ((i >> 16) & 0x000000FF);
       
    63 		byteWord[2] = (byte) ((i >> 8) & 0x000000FF);
       
    64 		byteWord[3] = (byte) (i & 0x00FF);
       
    65 		return byteWord;
       
    66 	}
       
    67 	
       
    68 	public static void uIntLongToByteWord(long j, byte[] byteword, int srcPos) {
       
    69 		int i = (int) j;
       
    70 		byteword[0 + srcPos] = (byte) ((i >>> 24) & 0x000000FF);
       
    71 		byteword[1 + srcPos] = (byte) ((i >> 16) & 0x000000FF);
       
    72 		byteword[2 + srcPos] = (byte) ((i >> 8) & 0x000000FF);
       
    73 		byteword[3 + srcPos] = (byte) (i & 0x00FF);
       
    74 	}
       
    75 
       
    76 	/**
       
    77 	 * Combines two bytes (most significant bit first) into a 16 bit unsigned
       
    78 	 * integer.
       
    79 	 * 
       
    80 	 * @param index
       
    81 	 *            of most significant byte
       
    82 	 * @return int with the 16 bit unsigned integer
       
    83 	 */
       
    84 	public static int bytesToUIntInt(byte[] bytes, int index) {
       
    85 		int accum = 0;
       
    86 		int i = 1;
       
    87 		for (int shiftBy = 0; shiftBy < 16; shiftBy += 8) {
       
    88 			accum |= ((long) (bytes[index + i] & 0xff)) << shiftBy;
       
    89 			i--;
       
    90 		}
       
    91 		return accum;
       
    92 	}
       
    93 
       
    94 	/**
       
    95 	 * Combines four bytes (most significant bit first) into a 32 bit unsigned
       
    96 	 * integer.
       
    97 	 * 
       
    98 	 * @param bytes
       
    99 	 * @param index
       
   100 	 *            of most significant byte
       
   101 	 * @return long with the 32 bit unsigned integer
       
   102 	 */
       
   103 	public static long bytesToUIntLong(byte[] bytes, int index) {
       
   104 		long accum = 0;
       
   105 		int i = 3;
       
   106 		for (int shiftBy = 0; shiftBy < 32; shiftBy += 8) {
       
   107 			accum |= ((long) (bytes[index + i] & 0xff)) << shiftBy;
       
   108 			i--;
       
   109 		}
       
   110 		return accum;
       
   111 	}
       
   112 
       
   113 	/**
       
   114 	 * Converts an arbitrary number of bytes, assumed to represent an unsigned
       
   115 	 * integer, to a Java long
       
   116 	 */
       
   117 	/*
       
   118 	 * public static long bytesToUintLong(byte[] bytes, int firstByte, int
       
   119 	 * lastByte) { long accum = 0; int i = lastByte - firstByte; if(i > 7) {
       
   120 	 * System.out.println(
       
   121 	 * "!!!! StaticProcs.bytesToUintLong() Can't convert more than 63 bits!");
       
   122 	 * return -1; } int stop = (i+1)*8;
       
   123 	 * 
       
   124 	 * for (int shiftBy = 0; shiftBy < stop; shiftBy += 8 ) { accum |= ( (long)(
       
   125 	 * bytes[firstByte + i] & 0xff ) ) << shiftBy; i--; } return accum; }
       
   126 	 */
       
   127 
       
   128 	/**
       
   129 	 * Converts an arbitrary number of bytes, assumed to represent an unsigned
       
   130 	 * integer, to a Java int
       
   131 	 */
       
   132 	/*
       
   133 	 * public static int bytesToUintInt(byte[] bytes, int firstByte, int
       
   134 	 * lastByte) { int accum = 0; int i = lastByte - firstByte; if(i > 3) {
       
   135 	 * System.out.println(
       
   136 	 * "!!!! StaticProcs.bytesToUintLong() Can't convert more than 31 bits!");
       
   137 	 * return -1; } int stop = (i+1)*8;
       
   138 	 * 
       
   139 	 * for (int shiftBy = 0; shiftBy < stop; shiftBy += 8 ) { accum |= ( (long)(
       
   140 	 * bytes[firstByte + i] & 0xff ) ) << shiftBy; i--; } return accum; }
       
   141 	 */
       
   142 
       
   143 	/**
       
   144 	 * Recreates a UNIX timestamp based on the NTP representation used in RTCP
       
   145 	 * SR packets
       
   146 	 * 
       
   147 	 * @param ntpTs1
       
   148 	 *            from RTCP SR packet
       
   149 	 * @param ntpTs2
       
   150 	 *            from RTCP SR packet
       
   151 	 * @return the UNIX timestamp
       
   152 	 */
       
   153 	public static long undoNtpMess(long ntpTs1, long ntpTs2) {
       
   154 		long timeVal = (ntpTs1 - 2208988800L) * 1000;
       
   155 
       
   156 		double tmp = (1000.0 * (double) ntpTs2) / ((double) 4294967295L);
       
   157 		long ms = (long) tmp;
       
   158 		// System.out.println(" timeVal: " +Long.toString(timeVal)+ " ms " +
       
   159 		// Long.toString(ms));
       
   160 		timeVal += ms;
       
   161 
       
   162 		return timeVal;
       
   163 	}
       
   164 
       
   165 	/**
       
   166 	 * Get the bits of a byte
       
   167 	 * 
       
   168 	 * @param aByte
       
   169 	 *            the byte you wish to convert
       
   170 	 * @return a String of 1's and 0's
       
   171 	 */
       
   172 	public static String bitsOfByte(byte aByte) {
       
   173 		int temp;
       
   174 		String out = "";
       
   175 		for (int i = 7; i >= 0; i--) {
       
   176 			temp = (aByte >>> i);
       
   177 			temp &= 0x0001;
       
   178 			out += ("" + temp);
       
   179 		}
       
   180 		return out;
       
   181 	}
       
   182 
       
   183 	/**
       
   184 	 * Get the hex representation of a byte
       
   185 	 * 
       
   186 	 * @param aByte
       
   187 	 *            the byte you wish to convert
       
   188 	 * @return a String of two chars 0-1,A-F
       
   189 	 */
       
   190 	public static String hexOfByte(byte aByte) {
       
   191 		String out = "";
       
   192 
       
   193 		for (int i = 0; i < 2; i++) {
       
   194 			int temp = (int) aByte;
       
   195 			if (temp < 0) {
       
   196 				temp += 256;
       
   197 			}
       
   198 			if (i == 0) {
       
   199 				temp = temp / 16;
       
   200 			} else {
       
   201 				temp = temp % 16;
       
   202 			}
       
   203 
       
   204 			if (temp > 9) {
       
   205 				switch (temp) {
       
   206 				case 10:
       
   207 					out += "A";
       
   208 					break;
       
   209 				case 11:
       
   210 					out += "B";
       
   211 					break;
       
   212 				case 12:
       
   213 					out += "C";
       
   214 					break;
       
   215 				case 13:
       
   216 					out += "D";
       
   217 					break;
       
   218 				case 14:
       
   219 					out += "E";
       
   220 					break;
       
   221 				case 15:
       
   222 					out += "F";
       
   223 					break;
       
   224 				}
       
   225 			} else {
       
   226 				out += Integer.toString(temp);
       
   227 			}
       
   228 		}
       
   229 		return out;
       
   230 	}
       
   231 
       
   232 	/**
       
   233 	 * Get the hex representation of a byte
       
   234 	 * 
       
   235 	 * @param hex
       
   236 	 *            4 bytes the byte you wish to convert
       
   237 	 * @return a String of two chars 0-1,A-F
       
   238 	 */
       
   239 	public static byte byteOfHex(byte[] hex) {
       
   240 		byte retByte = 0;
       
   241 		Byte tmp;
       
   242 		int val = 0;
       
   243 
       
   244 		// First 4 bits
       
   245 		tmp = hex[0];
       
   246 		val = tmp.intValue();
       
   247 		if (val > 64) {
       
   248 			// Letter
       
   249 			val -= 55;
       
   250 		} else {
       
   251 			// Number
       
   252 			val -= 48;
       
   253 		}
       
   254 		retByte = ((byte) (val << 4));
       
   255 
       
   256 		// Last 4 bits
       
   257 		tmp = hex[1];
       
   258 		val = tmp.intValue();
       
   259 		if (val > 64) {
       
   260 			// Letter
       
   261 			val -= 55;
       
   262 		} else {
       
   263 			// Number
       
   264 			val -= 48;
       
   265 		}
       
   266 		retByte |= ((byte) val);
       
   267 
       
   268 		return retByte;
       
   269 	}
       
   270 
       
   271 	/**
       
   272 	 * Print the bits of a byte to standard out. For debugging.
       
   273 	 * 
       
   274 	 * @param aByte
       
   275 	 *            the byte you wish to print out.
       
   276 	 */
       
   277 	public static void printBits(byte aByte) {
       
   278 		int temp;
       
   279 		for (int i = 7; i >= 0; i--) {
       
   280 			temp = (aByte >>> i);
       
   281 			temp &= 0x0001;
       
   282 			System.out.print("" + temp);
       
   283 		}
       
   284 		System.out.println();
       
   285 	}
       
   286 
       
   287 	public static String bitsOfBytes(byte[] bytes) {
       
   288 		String str = "";
       
   289 		// Expensive, but who cares
       
   290 		for (int i = 0; i < bytes.length; i++) {
       
   291 			str += bitsOfByte(bytes[i]) + " ";
       
   292 			if ((i + 1) % 4 == 0)
       
   293 				str += "\n";
       
   294 		}
       
   295 
       
   296 		return str;
       
   297 	}
       
   298 }