1 21 package org.apache.derby.impl.drda; 22 23 26 class SignedBinary 27 { 28 private SignedBinary () {} 30 31 34 public final static int BIG_ENDIAN = 1; 35 36 39 public final static int LITTLE_ENDIAN = 2; 40 41 50 public static short getShort (byte[] buffer, int offset, int byteOrder) 51 { 52 switch (byteOrder) { 53 case BIG_ENDIAN: 54 return bigEndianBytesToShort (buffer, offset); 55 case LITTLE_ENDIAN: 56 return littleEndianBytesToShort (buffer, offset); 57 default: 58 throw new java.lang.IllegalArgumentException (); 59 } 60 } 61 62 71 public static int getInt (byte[] buffer, int offset, int byteOrder) 72 { 73 switch (byteOrder) { 74 case BIG_ENDIAN: 75 return bigEndianBytesToInt (buffer, offset); 76 case LITTLE_ENDIAN: 77 return littleEndianBytesToInt (buffer, offset); 78 default: 79 throw new java.lang.IllegalArgumentException (); 80 } 81 } 82 83 93 public static long getLong (byte[] buffer, int offset, int byteOrder) 94 { 95 switch (byteOrder) { 96 case BIG_ENDIAN: 97 return bigEndianBytesToLong (buffer, offset); 98 case LITTLE_ENDIAN: 99 return littleEndianBytesToLong (buffer, offset); 100 default: 101 throw new java.lang.IllegalArgumentException (); 102 } 103 } 104 105 108 public static short bigEndianBytesToShort (byte[] buffer, int offset) 109 { 110 return (short) (((buffer[offset+0] & 0xff) << 8) + 111 ((buffer[offset+1] & 0xff) << 0)); 112 } 113 114 117 public static short littleEndianBytesToShort (byte[] buffer, int offset) 118 { 119 return (short) (((buffer[offset+0] & 0xff) << 0) + 120 ((buffer[offset+1] & 0xff) << 8)); 121 } 122 123 126 public static int bigEndianBytesToInt (byte[] buffer, int offset) 127 { 128 return (int) (((buffer[offset+0] & 0xff) << 24) + 129 ((buffer[offset+1] & 0xff) << 16) + 130 ((buffer[offset+2] & 0xff) << 8) + 131 ((buffer[offset+3] & 0xff) << 0)); 132 } 133 134 137 public static int littleEndianBytesToInt (byte[] buffer, int offset) 138 { 139 return (int) (((buffer[offset+0] & 0xff) << 0) + 140 ((buffer[offset+1] & 0xff) << 8) + 141 ((buffer[offset+2] & 0xff) << 16) + 142 ((buffer[offset+3] & 0xff) << 24)); 143 } 144 145 148 public static long bigEndianBytesToLong (byte[] buffer, int offset) 149 { 150 return (long) (((buffer[offset+0] & 0xffL) << 56) + 151 ((buffer[offset+1] & 0xffL) << 48) + 152 ((buffer[offset+2] & 0xffL) << 40) + 153 ((buffer[offset+3] & 0xffL) << 32) + 154 ((buffer[offset+4] & 0xffL) << 24) + 155 ((buffer[offset+5] & 0xffL) << 16) + 156 ((buffer[offset+6] & 0xffL) << 8) + 157 ((buffer[offset+7] & 0xffL) << 0)); 158 } 159 160 163 public static long littleEndianBytesToLong (byte[] buffer, int offset) 164 { 165 return (long) (((buffer[offset+0] & 0xffL) << 0) + 166 ((buffer[offset+1] & 0xffL) << 8) + 167 ((buffer[offset+2] & 0xffL) << 16) + 168 ((buffer[offset+3] & 0xffL) << 24) + 169 ((buffer[offset+4] & 0xffL) << 32) + 170 ((buffer[offset+5] & 0xffL) << 40) + 171 ((buffer[offset+6] & 0xffL) << 48) + 172 ((buffer[offset+7] & 0xffL) << 56)); 173 } 174 } 175 | Popular Tags |