本文整理汇总了Java中org.snmp4j.asn1.BER类的典型用法代码示例。如果您正苦于以下问题:Java BER类的具体用法?Java BER怎么用?Java BER使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
BER类属于org.snmp4j.asn1包,在下文中一共展示了BER类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: decodeBER
import org.snmp4j.asn1.BER; //导入依赖的package包/类
public void decodeBER(BERInputStream inputStream) throws java.io.IOException {
BER.MutableByte type = new BER.MutableByte();
byte[] value = BER.decodeString(inputStream, type);
if (type.getValue() != BER.IPADDRESS) {
throw new IOException("Wrong type encountered when decoding Counter: "+
type.getValue());
}
if (value.length != 4) {
if ( (value.length == 8) && Boolean.getBoolean("org.opennms.snmp.workarounds.allow64BitIpAddress") ) {
byte[] tempValue = { 0,0,0,0 };
for (int i = 0; i < 4; i++) {
tempValue[i] = value[i];
}
value = tempValue;
if (log().isDebugEnabled()) {
log().debug("Working around misencoded IpAddress (8 bytes, truncating to 4); likely dealing with a buggy Net-SNMP agent");
}
} else {
throw new IOException("IpAddress encoding error, wrong length: " +
value.length);
}
}
this.setInetAddress(InetAddress.getByAddress(value));
}
示例2: setOIDEncodingToByteArray
import org.snmp4j.asn1.BER; //导入依赖的package包/类
/**
*
* @param sObjectID
* @param bBerDataType
* @param sValue
* @return byte[]
* @throws Exception */
public static byte[] setOIDEncodingToByteArray (String sObjectID , byte bBerDataType , String sValue) throws Exception {
boolean localDebug = Boolean.FALSE;
if (localDebug|debug) {
logger.debug("BERService.setOIDEncodingToByteArray(s,b,s) - OID: " + sObjectID + " -> Value: " + sValue);
}
sObjectID = NetSNMP.toDottedOID(sObjectID);
if (bBerDataType == HEX) {
return HexString.toByteArray(setOIDEncoding ( sObjectID , BER.OCTETSTRING , HexString.toByteArray(sValue)));
} else {
return HexString.toByteArray(setOIDEncoding ( sObjectID , bBerDataType , sValue));
}
}
示例3: isNumberDataType
import org.snmp4j.asn1.BER; //导入依赖的package包/类
/**
*
* @param iBerDataType
* @return boolean
*/
public static boolean isNumberDataType (int iBerDataType) {
boolean boolStatus = false;
if ((iBerDataType == BER.COUNTER) ||
(iBerDataType == BER.COUNTER32) ||
(iBerDataType == BER.COUNTER64) ||
(iBerDataType == BER.GAUGE) ||
(iBerDataType == BER.GAUGE32) ||
(iBerDataType == BER.INTEGER) ||
(iBerDataType == BER.INTEGER32) ||
(iBerDataType == BER.TIMETICKS)) {
boolStatus = true;
}
return boolStatus;
}
示例4: berStringDataTypeToByte
import org.snmp4j.asn1.BER; //导入依赖的package包/类
/**
* @param sDataType
* @return byte */
public static byte berStringDataTypeToByte (String sDataType) {
if (COUNTER32.equalsIgnoreCase(sDataType)) {
return BER.COUNTER32;
} else if (COUNTER64.equalsIgnoreCase(sDataType)) {
return BER.COUNTER64;
} else if (GAUGE32.equalsIgnoreCase(sDataType)) {
return BER.GAUGE32;
} else if (INTEGER32.equalsIgnoreCase(sDataType)) {
return BER.INTEGER32;
} else if (TIMETICKS.equalsIgnoreCase(sDataType)) {
return BER.TIMETICKS;
} else if (IPADDRESS.equalsIgnoreCase(sDataType)) {
return BER.IPADDRESS;
} else if (OCTETSTRING.equalsIgnoreCase(sDataType)) {
return BER.OCTETSTRING;
} else if (HEXSTRING.equalsIgnoreCase(sDataType)) {
return HEX;
}
return 0x00;
}
示例5: decodeBER
import org.snmp4j.asn1.BER; //导入依赖的package包/类
public void decodeBER(BERInputStream inputStream) throws IOException {
BER.MutableByte type = new BER.MutableByte();
byte[] value = BER.decodeString(inputStream, type);
if (type.getValue() != BER.IPADDRESS) {
throw new IOException(
"Wrong type encountered when decoding Counter: "
+ type.getValue()
);
}
if (value.length != 4) {
logger.warn(
"IpAddress encoding is invalid, wrong length: " + value.length
);
super.setInetAddress(null);
rawValue = value;
}
else {
setAddress(value);
}
}
示例6: isStringDataType
import org.snmp4j.asn1.BER; //导入依赖的package包/类
/**
*
* @param iBerDataType
* @return boolean*/
public static boolean isStringDataType (int iBerDataType) {
boolean boolStatus = false;
if ((iBerDataType == BER.IPADDRESS) || (iBerDataType == BER.OCTETSTRING) ) {
boolStatus = true;
}
return boolStatus;
}
示例7: initBER
import org.snmp4j.asn1.BER; //导入依赖的package包/类
/**
* */
private void initBER() {
BER_DATA_TYPE.put((int) BER.COUNTER32, "Counter32");
BER_DATA_TYPE.put((int) BER.COUNTER64, "Counter64");
BER_DATA_TYPE.put((int) BER.GAUGE32, "Gauge32");
BER_DATA_TYPE.put((int) BER.INTEGER32, "Integer32");
BER_DATA_TYPE.put((int) BER.TIMETICKS, "TimeTicks");
BER_DATA_TYPE.put((int) BER.IPADDRESS, "IpAddress");
BER_DATA_TYPE.put((int) BER.OCTETSTRING, "OctetString");
//This Type does not exists as a SNMP DataType, this is only for use in this program
BER_DATA_TYPE.put(BinaryConversion.byteToUnsignedInteger(BERService.HEX), "HexString");
}
示例8: init
import org.snmp4j.asn1.BER; //导入依赖的package包/类
public void init(String host, String varBind) {
vbs = getVariableBinding(varBind);
if (pduType == PDU.INFORM || pduType == PDU.TRAP) {
checkTrapVariables(vbs);
}
address = new UdpAddress(host + "/" + _port);
LogFactory.setLogFactory(new Log4jLogFactory());
BER.setCheckSequenceLength(false);
}
示例9: decodeBER
import org.snmp4j.asn1.BER; //导入依赖的package包/类
public void decodeBER(BERInputStream inputStream) throws java.io.IOException {
BER.MutableByte type = new BER.MutableByte();
int length;
BigInteger value = BigInteger.ZERO;
type.setValue((byte)inputStream.read());
if ((type.getValue() != 0x02) && (type.getValue() != 0x43) &&
(type.getValue() != 0x41)) {
throw new IOException("Wrong ASN.1 type. Not an integer: "+type.getValue()+
(" at position "+inputStream.getPosition()));
}
length = BER.decodeLength(inputStream);
if (length > 4) {
if (log().isDebugEnabled()) {
log().debug("Working around invalid Integer32 likely dealing with a permissive Net-SNMP agent");
}
}
int b = inputStream.read() & 0xFF;
if ((b & 0x80) > 0) {
value = BigInteger.ONE.negate();
}
while (length-- > 0) {
value = value.shiftLeft(8).or(BigInteger.valueOf(b));
if (length > 0) {
b = inputStream.read();
}
}
int newValue = value.compareTo(BigInteger.valueOf(Integer.MAX_VALUE)) > 0 ? 0 : value.intValue();
if (type.getValue() != BER.INTEGER) {
throw new IOException("Wrong type encountered when decoding Counter: "+type.getValue());
}
setValue(newValue);
}
示例10: encodeBER
import org.snmp4j.asn1.BER; //导入依赖的package包/类
public void encodeBER(OutputStream outputStream) throws IOException {
if (isValid()) {
super.encodeBER(outputStream);
}
else {
logger.warn("Encode invalid IpAddress: " + joinBytesWithDot(rawValue));
BER.encodeString(outputStream, BER.IPADDRESS, rawValue);
}
}
示例11: getBERLength
import org.snmp4j.asn1.BER; //导入依赖的package包/类
public int getBERLength() {
if (isValid()) {
return super.getBERLength();
}
else {
return rawValue.length + BER.getBERLengthOfLength(rawValue.length) + 1;
}
}
示例12: format
import org.snmp4j.asn1.BER; //导入依赖的package包/类
/**
* Returns a textual representation of the supplied variable against the
* optionally supplied instance OID.
*
* @param instanceOID the instance OID <code>variable</code> is associated
* with. If <code>null</code> the formatting cannot take any MIB
* specification of the variable into account and has to format it based
* on its type only.
* @param variable
* the variable to format.
* @param withOID
* if <code>true</code> the <code>instanceOID</code> should be included
* in the textual representation to form a {@link VariableBinding}
* representation.
* @return the textual representation.
*/
public String format(OID instanceOID, Variable variable, boolean withOID) {
String valueString;
String typeString = variable.getSyntaxString();
switch (variable.getSyntax()) {
case SMIConstants.SYNTAX_TIMETICKS :
valueString = String.valueOf(((TimeTicks) variable).getValue());
break;
case SMIConstants.SYNTAX_OCTET_STRING :
if (!is_PrintableOneLine(((OctetString)variable))) {
typeString = TYPE_NAME_4_OCTETSTRING_IN_HEX;
valueString = ((OctetString)variable).toHexString();
}
else {
valueString = variable.toString();
}
break;
case BER.BITSTRING:
valueString = ((BitString)variable).toHexString();
break;
default :
valueString = variable.toString();
break;
}
if (withOID) {
return oidFormat.format(instanceOID.getValue())
+ SEPARATOR + typeString + SEPARATOR + valueString;
}
else {
return valueString;
}
}
示例13: parseVariableBinding
import org.snmp4j.asn1.BER; //导入依赖的package包/类
public VariableBinding parseVariableBinding(String text) throws ParseException {
String[] tokens = text.split(SEPARATOR, 3);
if (tokens.length != 3) {
throw new ParseException("Invalid format: "+ text, 0);
}
OID oid = new OID(oidFormat.parse(tokens[0]));
String typeName = tokens[1];
String valueString = tokens[2];
if (typeName.equals(TYPE_NAME_4_OCTETSTRING_IN_HEX)) {
OctetString os
= (OctetString)parse(SMIConstants.SYNTAX_OCTET_STRING, "");
os.setValue(OctetString.fromHexString(valueString).getValue());
return new VariableBinding(oid, os);
}
else if (typeName.equals(AbstractVariable.getSyntaxString(BER.BITSTRING))) {
BitString bs = (BitString)parse(BER.BITSTRING, "");
bs.setValue(BitString.fromHexString(valueString).getValue());
return new VariableBinding(oid, bs);
}
else {
return new VariableBinding(
oid,
parse(AbstractVariable.getSyntaxFromString(typeName), valueString)
);
}
}
示例14: main
import org.snmp4j.asn1.BER; //导入依赖的package包/类
public static void main(String[] args) {
ConfigurationFileExport cfe =
new ConfigurationFileExport (TestDirectoryStructure.fInputDirFileName("DOCSIS-GOLDEN.txt.bin"),
ConfigurationFileExport.DOCSIS_VER_31);
System.out.println(cfe.toPrettyPrint(ConfigurationFileExport.EXPORT_FOUND_TLV));
ConfigurationFileImport cfi =
new ConfigurationFileImport(new StringBuilder(cfe.toPrettyPrint(ConfigurationFileExport.EXPORT_FOUND_TLV)));
ConfigurationFile cf = new ConfigurationFile(cfi.getConfigurationFileType(),cfi.getTlvBuilder());
CommonTlvInsertions.insertSnmpOID("docsDevNmAccessIp.1", BER.INTEGER, 1, cf, CommonTlvInsertions.FINALIZE_FALSE);
cfe = new ConfigurationFileExport(cf);
System.out.println(cfe.toPrettyPrint(ConfigurationFileExport.EXPORT_FOUND_TLV));
}
示例15: parseBits
import org.snmp4j.asn1.BER; //导入依赖的package包/类
private BitString parseBits(String valueString) throws ParseException {
String[] strOctets = valueString.split(String.valueOf(HEX_SEPARATOR));
StringBuilder valuePattern = new StringBuilder();
StringBuilder parsedOctets = new StringBuilder();
boolean match = false;
for (int octIdx = 0; octIdx < strOctets.length; octIdx++) {
int octet;
try {
octet = Integer.parseInt(strOctets[octIdx], 16);
parsedOctets.append(strOctets[octIdx]).append(HEX_SEPARATOR);
} catch (NumberFormatException e) {
throw
new ParseException("Invalid value for BITS: "+ valueString, octIdx);
}
int mask = 128;
for (int bitIdx = 0; bitIdx < 8; bitIdx++) {
if ((octet & mask) != 0) {
valuePattern.append(octIdx * 8 + bitIdx).append(HEX_SEPARATOR);
}
mask >>= 1;
}
if (
valueString.matches(parsedOctets + "(00 )*" + valuePattern)
) {
match = true;
break;
}
}
BitString bs
= (BitString)AbstractVariable.createFromSyntax(BER.ASN_BIT_STR);
if (match) {
bs.setValue(
OctetString.fromHexString(
valueString.substring(
0,
valueString.lastIndexOf(valuePattern.toString())
),
HEX_SEPARATOR
).getValue()
);
}
else {
bs.setValue(
OctetString.fromHexString(
valueString,
HEX_SEPARATOR
).getValue()
);
}
return bs;
}