1 package org.jacorb.poa.util; 2 3 22 23 29 30 public final class IdUtil 31 { 32 33 public static byte[] concat(byte[] first, byte[] second) 34 { 35 byte[] result = new byte[first.length+second.length]; 36 System.arraycopy(first, 0, result, 0, first.length); 37 System.arraycopy(second, 0, result, first.length, second.length); 38 return result; 39 40 } 41 45 46 public static byte[] createId(int random_len) 47 { 48 byte[] time = toId(System.currentTimeMillis()); 49 long range = (long) Math.pow(10, random_len)-1; 50 byte[] random = toId((long)(Math.random()*range)); 51 return concat(time, random); 52 } 53 54 public static boolean equals(byte[] first, byte[] second) { 55 if (first.length != second.length) return false; 56 for (int i=0; i<first.length; i++) { 57 if (first[i] != second[i]) return false; 58 } 59 return true; 60 } 61 62 65 66 public static boolean equals(byte[] first, byte[] second, int len) 67 { 68 if (first.length < len || second.length < len) return false; 69 for (int i=0; i<len; i++) 70 { 71 if (first[i] != second[i]) return false; 72 } 73 return true; 74 } 75 76 79 80 public static byte[] extract(byte[] id, int offset, int len) 81 { 82 byte[] result = new byte[len]; 83 System.arraycopy(id, offset, result, 0, len); 84 return result; 85 } 86 87 90 91 public static byte[] toId(long l) 92 { 93 99 String str = Long.toOctalString(l); 100 if (str.length() % 2 != 0) str = "0" + str; 101 byte[] result = new byte[str.length()/2]; 102 StringBuffer number = new StringBuffer ("xx"); 103 for (int i=0; i<result.length; i++) { 104 number.setCharAt(0, str.charAt(i*2)); 105 number.setCharAt(1, str.charAt(i*2+1)); 106 result[i] = new Integer (number.toString()).byteValue(); 107 } 108 for (int i=0; i<result.length; i++) { 109 if (result[i] == org.jacorb.poa.POAConstants.OBJECT_KEY_SEP_BYTE) { 110 result[i] = (byte) 48; 112 } else if (result[i] == org.jacorb.poa.POAConstants.MASK_BYTE) { 113 result[i] = (byte) 19; } 115 } 116 return result; 117 } 118 } 119 120 121 122 123 124 125 126 127 128 129 130 131 | Popular Tags |