001: package org.jacorb.poa.util;
002:
003: /*
004: * JacORB - a free Java ORB
005: *
006: * Copyright (C) 1997-2004 Gerald Brose.
007: *
008: * This library is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU Library General Public
010: * License as published by the Free Software Foundation; either
011: * version 2 of the License, or (at your option) any later version.
012: *
013: * This library is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
016: * Library General Public License for more details.
017: *
018: * You should have received a copy of the GNU Library General Public
019: * License along with this library; if not, write to the Free
020: * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
021: */
022:
023: /**
024: * This class collects some oid related basic routines.
025: *
026: * @author Reimo Tiedemann, FU Berlin
027: * @version 1.05, 21/01/00, RT
028: */
029:
030: public final class IdUtil {
031:
032: public static byte[] concat(byte[] first, byte[] second) {
033: byte[] result = new byte[first.length + second.length];
034: System.arraycopy(first, 0, result, 0, first.length);
035: System
036: .arraycopy(second, 0, result, first.length,
037: second.length);
038: return result;
039:
040: }
041:
042: /**
043: * creates an id as a concatenation of the current time in msec
044: * and random_len random bytes
045: */
046:
047: public static byte[] createId(int random_len) {
048: byte[] time = toId(System.currentTimeMillis());
049: long range = (long) Math.pow(10, random_len) - 1;
050: byte[] random = toId((long) (Math.random() * range));
051: return concat(time, random);
052: }
053:
054: public static boolean equals(byte[] first, byte[] second) {
055: if (first.length != second.length)
056: return false;
057: for (int i = 0; i < first.length; i++) {
058: if (first[i] != second[i])
059: return false;
060: }
061: return true;
062: }
063:
064: /**
065: * compares first len bytes of two byte arrays
066: */
067:
068: public static boolean equals(byte[] first, byte[] second, int len) {
069: if (first.length < len || second.length < len)
070: return false;
071: for (int i = 0; i < len; i++) {
072: if (first[i] != second[i])
073: return false;
074: }
075: return true;
076: }
077:
078: /**
079: * extracts len bytes from id, the first byte to be copied is at index offset
080: */
081:
082: public static byte[] extract(byte[] id, int offset, int len) {
083: byte[] result = new byte[len];
084: System.arraycopy(id, offset, result, 0, len);
085: return result;
086: }
087:
088: /**
089: * converts the number l into a byte array
090: */
091:
092: public static byte[] toId(long l) {
093: /*
094: String str = "" + l;
095: return str.getBytes();
096:
097: String str = "" + l;
098: */
099: String str = Long.toOctalString(l);
100: if (str.length() % 2 != 0)
101: str = "0" + str;
102: byte[] result = new byte[str.length() / 2];
103: StringBuffer number = new StringBuffer("xx");
104: for (int i = 0; i < result.length; i++) {
105: number.setCharAt(0, str.charAt(i * 2));
106: number.setCharAt(1, str.charAt(i * 2 + 1));
107: result[i] = new Integer(number.toString()).byteValue();
108: }
109: for (int i = 0; i < result.length; i++) {
110: if (result[i] == org.jacorb.poa.POAConstants.OBJECT_KEY_SEP_BYTE) {
111: result[i] = (byte) 48; // char 0 , hex 30
112:
113: } else if (result[i] == org.jacorb.poa.POAConstants.MASK_BYTE) {
114: result[i] = (byte) 19; // char !!, hex 13
115: }
116: }
117: return result;
118: }
119: }
|