1 package com.quadcap.util; 2 3 40 41 import java.util.Comparator ; 42 43 48 public class OctetComparator implements Comparator { 49 boolean reverse = false; 50 boolean casemap = false; 51 52 static OctetComparator cmp = new OctetComparator(); 53 static OctetComparator casecmp = new OctetComparator(true,false); 54 57 public OctetComparator() {} 58 59 public OctetComparator(boolean casemap, boolean reverse) { 60 this.casemap = casemap; 61 this.reverse = reverse; 62 } 63 64 84 public int compare(Object a, Object b) { 85 OctetString oa = (OctetString)a; 86 OctetString ob = (OctetString)b; 87 88 int before = reverse ? 1 : -1; 89 int after = 0 - before; 90 91 byte[] va = getBytes(oa); 92 byte[] vb = getBytes(ob); 93 int i = 0; 94 while (i < va.length && i < vb.length) { 95 if (va[i] < vb[i]) return before; 96 if (va[i] > vb[i]) return after; 97 i++; 98 } 99 if (i < vb.length) return before; 100 if (i < va.length) return after; 101 return 0; 102 } 103 104 116 public boolean prefixMatch(OctetString obj, OctetString val) { 117 byte[] va = getBytes(val); 118 byte[] vb = getBytes(obj); 119 if (va.length > vb.length) return false; 120 for (int i = 0; i < va.length; i++) { 121 if (va[i] != vb[i]) return false; 122 } 123 return true; 124 } 125 126 138 public boolean substringMatch(OctetString obj, OctetString val) { 139 byte[] va = getBytes(obj); 140 byte[] vb = getBytes(val); 141 int left = va.length - vb.length; 142 for (int i = 0; i <= left; i++) { 143 boolean match = true; 144 for (int j = 0; match && j < vb.length; j++) { 145 match = va[i+j] == vb[j]; 146 } 147 if (match) return true; 148 } 149 return false; 150 } 151 152 153 161 public boolean patternMatch(OctetString obj, OctetString pattern) { 162 byte[] va = getBytes(obj); 163 byte[] vb = getBytes(pattern); 164 return pMatch(va, 0, vb, 0); 165 } 166 167 boolean pMatch(byte[] va, int a, byte[] vb, int b) { 168 while (b < vb.length) { 169 byte p = vb[b++]; 170 switch (p) { 171 case (byte)'*': 172 if (a >= va.length || b >= vb.length) return true; 173 while (a < va.length) { 174 if (pMatch(va, a++, vb, b)) return true; 175 } 176 return false; 177 case (byte)'?': 178 if (a++ >= va.length) return false; 179 break; 180 case (byte)'\\': 181 if (b >= vb.length) return false; 182 p = vb[b++]; 183 default: 185 if (a >= va.length) return false; 186 if (va[a++] != p) return false; 187 } 188 } 189 return a >= va.length; 190 } 191 192 int pMatchOrder(byte[] va, int a, byte[] vb, int b) { 193 int ret = 0; 194 while (b < vb.length) { 195 byte p = vb[b++]; 196 switch (p) { 197 case (byte)'*': 198 while (a < va.length) { 199 ret = pMatchOrder(va, a++, vb, b); 200 if (ret == 0) return ret; 201 } 202 return ret; 203 case (byte)'?': 204 if (a++ >= va.length) return -1; 205 break; 206 case (byte)'\\': 207 if (b >= vb.length) return 1; 208 p = vb[b++]; 209 default: 211 if (a >= va.length) return -1; 212 byte q = va[a++]; 213 if (q < p) return -1; 214 if (q > p) return 1; 215 } 216 } 217 return a >= va.length ? 0 : 1; 218 } 219 220 226 byte[] getBytes(OctetString obj) { 227 byte[] b = obj.getBytes(); 228 if (casemap) { 229 byte[] cb = new byte[b.length]; 230 for (int i = 0; i < b.length; i++) { 231 byte c = b[i]; 232 if (c >= 'a' && c <= 'z') 233 cb[i] = (byte)(c - 32); 234 else 235 cb[i] = c; 236 } 237 b = cb; 238 } 239 return b; 240 } 241 } 242 | Popular Tags |