📄 octetstring.java
字号:
/*_############################################################################
_##
_## SNMP4J - OctetString.java
_##
_## Copyright 2003-2007 Frank Fock and Jochen Katz (SNMP4J.org)
_##
_## Licensed under the Apache License, Version 2.0 (the "License");
_## you may not use this file except in compliance with the License.
_## You may obtain a copy of the License at
_##
_## http://www.apache.org/licenses/LICENSE-2.0
_##
_## Unless required by applicable law or agreed to in writing, software
_## distributed under the License is distributed on an "AS IS" BASIS,
_## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
_## See the License for the specific language governing permissions and
_## limitations under the License.
_##
_##########################################################################*/
package org.snmp4j.smi;
import java.io.*;
import java.util.*;
import org.snmp4j.asn1.BER;
import org.snmp4j.asn1.BERInputStream;
/**
* The <code>OctetString</code> class represents the SMI type OCTET STRING.
*
* @author Frank Fock
* @version 1.8
* @since 1.0
*/
public class OctetString extends AbstractVariable
implements AssignableFromByteArray, AssignableFromString {
private static final long serialVersionUID = 4125661211046256289L;
private static final char DEFAULT_HEX_DELIMITER = ':';
private byte[] value = new byte[0];
/**
* Creates a zero length octet string.
*/
public OctetString() {
}
/**
* Creates an octet string from an byte array.
* @param rawValue
* an array of bytes.
*/
public OctetString(byte[] rawValue) {
this(rawValue, 0, rawValue.length);
}
/**
* Creates an octet string from an byte array.
* @param rawValue
* an array of bytes.
* @param offset
* the position (zero based) of the first byte to be copied from
* <code>rawValue</code>into the new <code>OctetSring</code>.
* @param length
* the number of bytes to be copied.
*/
public OctetString(byte[] rawValue, int offset, int length) {
value = new byte[length];
System.arraycopy(rawValue, offset, value, 0, length);
}
/**
* Creates an octet string from a java string.
*
* @param stringValue
* a Java string.
*/
public OctetString(String stringValue) {
this.value = stringValue.getBytes();
}
/**
* Creates an octet string from another OctetString by cloning its value.
*
* @param other
* an <code>OctetString</code> instance.
*/
public OctetString(OctetString other) {
this.value = new byte[0];
append(other);
}
/**
* Appends a single byte to this octet string.
* @param b
* a byte value.
*/
public void append(byte b) {
byte[] newValue = new byte[value.length+1];
System.arraycopy(value, 0, newValue, 0, value.length);
newValue[value.length] = b;
value = newValue;
}
/**
* Appends an array of bytes to this octet string.
* @param bytes
* an array of bytes.
*/
public void append(byte[] bytes) {
byte[] newValue = new byte[value.length + bytes.length];
System.arraycopy(value, 0, newValue, 0, value.length);
System.arraycopy(bytes, 0, newValue, value.length, bytes.length);
value = newValue;
}
/**
* Appends an octet string.
* @param octetString
* an <code>OctetString</code> to append to this octet string.
*/
public void append(OctetString octetString) {
append(octetString.getValue());
}
/**
* Appends the supplied string to this <code>OctetString</code>. Calling this
* method is identical to <I>append(string.getBytes())</I>.
* @param string
* a String instance.
*/
public void append(String string) {
append(string.getBytes());
}
/**
* Sets the value of the octet string to a zero length string.
*/
public void clear() {
value = new byte[0];
}
public void encodeBER(OutputStream outputStream) throws java.io.IOException {
BER.encodeString(outputStream, BER.OCTETSTRING, getValue());
}
public void decodeBER(BERInputStream inputStream) throws java.io.IOException {
BER.MutableByte type = new BER.MutableByte();
byte[] v = BER.decodeString(inputStream, type);
if (type.getValue() != BER.OCTETSTRING) {
throw new IOException("Wrong type encountered when decoding OctetString: "+
type.getValue());
}
setValue(v);
}
public int getBERLength() {
return value.length + BER.getBERLengthOfLength(value.length) + 1;
}
public int getSyntax() {
return SMIConstants.SYNTAX_OCTET_STRING;
}
/**
* Gets the byte at the specified index.
* @param index
* a zero-based index into the octet string.
* @return
* the byte value at the specified index.
* @throws ArrayIndexOutOfBoundsException
* if <code>index</code> < 0 or > {@link #length()}.
*/
public final byte get(int index) {
return value[index];
}
/**
* Sets the byte value at the specified index.
* @param index
* an index value greater or equal 0 and less than {@link #length()}.
* @param b
* the byte value to set.
* @since v1.2
*/
public final void set(int index, byte b) {
value[index] = b;
}
public int hashCode() {
int hash = 0;
for (int i=0; i<value.length; i++) {
hash += value[i]*31^((value.length-1)-i);
}
return hash;
}
public boolean equals(Object o) {
if (o instanceof OctetString) {
OctetString other = (OctetString)o;
return Arrays.equals(value, other.value);
}
else if (o instanceof byte[]) {
return Arrays.equals(value, (byte[])o);
}
return false;
}
public int compareTo(Object o) {
if (o instanceof OctetString) {
OctetString other = (OctetString)o;
int maxlen = Math.min(value.length, other.value.length);
for (int i=0; i<maxlen; i++) {
if (value[i] != other.value[i]) {
if ((value[i] & 0xFF) < (other.value[i] & 0xFF)) {
return -1;
}
else {
return 1;
}
}
}
return (value.length - other.value.length);
}
throw new ClassCastException(o.getClass().getName());
}
/**
* Returns a new string that is a substring of this string. The substring
* begins at the specified <code>beginIndex</code> and extends to the
* character at index <code>endIndex - 1</code>.
* Thus the length of the substring is <code>endIndex-beginIndex</code>.
* @param beginIndex
* the beginning index, inclusive.
* @param endIndex
* the ending index, exclusive.
* @return
* the specified substring.
* @since 1.3
*/
public OctetString substring(int beginIndex, int endIndex) {
if ((beginIndex < 0) || (endIndex > length())) {
throw new IndexOutOfBoundsException();
}
byte[] substring = new byte[endIndex - beginIndex];
System.arraycopy(value, beginIndex, substring, 0, substring.length);
return new OctetString(substring);
}
/**
* Tests if this octet string starts with the specified prefix.
* @param prefix
* the prefix.
* @return
* <code>true</code> if the bytes of this octet string up to the length
* of <code>prefix</code> equal those of <code>prefix</code>.
* @since 1.2
*/
public boolean startsWith(OctetString prefix) {
if ((prefix == null) || prefix.length() > length()) {
return false;
}
for (int i=0; i<prefix.length(); i++) {
if (prefix.get(i) != value[i]) {
return false;
}
}
return true;
}
/**
* Determines whether this octet string contains non ISO control characters
* only.
* @return
* <code>false</code> if this octet string contains any ISO control
* characters as defined by <code>Character.isISOControl(char)</code>
* except if these ISO control characters are all whitespace characters
* as defined by <code>Character.isWhitespace(char)</code>.
*/
public boolean isPrintable() {
for (int i=0; i<value.length; i++) {
char c = (char)value[i];
if ((Character.isISOControl(c) ||
((value[i] & 0xFF) >= 0x80)) && (!Character.isWhitespace(c))) {
return false;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -