一、前言
关于apache的bsf源码包中的org.apache.bsf.util.event.generator.ByteUtility字节数组工具类,对字节数组byte[]追加内容addBytes、字节数组区间范围转换double/float/int/long/short类型byteArrayToDouble/byteArrayToFloat/byteArrayToInt/byteArrayToLong/byteArrayToShort等,详情见源码说明。
二、源码说明
package org.apache.bsf.util.event.generator;@b@@b@public class ByteUtility@b@{@b@ public static byte[] addBytes(byte[] array, byte[] value)@b@ {@b@ if (null != array)@b@ {@b@ byte[] newarray = new byte[array.length + value.length];@b@ System.arraycopy(array, 0, newarray, 0, array.length);@b@ System.arraycopy(value, 0, newarray, array.length, value.length);@b@ array = newarray;@b@ }@b@ else@b@ {@b@ array = value;@b@ }@b@ return array;@b@ }@b@@b@ public static byte[] addBytes(byte[] array, byte value) {@b@ if (null != array)@b@ {@b@ byte[] newarray = new byte[array.length + 1];@b@ System.arraycopy(array, 0, newarray, 0, array.length);@b@ newarray[(newarray.length - 1)] = value;@b@ array = newarray;@b@ }@b@ else@b@ {@b@ array = new byte[1];@b@ array[0] = value;@b@ }@b@ return array;@b@ }@b@@b@ public static byte[] addBytes(byte[] array, int value) {@b@ if (null != array)@b@ {@b@ byte[] newarray = new byte[array.length + 3];@b@ System.arraycopy(array, 0, newarray, 0, array.length);@b@ newarray[(newarray.length - 3)] = (byte)(value >> 16 & 0xFF);@b@ newarray[(newarray.length - 2)] = (byte)(value >> 8 & 0xFF);@b@ newarray[(newarray.length - 1)] = (byte)(value & 0xFF);@b@ array = newarray;@b@ }@b@ else@b@ {@b@ array = new byte[3];@b@ array[0] = (byte)(value >> 16 & 0xFF);@b@ array[1] = (byte)(value >> 8 & 0xFF);@b@ array[2] = (byte)(value & 0xFF);@b@ }@b@ return array;@b@ }@b@@b@ public static byte[] addBytes(byte[] array, long value) {@b@ if (null != array)@b@ {@b@ byte[] newarray = new byte[array.length + 4];@b@ System.arraycopy(array, 0, newarray, 0, array.length);@b@ newarray[(newarray.length - 4)] = (byte)(int)(value >> 24 & 0xFF);@b@ newarray[(newarray.length - 3)] = (byte)(int)(value >> 16 & 0xFF);@b@ newarray[(newarray.length - 2)] = (byte)(int)(value >> 8 & 0xFF);@b@ newarray[(newarray.length - 1)] = (byte)(int)(value & 0xFF);@b@ array = newarray;@b@ }@b@ else@b@ {@b@ array = new byte[4];@b@ array[0] = (byte)(int)(value >> 24 & 0xFF);@b@ array[1] = (byte)(int)(value >> 16 & 0xFF);@b@ array[2] = (byte)(int)(value >> 8 & 0xFF);@b@ array[3] = (byte)(int)(value & 0xFF);@b@ }@b@ return array;@b@ }@b@@b@ public static byte[] addBytes(byte[] array, String value) {@b@ if (null != value)@b@ {@b@ if (null != array)@b@ {@b@ byte[] newarray = new byte[array.length + value.length()];@b@ System.arraycopy(array, 0, newarray, 0, array.length);@b@ System.arraycopy(value.getBytes(), 0, newarray, array.length, value.length());@b@ array = newarray;@b@ }@b@ else@b@ {@b@ array = value.getBytes();@b@ }@b@ }@b@ return array;@b@ }@b@@b@ public static byte[] addBytes(byte[] array, short value) {@b@ if (null != array)@b@ {@b@ byte[] newarray = new byte[array.length + 2];@b@ System.arraycopy(array, 0, newarray, 0, array.length);@b@ newarray[(newarray.length - 2)] = (byte)(value >> 8 & 0xFF);@b@ newarray[(newarray.length - 1)] = (byte)(value & 0xFF);@b@ array = newarray;@b@ }@b@ else@b@ {@b@ array = new byte[2];@b@ array[0] = (byte)(value >> 8 & 0xFF);@b@ array[1] = (byte)(value & 0xFF);@b@ }@b@ return array;@b@ }@b@@b@ public static double byteArrayToDouble(byte[] high, byte[] low) {@b@ double temp = 0.0D;@b@@b@ temp += ((high[0] & 0xFF) << 56);@b@ temp += ((high[1] & 0xFF) << 48);@b@ temp += ((high[2] & 0xFF) << 40);@b@ temp += ((high[3] & 0xFF) << 32);@b@@b@ temp += ((low[0] & 0xFF) << 24);@b@ temp += ((low[1] & 0xFF) << 16);@b@ temp += ((low[2] & 0xFF) << 8);@b@ temp += (low[3] & 0xFF);@b@ return temp;@b@ }@b@@b@ public static double byteArrayToDouble(byte[] value) {@b@ byte[] high = new byte[4];@b@ byte[] low = new byte[4];@b@ high[0] = value[0];@b@ high[1] = value[1];@b@ high[2] = value[2];@b@ high[3] = value[3];@b@ low[0] = value[4];@b@ low[1] = value[5];@b@ low[2] = value[6];@b@ low[3] = value[7];@b@ return byteArrayToDouble(high, low);@b@ }@b@@b@ public static float byteArrayToFloat(byte[] value) {@b@ float temp = 0.0F;@b@ temp += ((value[0] & 0xFF) << 24);@b@ temp += ((value[1] & 0xFF) << 16);@b@ temp += ((value[2] & 0xFF) << 8);@b@ temp += (value[3] & 0xFF);@b@ return temp;@b@ }@b@@b@ public static int byteArrayToInt(byte[] value) {@b@ int temp = 0;@b@ temp += ((value[0] & 0xFF) << 24);@b@ temp += ((value[1] & 0xFF) << 16);@b@ temp += ((value[2] & 0xFF) << 8);@b@ temp += (value[3] & 0xFF);@b@ return temp;@b@ }@b@@b@ public static long byteArrayToLong(byte[] value) {@b@ byte[] high = new byte[4];@b@ byte[] low = new byte[4];@b@ high[0] = value[0];@b@ high[1] = value[1];@b@ high[2] = value[2];@b@ high[3] = value[3];@b@ low[0] = value[4];@b@ low[1] = value[5];@b@ low[2] = value[6];@b@ low[3] = value[7];@b@ return byteArrayToLong(high, low);@b@ }@b@@b@ public static long byteArrayToLong(byte[] high, byte[] low) {@b@ long temp = 0L;@b@@b@ temp += ((high[0] & 0xFF) << 56);@b@ temp += ((high[1] & 0xFF) << 48);@b@ temp += ((high[2] & 0xFF) << 40);@b@ temp += ((high[3] & 0xFF) << 32);@b@@b@ temp += ((low[0] & 0xFF) << 24);@b@ temp += ((low[1] & 0xFF) << 16);@b@ temp += ((low[2] & 0xFF) << 8);@b@ temp += (low[3] & 0xFF);@b@ return temp;@b@ }@b@@b@ public static short byteArrayToShort(byte[] value)@b@ {@b@ short temp = 0;@b@ temp = (short)(temp + ((value[0] & 0xFF) << 8));@b@ temp = (short)(temp + (value[1] & 0xFF));@b@ return temp;@b@ }@b@@b@ public static String byteToHexString(byte value) {@b@ String temp = null;@b@@b@ switch ((value & 0xF0) >> 4)@b@ {@b@ case 0:@b@ temp = "0";@b@ break;@b@ case 1:@b@ temp = "1";@b@ break;@b@ case 2:@b@ temp = "2";@b@ break;@b@ case 3:@b@ temp = "3";@b@ break;@b@ case 4:@b@ temp = "4";@b@ break;@b@ case 5:@b@ temp = "5";@b@ break;@b@ case 6:@b@ temp = "6";@b@ break;@b@ case 7:@b@ temp = "7";@b@ break;@b@ case 8:@b@ temp = "8";@b@ break;@b@ case 9:@b@ temp = "9";@b@ break;@b@ case 10:@b@ temp = "A";@b@ break;@b@ case 11:@b@ temp = "B";@b@ break;@b@ case 12:@b@ temp = "C";@b@ break;@b@ case 13:@b@ temp = "D";@b@ break;@b@ case 14:@b@ temp = "E";@b@ break;@b@ case 15:@b@ temp = "F";@b@ }@b@@b@ switch (value & 0xF)@b@ {@b@ case 0:@b@ temp = temp + "0";@b@ break;@b@ case 1:@b@ temp = temp + "1";@b@ break;@b@ case 2:@b@ temp = temp + "2";@b@ break;@b@ case 3:@b@ temp = temp + "3";@b@ break;@b@ case 4:@b@ temp = temp + "4";@b@ break;@b@ case 5:@b@ temp = temp + "5";@b@ break;@b@ case 6:@b@ temp = temp + "6";@b@ break;@b@ case 7:@b@ temp = temp + "7";@b@ break;@b@ case 8:@b@ temp = temp + "8";@b@ break;@b@ case 9:@b@ temp = temp + "9";@b@ break;@b@ case 10:@b@ temp = temp + "A";@b@ break;@b@ case 11:@b@ temp = temp + "B";@b@ break;@b@ case 12:@b@ temp = temp + "C";@b@ break;@b@ case 13:@b@ temp = temp + "D";@b@ break;@b@ case 14:@b@ temp = temp + "E";@b@ break;@b@ case 15:@b@ temp = temp + "F";@b@ }@b@@b@ return temp;@b@ }@b@}