1 29 30 31 package snmp; 32 33 34 import java.util.*; 35 36 37 40 41 public class SNMPNSAPAddress extends SNMPOctetString 42 { 43 45 46 49 50 public SNMPNSAPAddress() 51 { 52 tag = SNMPBERCodec.SNMPNSAPADDRESS; 53 54 data = new byte[6]; 56 for (int i = 0; i < 6; i++) 57 data[i] = 0; 58 } 59 60 61 62 public SNMPNSAPAddress(String string) 63 throws SNMPBadValueException 64 { 65 tag = SNMPBERCodec.SNMPNSAPADDRESS; 66 67 data = parseNSAPAddress(string); 68 } 69 70 71 72 73 79 80 public SNMPNSAPAddress(byte[] enc) 81 throws SNMPBadValueException 82 { 83 tag = SNMPBERCodec.SNMPNSAPADDRESS; 84 85 if (enc.length == 6) 86 { 87 data = enc; 88 } 89 else { 91 throw new SNMPBadValueException(" NSAPAddress: bad BER encoding supplied to set value "); 92 } 93 } 94 95 96 97 102 103 public void setValue(Object newAddress) 104 throws SNMPBadValueException 105 { 106 if ((newAddress instanceof byte[]) && (((byte[])newAddress).length == 6)) 107 data = (byte[])newAddress; 108 else if (newAddress instanceof String ) 109 { 110 data = parseNSAPAddress((String )newAddress); 111 } 112 else 113 throw new SNMPBadValueException(" NSAPAddress: bad length byte string supplied to set value "); 114 } 115 116 117 118 119 122 123 public String toString() 124 { 125 StringBuffer returnStringBuffer = new StringBuffer (); 126 127 if (data.length > 0) 128 { 129 int convert = data[0]; 130 if (convert < 0) 131 convert += 256; 132 returnStringBuffer.append(Integer.toHexString(convert)); 133 134 for (int i = 1; i < data.length; i++) 135 { 136 convert = data[i]; 137 if (convert < 0) 138 convert += 256; 139 returnStringBuffer.append("-"); 140 returnStringBuffer.append(Integer.toHexString(convert)); 141 } 142 } 143 144 return returnStringBuffer.toString(); 145 } 146 147 148 149 private byte[] parseNSAPAddress(String addressString) 150 throws SNMPBadValueException 151 { 152 try 153 { 154 StringTokenizer st = new StringTokenizer(addressString, " .-"); int size = 0; 156 157 while (st.hasMoreTokens()) 158 { 159 size++; 161 st.nextToken(); 162 } 163 164 if (size != 6) 165 { 166 throw new SNMPBadValueException(" NSAPAddress: wrong number of components supplied to set value "); 167 } 168 169 byte[] returnBytes = new byte[size]; 170 171 st = new StringTokenizer(addressString, " .-"); 172 173 for (int i = 0; i < size; i++) 174 { 175 int addressComponent = (Integer.parseInt(st.nextToken(), 16)); 176 if ((addressComponent < 0) || (addressComponent > 255)) 177 throw new SNMPBadValueException(" NSAPAddress: invalid component supplied to set value "); 178 returnBytes[i] = (byte)addressComponent; 179 } 180 181 return returnBytes; 182 183 } 184 catch (NumberFormatException e) 185 { 186 throw new SNMPBadValueException(" NSAPAddress: invalid component supplied to set value "); 187 } 188 189 } 190 191 192 193 } | Popular Tags |