1 11 12 13 package com.sun.jmx.snmp; 14 15 16 17 18 26 27 public class SnmpIpAddress extends SnmpOid { 28 29 36 public SnmpIpAddress(byte[] bytes) throws IllegalArgumentException { 37 buildFromByteArray(bytes); 38 } 39 40 44 public SnmpIpAddress(long addr) { 45 int address = (int)addr ; 46 byte[] ipaddr = new byte[4]; 47 48 ipaddr[0] = (byte) ((address >>> 24) & 0xFF); 49 ipaddr[1] = (byte) ((address >>> 16) & 0xFF); 50 ipaddr[2] = (byte) ((address >>> 8) & 0xFF); 51 ipaddr[3] = (byte) (address & 0xFF); 52 53 buildFromByteArray(ipaddr); 54 } 55 56 62 public SnmpIpAddress(String dotAddress) throws IllegalArgumentException { 63 super(dotAddress) ; 64 if ((componentCount > 4) || 65 (components[0] > 255) || 66 (components[1] > 255) || 67 (components[2] > 255) || 68 (components[3] > 255)) { 69 throw new IllegalArgumentException (dotAddress) ; 70 } 71 } 72 73 81 public SnmpIpAddress(long b1, long b2, long b3, long b4) { 82 super(b1, b2, b3, b4) ; 83 if ((components[0] > 255) || 84 (components[1] > 255) || 85 (components[2] > 255) || 86 (components[3] > 255)) { 87 throw new IllegalArgumentException () ; 88 } 89 } 90 91 97 public byte[] byteValue() { 98 byte[] result = new byte[4] ; 99 result[0] = (byte)components[0] ; 100 result[1] = (byte)components[1] ; 101 result[2] = (byte)components[2] ; 102 result[3] = (byte)components[3] ; 103 104 return result ; 105 } 106 107 112 public String stringValue() { 113 return toString() ; 114 } 115 116 125 public static SnmpOid toOid(long[] index, int start) throws SnmpStatusException { 126 if (start + 4 <= index.length) { 127 try { 128 return new SnmpOid( 129 index[start], 130 index[start+1], 131 index[start+2], 132 index[start+3]) ; 133 } 134 catch(IllegalArgumentException e) { 135 throw new SnmpStatusException(SnmpStatusException.noSuchName) ; 136 } 137 } 138 else { 139 throw new SnmpStatusException(SnmpStatusException.noSuchName) ; 140 } 141 } 142 143 152 public static int nextOid(long[] index, int start) throws SnmpStatusException { 153 if (start + 4 <= index.length) { 154 return start + 4 ; 155 } 156 else { 157 throw new SnmpStatusException(SnmpStatusException.noSuchName) ; 158 } 159 } 160 161 166 public static void appendToOid(SnmpOid source, SnmpOid dest) { 167 if (source.getLength() != 4) { 168 throw new IllegalArgumentException () ; 169 } 170 dest.append(source) ; 171 } 172 173 177 final public String getTypeName() { 178 return name ; 179 } 180 181 186 private void buildFromByteArray(byte[] bytes) { 187 if (bytes.length != 4) { 188 throw new IllegalArgumentException () ; 189 } 190 components = new long[4] ; 191 componentCount= 4; 192 components[0] = (bytes[0] >= 0) ? bytes[0] : bytes[0] + 256 ; 193 components[1] = (bytes[1] >= 0) ? bytes[1] : bytes[1] + 256 ; 194 components[2] = (bytes[2] >= 0) ? bytes[2] : bytes[2] + 256 ; 195 components[3] = (bytes[3] >= 0) ? bytes[3] : bytes[3] + 256 ; 196 } 197 198 203 final static String name = "IpAddress" ; 204 } 205 | Popular Tags |