Go to the documentation of this file.00001 package com.github.rosjava.android_remocons.common_tools.system;
00002
00003 import java.security.InvalidParameterException;
00004
00005 public class Util {
00006
00007
00008 private static final byte[] HEX_CHAR_TABLE = { (byte) '0', (byte) '1',
00009 (byte) '2', (byte) '3', (byte) '4', (byte) '5', (byte) '6',
00010 (byte) '7', (byte) '8', (byte) '9', (byte) 'A', (byte) 'B',
00011 (byte) 'C', (byte) 'D', (byte) 'E', (byte) 'F' };
00012
00013 public Util() {
00014
00015 }
00016
00017 public static String getHexString(byte[] raw, int len) {
00018 byte[] hex = new byte[3 * len];
00019 int index = 0;
00020 int pos = 0;
00021
00022 for (byte b : raw) {
00023 if (pos >= len)
00024 break;
00025
00026 pos++;
00027 int v = b & 0xFF;
00028 hex[index++] = HEX_CHAR_TABLE[v >>> 4];
00029 hex[index++] = HEX_CHAR_TABLE[v & 0xF];
00030 hex[index++] = ' ';
00031 }
00032
00033 return new String(hex);
00034 }
00035
00036 public static byte[] concat(byte[]... arrays) {
00037 int length = 0;
00038 for (byte[] array : arrays) {
00039 length += array.length;
00040 }
00041
00042 byte[] result = new byte[length];
00043 int pos = 0;
00044 for (byte[] array : arrays) {
00045 System.arraycopy(array, 0, result, pos, array.length);
00046 pos += array.length;
00047 }
00048
00049 return result;
00050 }
00051
00052 public static String toString(byte[] input, int offset, int count) {
00053 if ((offset + count) > input.length)
00054 throw new ArrayIndexOutOfBoundsException("Requested chunk exceeds byte array limits");
00055
00056 byte[] result = new byte[count];
00057 for (int i = 0; i < count; i++)
00058 result[i] = input[offset + i];
00059
00060 return new String(result);
00061 }
00062
00063 public static short toShort(byte[] input, int offset) {
00064 if ((offset + 2) > input.length)
00065 throw new ArrayIndexOutOfBoundsException("Requested chunk exceeds byte array limits");
00066
00067 return (short) (input[offset + 1] & 0xFF |
00068 (input[offset + 0] & 0xFF) << 8);
00069 }
00070
00071
00072 public static int toInteger(byte[] input, int offset) {
00073 if ((offset + 4) > input.length)
00074 throw new ArrayIndexOutOfBoundsException("Requested chunk exceeds byte array limits");
00075
00076 return input[offset + 3] & 0xFF |
00077 (input[offset + 2] & 0xFF) << 8 |
00078 (input[offset + 1] & 0xFF) << 16 |
00079 (input[offset + 0] & 0xFF) << 24;
00080 }
00081
00082 public static byte[] toFixSizeBytes(String input, int length, byte padding) {
00083 if (input.length() > length)
00084 throw new InvalidParameterException(length + "exceeds limit in "
00085 + (input.length() - length) + " chars");
00086
00087 byte[] result = new byte[length];
00088 byte[] source = input.getBytes();
00089 for (int i = 0; i < length; i++)
00090 result[i] = i < source.length ? source[i] : padding;
00091
00092 return result;
00093 }
00094
00095 public static byte[] toBytes(int input)
00096 {
00097 byte[] result = new byte[4];
00098 result[0] = (byte) (input >> 24);
00099 result[1] = (byte) (input >> 16);
00100 result[2] = (byte) (input >> 8);
00101 result[3] = (byte) (input);
00102
00103 return result;
00104 }
00105
00106 public static byte[] toBytes(short input)
00107 {
00108 byte[] result = new byte[2];
00109 result[0] = (byte) (input >> 8);
00110 result[1] = (byte) (input);
00111
00112 return result;
00113 }
00114 }