package org.opencloudb.util; import java.nio.charset.Charset; public class ByteUtil { /** * compare to 'number' array, for number :123456 ,store to array * [1,2,3,4,5,6] * * @param b1 * @param b2 * @return -1 means b1 < b2, or 0 means b1=b2 else return 1 */ public static int compareNumberArray(byte[] b1, byte[] b2) { if (b1.length > b2.length) { return 1; } else if (b1.length < b2.length) { return -1; } else { for (int i = 0; i < b1.length; i++) { if (b1[i] > b2[i]) { return 1; } else if (b1[i] < b2[i]) { return -1; } } } return 0; } public static byte[] compareNumberArray2(byte[] b1, byte[] b2, int order) { if (b1.length <= 0 && b2.length > 0) { return b2; } if (b1.length > 0 && b2.length <= 0) { return b1; } int len = b1.length > b2.length ? b1.length : b2.length; for (int i = 0; i < len; i++) { if (b1[i] != b2[i]) if (order == 1) return ((b1[i] & 0xff) - (b2[i] & 0xff)) > 0 ? b1 : b2; else return ((b1[i] & 0xff) - (b2[i] & 0xff)) > 0 ? b2 : b1; } return b1; } public static byte[] getBytes(short data) { byte[] bytes = new byte[2]; bytes[0] = (byte) (data & 0xff); bytes[1] = (byte) ((data & 0xff00) >> 8); return bytes; } public static byte[] getBytes(char data) { byte[] bytes = new byte[2]; bytes[0] = (byte) (data); bytes[1] = (byte) (data >> 8); return bytes; } public static byte[] getBytes(int data) { byte[] bytes = new byte[4]; bytes[0] = (byte) (data & 0xff); bytes[1] = (byte) ((data & 0xff00) >> 8); bytes[2] = (byte) ((data & 0xff0000) >> 16); bytes[3] = (byte) ((data & 0xff000000) >> 24); return bytes; } public static byte[] getBytes(long data) { byte[] bytes = new byte[8]; bytes[0] = (byte) (data & 0xff); bytes[1] = (byte) ((data >> 8) & 0xff); bytes[2] = (byte) ((data >> 16) & 0xff); bytes[3] = (byte) ((data >> 24) & 0xff); bytes[4] = (byte) ((data >> 32) & 0xff); bytes[5] = (byte) ((data >> 40) & 0xff); bytes[6] = (byte) ((data >> 48) & 0xff); bytes[7] = (byte) ((data >> 56) & 0xff); return bytes; } public static byte[] getBytes(float data) { int intBits = Float.floatToIntBits(data); return getBytes(intBits); } public static byte[] getBytes(double data) { long intBits = Double.doubleToLongBits(data); return getBytes(intBits); } public static byte[] getBytes(String data, String charsetName) { Charset charset = Charset.forName(charsetName); return data.getBytes(charset); } public static byte[] getBytes(String data) { return getBytes(data, "GBK"); } public static short getShort(byte[] bytes) { return (short) ((0xff & bytes[0]) | (0xff00 & (bytes[1] << 8))); } public static char getChar(byte[] bytes) { return (char) ((0xff & bytes[0]) | (0xff00 & (bytes[1] << 8))); } public static int getInt(byte[] bytes) { return Integer.parseInt(new String(bytes)); // return (0xff & bytes[0]) | (0xff00 & (bytes[1] << 8)) | (0xff0000 & // (bytes[2] << 16)) | (0xff000000 & (bytes[3] << 24)); } public static long getLong(byte[] bytes) { return Long.parseLong(new String(bytes)); // return(0xffL & (long)bytes[0]) | (0xff00L & ((long)bytes[1] << 8)) | // (0xff0000L & ((long)bytes[2] << 16)) | (0xff000000L & ((long)bytes[3] // << 24)) // | (0xff00000000L & ((long)bytes[4] << 32)) | (0xff0000000000L & // ((long)bytes[5] << 40)) | (0xff000000000000L & ((long)bytes[6] << // 48)) | (0xff00000000000000L & ((long)bytes[7] << 56)); } public static float getFloat(byte[] bytes) { return Float.intBitsToFloat(getInt(bytes)); } public static double getDouble(byte[] bytes) { long l = getLong(bytes); return Double.longBitsToDouble(l); } public static String getString(byte[] bytes, String charsetName) { return new String(bytes, Charset.forName(charsetName)); } public static String getString(byte[] bytes) { return getString(bytes, "GBK"); } public static String getDate(byte[] bytes) { return new String(bytes); } public static String getTimestmap(byte[] bytes) { return new String(bytes); } }