一、前言
关于字节工具类ByteUtils,实现常用基本数据类型长度(sizeOfBool/sizeOfShort..sizeOfString)、基本数据类型读写(readBool/readByte..readString)数据操作,详情参见代码示例。
二、代码示例
public class ByteUtils {@b@@b@ /**@b@ * size@b@ */@b@@b@ public static int sizeOfBool(boolean input) {@b@ return 1;@b@ }@b@@b@ public static int sizeOfShort(int input) {@b@ return 2;@b@ }@b@ @b@ public static int sizeOfInt(int input) {@b@ return 4;@b@ }@b@@b@ public static int sizeOfLong(long input) {@b@ return 8;@b@ }@b@@b@ public static int sizeOfFloat(float input) {@b@ return 4;@b@ }@b@@b@ public static int sizeOfDouble(double input) {@b@ return 8;@b@ }@b@@b@ public static int sizeOfString(String input) {@b@ return 4 + input.getBytes().length;@b@ }@b@@b@ /**@b@ * write@b@ */@b@@b@ public static int writeBool(byte[] output, int offset, boolean input) {@b@ output[offset] = (byte) (input ? 0 : 1);@b@ return 1;@b@ }@b@@b@ public static int writeByte(byte[] output, int offset, byte input) {@b@ output[offset] = input;@b@ return 1;@b@ }@b@ @b@ public static int writeShort(byte[] output, int offset, int input) {@b@ output[offset ] = (byte)((input ) & 0xff);@b@ output[offset+1] = (byte)((input >> 8) & 0xff);@b@ return 2;@b@ }@b@@b@ public static int writeInt(byte[] output, int offset, int input) {@b@ output[offset ] = (byte)((input ) & 0xff);@b@ output[offset+1] = (byte)((input >> 8) & 0xff);@b@ output[offset+2] = (byte)((input >> 16) & 0xff);@b@ output[offset+3] = (byte)((input >> 24) & 0xff);@b@ return 4;@b@ }@b@@b@ public static int writeLong(byte[] output, int offset, long input) {@b@ output[offset ] = (byte)((input ) & 0xff);@b@ output[offset+1] = (byte)((input >> 8) & 0xff);@b@ output[offset+2] = (byte)((input >> 16) & 0xff);@b@ output[offset+3] = (byte)((input >> 24) & 0xff);@b@ output[offset+4] = (byte)((input >> 32) & 0xff);@b@ output[offset+5] = (byte)((input >> 40) & 0xff);@b@ output[offset+6] = (byte)((input >> 48) & 0xff);@b@ output[offset+7] = (byte)((input >> 56) & 0xff);@b@ return 8;@b@ }@b@@b@ public static int writeFloat(byte[] output, int offset, float input) {@b@ return writeInt(output, offset, Float.floatToRawIntBits(input));@b@ }@b@@b@ public static int writeDouble(byte[] output, int offset, double input) {@b@ return writeLong(output, offset, Double.doubleToRawLongBits(input));@b@ }@b@@b@ public static int writeString(byte[] output, int offset, String input) {@b@ writeInt(output, offset, input.length());@b@ offset += 4;@b@ byte[] value = input.getBytes();@b@ for (int i = 0; i < value.length; i ++) {@b@ output[offset+i] = value[i];@b@ }@b@ return 4 + value.length;@b@ }@b@ @b@ /**@b@ * read@b@ */@b@@b@ public static boolean readBool(byte[] input, int offset) {@b@ return input[offset] != 0;@b@ }@b@@b@ public static byte readByte(byte[] input, int offset) {@b@ return input[offset];@b@ }@b@ @b@ public static int readShort(byte[] input, int offset) {@b@ return (((int)input[offset ] & 0xff) ) |@b@ (((int)input[offset + 1] & 0xff) << 8);@b@ }@b@@b@ public static int readInt(byte[] input, int offset) {@b@ return (((int)input[offset ] & 0xff) ) |@b@ (((int)input[offset + 1] & 0xff) << 8) |@b@ (((int)input[offset + 2] & 0xff) << 16) |@b@ (((int)input[offset + 3] & 0xff) << 24) ;@b@ }@b@@b@ public static long readLong(byte[] input, int offset) {@b@ return (((long)input[offset] & 0xff) ) |@b@ (((long)input[offset + 1] & 0xff) << 8) |@b@ (((long)input[offset + 2] & 0xff) << 16) |@b@ (((long)input[offset + 3] & 0xff) << 24) |@b@ (((long)input[offset + 4] & 0xff) << 32) |@b@ (((long)input[offset + 5] & 0xff) << 40) |@b@ (((long)input[offset + 6] & 0xff) << 48) |@b@ (((long)input[offset + 7] & 0xff) << 56) ;@b@ }@b@@b@ public static float readFloat(byte[] input, int offset) {@b@ return Float.intBitsToFloat(readInt(input, offset));@b@ }@b@@b@ public static double readDouble(byte[] input, int offset) {@b@ return Double.longBitsToDouble(readLong(input, offset));@b@ }@b@@b@ public static String readString(byte[] input, int offset) {@b@ int length = readInt(input, offset);@b@ return new String(input, offset + 4, length);@b@ }@b@}