1 19 package gcc.util; 20 21 public abstract class BigEndian 22 { 23 public static byte[] getShortBytes(short value) 24 { 25 byte[] bytes = new byte[2]; 26 setShort(bytes, value); 27 return bytes; 28 } 29 30 public static byte[] getIntBytes(int value) 31 { 32 byte[] bytes = new byte[4]; 33 setInt(bytes, value); 34 return bytes; 35 } 36 37 public static byte[] getLongBytes(long value) 38 { 39 byte[] bytes = new byte[8]; 40 setLong(bytes, value); 41 return bytes; 42 } 43 44 public static byte[] get48BitLongBytes(long value) 45 { 46 byte[] bytes = new byte[6]; 47 set48BitLong(bytes, value); 48 return bytes; 49 } 50 51 public static short getShort(byte[] bytes) 52 { 53 return getShort(bytes, 0); 54 } 55 56 public static short getShort(byte[] bytes, int offset) 57 { 58 int b1 = (bytes[offset] << 8) & 0x0000ff00; 59 int b0 = bytes[offset + 1] & 0x000000ff; 60 return (short)(b1 | b0); 61 } 62 63 public static int getInt(byte[] bytes) 64 { 65 return getInt(bytes, 0); 66 } 67 68 public static int getInt(byte[] bytes, int offset) 69 { 70 int b3 = (bytes[offset] << 24) & 0xff000000; 71 int b2 = (bytes[offset + 1] << 16) & 0x00ff0000; 72 int b1 = (bytes[offset + 2] << 8) & 0x0000ff00; 73 int b0 = bytes[offset + 3] & 0x000000ff; 74 return b3 | b2 | b1 | b0; 75 } 76 77 public static long getLong(byte[] bytes) 78 { 79 return getLong(bytes, 0); 80 } 81 82 public static long getLong(byte[] bytes, int offset) 83 { 84 long hi = getInt(bytes, offset) & 0xffffffffL; 85 long lo = getInt(bytes, offset + 4) & 0xffffffffL; 86 return (hi << 32) | lo; 87 } 88 89 public static long get48BitLong(byte[] bytes) 90 { 91 return get48BitLong(bytes, 0); 92 } 93 94 public static long get48BitLong(byte[] bytes, int offset) 95 { 96 long hi = getShort(bytes, offset) & 0xffffL; 97 long lo = getInt(bytes, offset + 2) & 0xffffffffL; 98 return (hi << 32) | lo; 99 } 100 101 public static void setShort(byte[] bytes, short value) 102 { 103 setShort(bytes, 0, value); 104 } 105 106 public static void setShort(byte[] bytes, int offset, short value) 107 { 108 bytes[offset] = (byte)((value >>> 8) & 0xff); 109 bytes[offset + 1] = (byte)(value & 0xff); 110 } 111 112 public static void setInt(byte[] bytes, int value) 113 { 114 setInt(bytes, 0, value); 115 } 116 117 public static void setInt(byte[] bytes, int offset, int value) 118 { 119 bytes[offset] = (byte)((value >>> 24) & 0xff); 120 bytes[offset + 1] = (byte)((value >>> 16) & 0xff); 121 bytes[offset + 2] = (byte)((value >>> 8) & 0xff); 122 bytes[offset + 3] = (byte)(value & 0xff); 123 } 124 125 public static void setLong(byte[] bytes, long value) 126 { 127 setLong(bytes, 0, value); 128 } 129 130 public static void setLong(byte[] bytes, int offset, long value) 131 { 132 int hi = (int)(value >>> 32); 133 int lo = (int)value; 134 setInt(bytes, offset, hi); 135 setInt(bytes, offset + 4, lo); 136 } 137 138 public static void set48BitLong(byte[] bytes, long value) 139 { 140 set48BitLong(bytes, 0, value); 141 } 142 143 public static void set48BitLong(byte[] bytes, int offset, long value) 144 { 145 int hi = (int)(value >>> 32); 146 int lo = (int)value; 147 setShort(bytes, offset, (short)hi); 148 setInt(bytes, offset + 2, lo); 149 } 150 } 151 | Popular Tags |