/** * 将SnmpVar转换为字符串 * * @param xVar SnmpVar * @return 字符串 * @throws Exception */ public static String snmpVar2Str(SnmpVar var) { String result = null; if (var == null) { return ""; } // 增加中文支持 if (var instanceof SnmpString) { String str = new String(snmpVar2ByteArray(var)); if (str.getBytes().length != str.length()) { result = str; } else { SnmpString strSnmp = (SnmpString) var; result = (strSnmp.toString()); } } else if (var instanceof SnmpInt) { SnmpInt intSnmp = (SnmpInt) var; result = "" + intSnmp.intValue(); } else if (var instanceof SnmpGauge) { SnmpGauge snmpGauge = (SnmpGauge) var; result = "" + snmpGauge.longValue(); } return result; }
/** * 将SnmpVar转换为int * * @param xVar SnmpVar * @return int * @throws Exception */ public static long snmpVar2Long(SnmpVar var) { long result = 0; if (var instanceof SnmpInt) { SnmpInt intVar = (SnmpInt) var; result = intVar.longValue(); } else if (var instanceof SnmpGauge) { SnmpGauge gauge = (SnmpGauge) var; result = gauge.longValue(); } return result; }
/** * 将SnmpVar转换为时间,排除+8时区的问题 * * @param var * @return */ public static String snmpVar2Timeticks_String_TimeZone(SnmpVar var) { String resu = ""; if (var == null) { return resu; } if (var instanceof SnmpTimeticks) { SnmpTimeticks loSnmp = (SnmpTimeticks) var; resu = loSnmp.toString(); } if (var instanceof SnmpGauge) { SnmpGauge timeVar = (SnmpGauge) var; resu = DateFormatter.getLongDate((timeVar.longValue() - 8 * 3600) * 1000); } else if (var instanceof SnmpInt) { SnmpInt timeVar = (SnmpInt) var; resu = DateFormatter.getLongDate((timeVar.longValue() - 8 * 3600) * 1000); } return resu; }
/** * 将SnmpVar转换为时间 * * @param xVar SnmpVar * @return * @throws Exception */ public static Long snmpVar2Timeticks_Long(SnmpVar var) { // 改写方法返回String Long resu = 0L; if (var == null) { return resu; } if (var instanceof SnmpTimeticks) { SnmpTimeticks loSnmp = (SnmpTimeticks) var; resu = loSnmp.longValue(); } if (var instanceof SnmpGauge) { SnmpGauge timeVar = (SnmpGauge) var; resu = timeVar.longValue(); } else if (var instanceof SnmpInt) { SnmpInt timeVar = (SnmpInt) var; resu = timeVar.longValue(); } return resu; }
/** * 将SnmpVar转换为int * * @param xVar SnmpVar * @return int * @throws Exception */ public static int snmpVar2Int(SnmpVar var) { int result = -1; if (var instanceof SnmpInt) { SnmpInt intVar = (SnmpInt) var; result = intVar.intValue(); } else if (var instanceof SnmpCounter) { SnmpCounter counter = (SnmpCounter) var; result = (int) counter.longValue(); } else if (var instanceof SnmpGauge) { SnmpGauge gauge = (SnmpGauge) var; result = (int) gauge.longValue(); } else if (var instanceof SnmpUnsignedInt) { SnmpUnsignedInt unsignedInt = (SnmpUnsignedInt) var; result = (int) unsignedInt.longValue(); } else if (var instanceof SnmpNull) { result = -1; } else if (var instanceof SnmpCounter64) { SnmpCounter64 counter = (SnmpCounter64) var; result = Integer.parseInt(counter.getNumericValueAsString()); } return result; }