📄 octetstring.java
字号:
return true;
}
public String toString() {
if (isPrintable()) {
return new String(value);
}
return toHexString();
}
public String toHexString() {
return toHexString(DEFAULT_HEX_DELIMITER);
}
public String toHexString(char separator) {
return toString(separator, 16);
}
public static OctetString fromHexString(String hexString) {
return fromHexString(hexString, DEFAULT_HEX_DELIMITER);
}
public static OctetString fromHexString(String hexString, char delimiter) {
return OctetString.fromString(hexString, delimiter, 16);
}
public static OctetString fromString(String string, char delimiter, int radix) {
String delim = "";
delim += delimiter;
StringTokenizer st = new StringTokenizer(string, delim);
byte[] value = new byte[st.countTokens()];
for (int n=0; st.hasMoreTokens(); n++) {
String s = st.nextToken();
value[n] = (byte)Integer.parseInt(s, radix);
}
return new OctetString(value);
}
/**
* Creates an OctetString from a string represantation in the specified
* radix.
* @param string
* the string representation of an octet string.
* @param radix
* the radix of the string represantion.
* @return
* the OctetString instance.
* @since 1.6
*/
public static OctetString fromString(String string, int radix) {
int digits = (int)(Math.round((float)Math.log(256)/Math.log(radix)));
byte[] value = new byte[string.length()/digits];
for (int n=0; n<string.length(); n+=digits) {
String s = string.substring(n, n+digits);
value[n/digits] = (byte)Integer.parseInt(s, radix);
}
return new OctetString(value);
}
public String toString(char separator, int radix) {
int digits = (int)(Math.round((float)Math.log(256)/Math.log(radix)));
StringBuffer buf = new StringBuffer(value.length*(digits+1));
for (int i=0; i<value.length; i++) {
if (i > 0) {
buf.append(separator);
}
int v = (value[i] & 0xFF);
String val = Integer.toString(v, radix);
for (int j=0; j < digits - val.length(); j++) {
buf.append('0');
}
buf.append(val);
}
return buf.toString();
}
/**
* Returns a string representation of this octet string in the radix
* specified. There will be no separation characters, but each byte will
* be represented by <code>round(log(256)/log(radix))</code> digits.
*
* @param radix
* the radix to use in the string representation.
* @return
* a string representation of this ocetet string in the specified radix.
* @since 1.6
*/
public String toString(int radix) {
int digits = (int)(Math.round((float)Math.log(256)/Math.log(radix)));
StringBuffer buf = new StringBuffer(value.length*(digits+1));
for (int i=0; i<value.length; i++) {
int v = (value[i] & 0xFF);
String val = Integer.toString(v, radix);
for (int j=0; j < digits - val.length(); j++) {
buf.append('0');
}
buf.append(val);
}
return buf.toString();
}
/**
* Formats the content into a ASCII string. Non-printable characters are
* replaced by the supplied placeholder character.
* @param placeholder
* a placeholder character, for example '.'.
* @return
* the contents of this octet string as ASCII formatted string.
* @since 1.6
*/
public String toASCII(char placeholder) {
StringBuffer buf = new StringBuffer(value.length);
for (int i=0; i<value.length; i++) {
if ((Character.isISOControl((char)value[i])) ||
((value[i] & 0xFF) >= 0x80)) {
buf.append(placeholder);
}
else {
buf.append((char) value[i]);
}
}
return buf.toString();
}
public void setValue(String value) {
setValue(value.getBytes());
}
public void setValue(byte[] value) {
if (value == null) {
throw new IllegalArgumentException(
"OctetString must not be assigned a null value");
}
this.value = value;
}
public byte[] getValue() {
return value;
}
/**
* Gets the length of the byte string.
* @return
* an integer >= 0.
*/
public final int length() {
return value.length;
}
public Object clone() {
return new OctetString(value);
}
/**
* Returns the length of the payload of this <code>BERSerializable</code>
* object in bytes when encoded according to the Basic Encoding Rules (BER).
*
* @return the BER encoded length of this variable.
*/
public int getBERPayloadLength() {
return value.length;
}
public int toInt() {
throw new UnsupportedOperationException();
}
public long toLong() {
throw new UnsupportedOperationException();
}
/**
* Returns a copy of this OctetString where each bit not set in the supplied
* mask zeros the corresponding bit in the returned OctetString.
* @param mask
* a mask where the n-th bit corresponds to the n-th bit in the returned
* OctetString.
* @return
* the masked OctetString.
* @since 1.7
*/
public OctetString mask(OctetString mask) {
byte[] masked = new byte[value.length];
System.arraycopy(value, 0, masked, 0, value.length);
for (int i=0; (i<mask.length()) && (i<masked.length); i++) {
masked[i] = (byte)(masked[i] & mask.get(i));
}
return new OctetString(masked);
}
public OID toSubIndex(boolean impliedLength) {
int[] subIndex;
int offset = 0;
if (!impliedLength) {
subIndex = new int[length()+1];
subIndex[offset++] = length();
}
else {
subIndex = new int[length()];
}
for (int i=0; i<length(); i++) {
subIndex[offset+i] = get(i) & 0xFF;
}
return new OID(subIndex);
}
public void fromSubIndex(OID subIndex, boolean impliedLength) {
if (impliedLength) {
setValue(subIndex.toByteArray());
}
else {
OID suffix = new OID(subIndex.getValue(), 1, subIndex.size() - 1);
setValue(suffix.toByteArray());
}
}
/**
* Splits an <code>OctetString</code> using a set of delimiter characters
* similar to how a StringTokenizer would do it.
* @param octetString
* the input string to tokenize.
* @param delimOctets
* a set of delimiter octets.
* @return
* a Collection of OctetString instances that contain the tokens.
*/
public static final Collection split(OctetString octetString,
OctetString delimOctets) {
List parts = new LinkedList();
int maxDelim = -1;
for (int i = 0; i<delimOctets.length(); i++) {
int delim = delimOctets.get(i) & 0xFF;
if (delim > maxDelim) {
maxDelim = delim;
}
}
int startPos = 0;
for (int i = 0; i<octetString.length(); i++) {
int c = octetString.value[i] & 0xFF;
boolean isDelim = false;
if (c <= maxDelim) {
for (int j=0; j<delimOctets.length(); j++) {
if (c == (delimOctets.get(j) & 0xFF)) {
if ((startPos >= 0) && (i > startPos)) {
parts.add(new OctetString(octetString.value,
startPos, i - startPos));
}
startPos = -1;
isDelim = true;
}
}
}
if (!isDelim && (startPos < 0)) {
startPos = i;
}
}
if (startPos >= 0) {
parts.add(new OctetString(octetString.value, startPos,
octetString.length() - startPos));
}
return parts;
}
/**
* Creates an <code>OctetString</code> from an byte array.
* @param value
* a byte array that is copied into the value of the created
* <code>OctetString</code> or <code>null</code>.
* @return
* an OctetString or <code>null</code> if <code>value</code>
* is <code>null</code>.
* @since 1.7
*/
public static OctetString fromByteArray(byte[] value) {
if (value == null) {
return null;
}
return new OctetString(value);
}
public byte[] toByteArray() {
return getValue();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -