public class ByteUtil {

来源:互联网 发布:linux中的find命令 编辑:程序博客网 时间:2024/06/14 04:30

public class ByteUtil {
    
    public static final int LARGE_BYTES_THRESHOLD = 2048;


    public static final byte[] EMPTY_BYTES = new byte[0];
            
    public static String toHexString(byte b) {
        String s = Integer.toHexString(b);
        s = "0" + s;
        s = s.substring(s.length() - 2, s.length());
        return s;
    }    
    
    public static String toBinaryString(byte b) {
        String s = Integer.toBinaryString(b);
        s = "0000000" + s;
        s = s.substring(s.length() - 8, s.length());
        return s;
    }
    
    public static String toHexString(byte[] bytes, int length) {
        if (bytes == null) {
            return null;
        } else {
            StringBuilder builder = new StringBuilder();
            for (int i = 0; i < length; i++) {
                byte byte0 = bytes[i];
                if (i > 0) {
                    builder.append(" ");
                }
                builder.append(toHexString(byte0));
            }
            return builder.toString();
        }
    }
    
    public static String toHexString(byte[] bytes, int startIndex, int length) {
        if (bytes == null) {
            return null;
        } else {
            StringBuilder builder = new StringBuilder();
            for (int i = 0; i < length; i++) {
                byte byte0 = bytes[startIndex + i];
                if (i > 0) {
                    builder.append(" ");
                }
                builder.append(toHexString(byte0));
            }
            return builder.toString();
        }
    }


    public static String toIntString(byte[] bytes, int startIndex, int length) {
        if (bytes == null) {
            return null;
        } else {
            StringBuilder builder = new StringBuilder();
            for (int i = 0; i < length; i++) {
                byte byte0 = bytes[startIndex + i];
                if (i > 0) {
                    builder.append(" ");
                }
                builder.append(byte0);
            }
            return builder.toString();
        }
    }


    public static byte getBit(byte b, int bitIndex) {
        assert bitIndex >= 0 && bitIndex < 8;
        byte maskByte = (byte) (1 << bitIndex);
        byte out = (byte) (b & maskByte);
        out = (byte) (out >>> bitIndex);
        return out;
    }
    
    public static boolean equals(byte[] bytes0, byte[] bytes1) {
        if (bytes0 == null && bytes1 == null) {
            return true;
        } else if (bytes0 == null || bytes1 == null) {
            return false;
        }
        if (bytes0.length != bytes1.length) {
            return false;
        }
        for (int i = 0; i < bytes0.length; i++) {
            if (bytes0[i] != bytes1[i]) {
                return false;
            }
        }
        return true;
    }
    
    public static byte[] merge(byte[] bytes0, byte[] bytes1) {
        if (bytes0.length == 0) {
            return bytes1;
        } else if (bytes1.length == 0) {
            return bytes0;
        }
        int capacity = bytes0.length + bytes1.length;
        byte[] bytes = new byte[capacity];
        int pos = 0;
        int length = bytes0.length;
        System.arraycopy(bytes0, 0, bytes, pos, length);
        pos += length;
        length = bytes1.length;
        System.arraycopy(bytes1, 0, bytes, pos, length);
        return bytes;
    }
    
    public static final byte toNegative(byte b) {
        assert b >= 0;
        return (byte) (b | N_SIGN_MASK);
    }
    
    public static final byte toNonNegative(byte b) {
        assert b < 0;
        return (byte) (b & P_SIGN_MASK);
    }
    
    public static char[] toChars(byte[] bytes) {
        return ByteToChar.toChars(bytes);
    }
        
    public static final byte N_SIGN_MASK = (byte) 0x80;
    public static final byte P_SIGN_MASK = (byte) 0x7F;
    
    
    
}
0 0
原创粉丝点击