1 17 package org.alfresco.filesys.smb.dcerpc; 18 19 import org.alfresco.filesys.util.DataPacker; 20 21 24 public class UUID 25 { 26 27 29 public static final int UUID_LENGTH = 36; 30 public static final int UUID_LENGTH_BINARY = 16; 31 private static final String UUID_VALIDCHARS = "0123456789ABCDEFabcdef"; 32 33 35 private String m_uuid; 36 37 39 private int m_ifVersion; 40 41 43 private byte[] m_uuidBytes; 44 45 50 public UUID(String id) 51 { 52 if (validateUUID(id)) 53 { 54 m_uuid = id; 55 m_ifVersion = 1; 56 } 57 } 58 59 65 public UUID(String id, int ver) 66 { 67 if (validateUUID(id)) 68 { 69 m_uuid = id; 70 m_ifVersion = ver; 71 } 72 } 73 74 80 public UUID(byte[] buf, int off) 81 { 82 83 85 if ((off + UUID_LENGTH_BINARY) <= buf.length) 86 { 87 88 90 m_uuidBytes = new byte[UUID_LENGTH_BINARY]; 91 for (int i = 0; i < UUID_LENGTH_BINARY; i++) 92 m_uuidBytes[i] = buf[off + i]; 93 94 96 m_uuid = generateUUIDString(m_uuidBytes); 97 } 98 } 99 100 105 public final boolean isValid() 106 { 107 return m_uuid != null ? true : false; 108 } 109 110 115 public final String getUUID() 116 { 117 return m_uuid; 118 } 119 120 125 public final int getVersion() 126 { 127 return m_ifVersion; 128 } 129 130 135 public final void setVersion(int ver) 136 { 137 m_ifVersion = ver; 138 } 139 140 145 public final byte[] getBytes() 146 { 147 148 150 if (m_uuidBytes == null) 151 { 152 153 155 m_uuidBytes = new byte[UUID_LENGTH_BINARY]; 156 157 try 158 { 159 160 162 String val = m_uuid.substring(0, 8); 163 long lval = Long.parseLong(val, 16); 164 DataPacker.putIntelInt((int) (lval & 0xFFFFFFFF), m_uuidBytes, 0); 165 166 168 val = m_uuid.substring(9, 13); 169 int ival = Integer.parseInt(val, 16); 170 DataPacker.putIntelShort(ival, m_uuidBytes, 4); 171 172 174 val = m_uuid.substring(14, 18); 175 ival = Integer.parseInt(val, 16); 176 DataPacker.putIntelShort(ival, m_uuidBytes, 6); 177 178 180 val = m_uuid.substring(19, 23); 181 ival = Integer.parseInt(val, 16); 182 DataPacker.putShort((short) (ival & 0xFFFF), m_uuidBytes, 8); 183 184 186 int strPos = 24; 187 int bytPos = 10; 188 189 for (int i = 0; i < 6; i++) 190 { 191 val = m_uuid.substring(strPos, strPos + 2); 192 m_uuidBytes[bytPos++] = (byte) (Short.parseShort(val, 16) & 0xFF); 193 strPos += 2; 194 } 195 } 196 catch (NumberFormatException ex) 197 { 198 m_uuidBytes = null; 199 } 200 } 201 202 204 return m_uuidBytes; 205 } 206 207 213 public static final boolean validateUUID(String idStr) 214 { 215 216 218 if (idStr == null || idStr.length() != UUID_LENGTH) 219 return false; 220 221 223 if (idStr.charAt(8) != '-' || idStr.charAt(13) != '-' || idStr.charAt(18) != '-' || idStr.charAt(23) != '-') 224 return false; 225 226 228 int i = 0; 229 for (i = 0; i < 8; i++) 230 if (UUID_VALIDCHARS.indexOf(idStr.charAt(i)) == -1) 231 return false; 232 for (i = 9; i < 13; i++) 233 if (UUID_VALIDCHARS.indexOf(idStr.charAt(i)) == -1) 234 return false; 235 for (i = 14; i < 18; i++) 236 if (UUID_VALIDCHARS.indexOf(idStr.charAt(i)) == -1) 237 return false; 238 for (i = 19; i < 23; i++) 239 if (UUID_VALIDCHARS.indexOf(idStr.charAt(i)) == -1) 240 return false; 241 for (i = 24; i < 36; i++) 242 if (UUID_VALIDCHARS.indexOf(idStr.charAt(i)) == -1) 243 return false; 244 245 247 return true; 248 } 249 250 256 public static final String generateUUIDString(byte[] buf) 257 { 258 259 261 StringBuffer str = new StringBuffer (UUID_LENGTH); 262 263 265 int ival = DataPacker.getIntelInt(buf, 0); 266 str.append(Integer.toHexString(ival)); 267 while (str.length() != 8) 268 str.insert(0, ' '); 269 str.append("-"); 270 271 273 ival = DataPacker.getIntelShort(buf, 4) & 0xFFFF; 274 str.append(Integer.toHexString(ival)); 275 while (str.length() != 13) 276 str.insert(9, '0'); 277 str.append("-"); 278 279 281 ival = DataPacker.getIntelShort(buf, 6) & 0xFFFF; 282 str.append(Integer.toHexString(ival)); 283 while (str.length() != 18) 284 str.insert(14, '0'); 285 str.append("-"); 286 287 289 for (int i = 8; i < UUID_LENGTH_BINARY; i++) 290 { 291 292 294 ival = (int) (buf[i] & 0xFF); 295 if (ival < 16) 296 str.append('0'); 297 str.append(Integer.toHexString(ival)); 298 299 301 if (i == 9) 302 str.append("-"); 303 } 304 305 307 return str.toString(); 308 } 309 310 316 public final boolean compareTo(UUID id) 317 { 318 319 321 if (getVersion() != id.getVersion()) 322 return false; 323 324 326 byte[] thisBytes = getBytes(); 327 byte[] idBytes = id.getBytes(); 328 329 for (int i = 0; i < UUID_LENGTH_BINARY; i++) 330 if (thisBytes[i] != idBytes[i]) 331 return false; 332 return true; 333 } 334 335 343 public final int storeUUID(byte[] buf, int off, boolean writeVer) 344 { 345 346 348 int pos = off; 349 byte[] uuidByts = getBytes(); 350 if (uuidByts == null) 351 return pos; 352 353 355 for (int i = 0; i < UUID_LENGTH_BINARY; i++) 356 buf[pos + i] = uuidByts[i]; 357 pos += UUID_LENGTH_BINARY; 358 359 361 if (writeVer) 362 { 363 DataPacker.putIntelInt(getVersion(), buf, pos); 364 pos += 4; 365 } 366 367 369 return pos; 370 } 371 372 377 public String toString() 378 { 379 StringBuffer str = new StringBuffer (); 380 381 str.append("["); 382 str.append(m_uuid); 383 str.append(":"); 384 str.append(m_ifVersion); 385 str.append("]"); 386 387 return str.toString(); 388 } 389 390 395 407 } 408 | Popular Tags |