📄 oid.java
字号:
/**
* Sets the sub-identifier at the specified position.
* @param index
* a zero-based index into the <code>OID</code>.
* @param value
* a 32bit unsigned integer value.
* @throws ArrayIndexOutOfBoundsException
* if the index is out of range (index < 0 || index >= size()).
*/
public final void set(int index, int value) {
this.value[index] = value;
}
/**
* Appends a dotted String OID to this <code>OID</code>.
* @param oid
* a dotted String with numerical sub-identifiers.
*/
public final void append(String oid) {
OID suffix = new OID(oid);
append(suffix);
}
/**
* Appends an <code>OID</code> to this OID.
* @param oid
* an <code>OID</code> instance.
*/
public final void append(OID oid) {
int[] newValue = new int[value.length+oid.value.length];
System.arraycopy(value, 0, newValue, 0, value.length);
System.arraycopy(oid.value, 0, newValue, value.length, oid.value.length);
value = newValue;
}
/**
* Appends a sub-identifier to this OID.
* @param subID
* an integer value.
*/
public final void append(int subID) {
int[] newValue = new int[value.length+1];
System.arraycopy(value, 0, newValue, 0, value.length);
newValue[value.length] = subID;
value = newValue;
}
/**
* Appends an unsigned long sub-identifier value to this OID.
* @param subID
* an unsigned long value less or equal to 2^32-1.
* @since 1.2
*/
public final void appendUnsigned(long subID) {
append((int)(subID & 0xFFFFFFFFL));
}
/**
* Checks whether this <code>OID</code> can be BER encoded.
* @return
* <code>true</code> if size() >= 2 and size() <= 128 and if the first
* two sub-identifiers are less than 3 and 40 respectively.
*/
public boolean isValid() {
return ((size() >= 2) && (size() <= 128) &&
((value[0] & 0xFFFFFFFFL) <= 2l) &&
((value[1] & 0xFFFFFFFFL) < 40l));
}
/**
* Returns the number of sub-identifiers in this <code>OID</code>.
* @return
* an integer value between 0 and 128.
*/
public final int size() {
return value.length;
}
/**
* Compares the n leftmost sub-identifiers with the given <code>OID</code>
* in left-to-right direction.
* @param n
* the number of sub-identifiers to compare.
* @param other
* an <code>OID</code> to compare with this <code>OID</code>.
* @return
* <UL>
* <LI>0 if the first <code>n</code> sub-identifiers are the same.
* <LI><0 if the first <code>n</code> sub-identifiers of this
* <code>OID</code> are lexicographic less than those of the comparand.
* <LI>>0 if the first <code>n</code> sub-identifiers of this
* <code>OID</code> are lexicographic greater than those of the comparand.
* </UL>
*/
public int leftMostCompare(int n, OID other) {
for (int i=0; i<n; i++) {
if (value[i] == other.value[i]) {
continue;
}
else if ((value[i] & 0xFFFFFFFFL) <
(other.value[i] & 0xFFFFFFFFL)) {
return -1;
}
else {
return 1;
}
}
return 0;
}
/**
* Compares the n rightmost sub-identifiers in direction right-to-left
* with those of the given <code>OID</code>.
* @param n
* the number of sub-identifiers to compare.
* @param other
* an <code>OID</code> to compare with this <code>OID</code>.
* @return
* <UL>
* <LI>0 if the first <code>n</code> sub-identifiers are the same.
* <LI><0 if the first <code>n</code> sub-identifiers of this
* <code>OID</code> are lexicographic less than those of the comparand.
* <LI>>0 if the first <code>n</code> sub-identifiers of this
* <code>OID</code> are lexicographic greater than those of the comparand.
* </UL>
*/
public int rightMostCompare(int n, OID other) {
int cursorA = value.length-1;
int cursorB = other.value.length-1;
for (int i=n-1; i>=0; i--,cursorA--,cursorB--) {
if (value[cursorA] == other.value[cursorB]) {
continue;
}
else if (value[cursorA] < other.value[cursorB]) {
return -1;
}
else {
return 1;
}
}
return 0;
}
/**
* Check if the OID starts with the given OID.
*
* @param other
* the OID to compare to
* @return
* false if the sub-identifiers do not match.
*/
public boolean startsWith(OID other) {
if (other.value.length > value.length) {
return false;
}
int min = Math.min(value.length, other.value.length);
return (leftMostCompare(min, other) == 0);
}
public Object clone() {
return new OID(value);
}
/**
* Returns the last sub-identifier as an integer value. If this OID is
* empty (i.e. has no sub-identifiers) then a
* <code>NoSuchElementException</code> is thrown
* @return
* the value of the last sub-identifier of this OID as an integer value.
* Sub-identifier values greater than 2^31-1 will be returned as negative
* values!
* @since 1.2
*/
public final int last() {
if (value.length > 0) {
return value[value.length-1];
}
throw new NoSuchElementException();
}
/**
* Returns the last sub-identifier as an unsigned long value. If this OID is
* empty (i.e. has no sub-identifiers) then a
* <code>NoSuchElementException</code> is thrown
* @return
* the value of the last sub-identifier of this OID as an unsigned long.
* @since 1.2
*/
public final long lastUnsigned() {
if (value.length > 0) {
return value[value.length-1] & 0xFFFFFFFFL;
}
throw new NoSuchElementException();
}
/**
* Removes the last sub-identifier (if available) from this <code>OID</code>
* and returns it.
* @return
* the last sub-identifier or -1 if there is no sub-identifier left in
* this <code>OID</code>.
*/
public int removeLast() {
if (value.length == 0) {
return -1;
}
int[] newValue = new int[value.length-1];
System.arraycopy(value, 0, newValue, 0, value.length-1);
int retValue = value[value.length-1];
value = newValue;
return retValue;
}
/**
* Remove the n rightmost subidentifiers from this OID.
* @param n
* the number of subidentifiers to remove. If <code>n</code> is zero or
* negative then this OID will not be changed. If <code>n</code> is greater
* than {@link #size()} all subidentifiers will be removed from this OID.
*/
public void trim(int n) {
if (n > 0) {
if (n > value.length) {
n = value.length;
}
int[] newValue = new int[value.length-n];
System.arraycopy(value, 0, newValue, 0, value.length-n);
value = newValue;
}
}
public int toInt() {
throw new UnsupportedOperationException();
}
public long toLong() {
throw new UnsupportedOperationException();
}
public final OID toSubIndex(boolean impliedLength) {
if (impliedLength) {
return new OID(value);
}
OID subIndex = new OID(new int[] { size() });
subIndex.append(this);
return subIndex;
}
public final void fromSubIndex(OID subIndex, boolean impliedLength) {
int offset = 1;
if (impliedLength) {
offset = 0;
}
setValue(subIndex.getValue(), offset, subIndex.size()-offset);
}
/**
* Returns the successor OID for this OID.
* @return
* an OID clone of this OID with a zero sub-identifier appended.
* @since 1.7
*/
public final OID successor() {
if (value.length == MAX_OID_LEN) {
for (int i=MAX_OID_LEN-1; i>=0; i--) {
if (value[i] != MAX_SUBID_VALUE) {
int[] succ = new int[i+1];
System.arraycopy(value, 0, succ, 0, i+1);
succ[i]++;
return new OID(succ);
}
}
return new OID();
}
else {
int[] succ = new int[value.length + 1];
System.arraycopy(value, 0, succ, 0, value.length);
succ[value.length] = 0;
return new OID(succ);
}
}
/**
* Returns the predecessor OID for this OID.
* @return
* if this OID ends on 0, then a {@link #MAX_OID_LEN}
* sub-identifier OID is returned where each sub-ID for index greater
* or equal to {@link #size()} is set to {@link #MAX_SUBID_VALUE}.
* @since 1.7
*/
public final OID predecessor() {
if (last() != 0) {
int[] pval = new int[MAX_OID_LEN];
System.arraycopy(value, 0, pval, 0, value.length);
Arrays.fill(pval, value.length, pval.length, MAX_SUBID_VALUE);
OID pred = new OID(pval);
pred.set(size()-1, last()-1);
return pred;
}
else {
OID pred = new OID(this);
pred.removeLast();
return pred;
}
}
/**
* Returns the next following OID with the same or lesser size (length).
* @return OID
* the next OID on the same or upper level or a clone of this OID, if
* it has a zero length or is 2^32-1.
* @since 1.7
*/
public final OID nextPeer() {
OID next = new OID(this);
if ((next.size() > 0) && (last() != MAX_SUBID_VALUE)) {
next.set(next.size()-1, last()+1);
}
else if (next.size() > 1) {
next.trim(1);
next = nextPeer();
}
return next;
}
/**
* Returns the greater of the two OID values.
* @param a
* an OID.
* @param b
* an OID.
* @return
* <code>a</code> if a >= b, <code>b</code> otherwise.
* @since 1.7
*/
public static final OID max(OID a, OID b) {
if (a.compareTo(b) >= 0) {
return a;
}
return b;
}
/**
* Returns the lesser of the two OID values.
* @param a
* an OID.
* @param b
* an OID.
* @return
* <code>a</code> if a <= b, <code>b</code> otherwise.
* @since 1.7
*/
public static final OID min(OID a, OID b) {
if (a.compareTo(b) <= 0) {
return a;
}
return b;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -