1 29 30 31 package snmp; 32 33 import java.io.*; 34 35 36 39 40 public class SNMPOctetString extends SNMPObject 41 { 42 protected byte[] data; 43 protected byte tag = SNMPBERCodec.SNMPOCTETSTRING; 44 45 46 49 50 public SNMPOctetString() 51 { 52 data = new byte[0]; 53 } 54 55 56 59 60 public SNMPOctetString(String stringData) 61 { 62 this.data = stringData.getBytes(); 63 } 64 65 66 67 68 73 74 public SNMPOctetString(byte[] enc) 75 { 76 extractFromBEREncoding(enc); 77 } 78 79 80 81 82 85 86 public Object getValue() 87 { 88 return data; 89 } 90 91 92 93 94 98 99 public void setValue(Object data) 100 throws SNMPBadValueException 101 { 102 if (data instanceof byte[]) 103 this.data = (byte[])data; 104 else if (data instanceof String ) 105 this.data = ((String )data).getBytes(); 106 else 107 throw new SNMPBadValueException(" Octet String: bad object supplied to set value "); 108 } 109 110 111 112 113 114 118 119 protected byte[] getBEREncoding() 120 { 121 122 ByteArrayOutputStream outBytes = new ByteArrayOutputStream(); 123 124 byte[] len = SNMPBERCodec.encodeLength(data.length); 126 127 outBytes.write(tag); 129 outBytes.write(len, 0, len.length); 130 outBytes.write(data, 0, data.length); 131 132 return outBytes.toByteArray(); 133 } 134 135 136 137 138 protected void extractFromBEREncoding(byte[] enc) 139 { 140 data = new byte[enc.length]; 141 142 for (int i = 0; i < enc.length; i++) 144 { 145 data[i] = enc[i]; 146 } 147 } 148 149 150 151 154 155 public boolean equals(Object other) 156 { 157 if (other == null) 159 { 160 return false; 161 } 162 163 if (!this.getClass().equals(other.getClass())) 165 { 166 return false; 167 } 168 169 SNMPOctetString otherSNMPObject = (SNMPOctetString)other; 170 171 if (java.util.Arrays.equals((byte[])this.getValue(),(byte[])otherSNMPObject.getValue())) 173 { 174 return true; 175 } 176 else 177 { 178 return false; 179 } 180 } 181 182 183 184 187 188 public int hashCode() 189 { 190 int hash = 0; 191 192 for (int i = 0; i < data.length; i++) 194 { 195 hash += data[i]; 196 hash += (hash << 10); 197 hash ^= (hash >> 6); 198 } 199 200 hash += (hash << 3); 201 hash ^= (hash >> 11); 202 hash += (hash << 15); 203 204 return hash; 205 } 206 207 208 209 210 211 215 216 public String toString() 217 { 218 String returnString; 219 220 241 242 255 256 returnString = new String (data); 257 258 return returnString; 259 260 } 261 262 263 264 private String hexByte(byte b) 265 { 266 int pos = b; 267 if (pos < 0) 268 pos += 256; 269 String returnString = new String (); 270 returnString += Integer.toHexString(pos/16); 271 returnString += Integer.toHexString(pos%16); 272 return returnString; 273 } 274 275 276 277 280 281 public String toHexString() 282 { 283 StringBuffer returnStringBuffer = new StringBuffer (); 284 285 286 for (int i = 0; i < data.length; i++) 287 { 288 returnStringBuffer.append(hexByte(data[i]) + " "); 289 } 290 291 return returnStringBuffer.toString(); 292 293 } 294 295 296 297 } | Popular Tags |