1 29 30 31 package snmp; 32 33 import java.util.*; 34 import java.io.*; 35 36 37 43 44 public class SNMPSequence extends SNMPObject 45 { 46 protected Vector sequence; 48 protected byte tag = SNMPBERCodec.SNMPSEQUENCE; 49 50 51 54 55 public SNMPSequence() 56 { 57 sequence = new Vector(); 58 } 59 60 61 62 63 67 68 public SNMPSequence(Vector v) 69 throws SNMPBadValueException 70 { 71 72 Enumeration e = v.elements(); 73 74 while (e.hasMoreElements()) 75 { 76 if (!(e.nextElement() instanceof SNMPObject)) 77 throw new SNMPBadValueException("Non-SNMPObject supplied to SNMPSequence."); 78 } 79 80 81 sequence = v; 82 } 83 84 85 86 87 91 92 protected SNMPSequence(byte[] enc) 93 throws SNMPBadValueException 94 { 95 extractFromBEREncoding(enc); 96 } 97 98 99 100 101 102 105 106 public Object getValue() 107 { 108 return sequence; 109 } 110 111 112 113 114 115 120 121 public void setValue(Object newSequence) 122 throws SNMPBadValueException 123 { 124 if (newSequence instanceof Vector) 125 { 126 127 129 Enumeration e = ((Vector)newSequence).elements(); 130 131 while (e.hasMoreElements()) 132 { 133 if (!(e.nextElement() instanceof SNMPObject)) 134 throw new SNMPBadValueException("Non-SNMPObject supplied to SNMPSequence."); 135 } 136 137 138 this.sequence = (Vector)newSequence; 139 } 140 else 141 throw new SNMPBadValueException(" Sequence: bad object supplied to set value "); 142 } 143 144 145 146 149 150 public int size() 151 { 152 return sequence.size(); 153 } 154 155 156 157 161 162 public void addSNMPObject(SNMPObject newObject) 163 throws SNMPBadValueException 164 { 165 sequence.insertElementAt(newObject, sequence.size()); 166 } 167 168 169 170 171 175 176 public void insertSNMPObjectAt(SNMPObject newObject, int index) 177 throws SNMPBadValueException 178 { 179 sequence.insertElementAt(newObject, index); 180 } 181 182 183 184 185 186 189 190 public SNMPObject getSNMPObjectAt(int index) 191 { 192 return (SNMPObject)(sequence.elementAt(index)); 193 } 194 195 196 197 198 201 202 protected byte[] getBEREncoding() 203 { 204 ByteArrayOutputStream outBytes = new ByteArrayOutputStream(); 205 206 207 byte[] data = encodeVector(); 209 210 byte[] len = SNMPBERCodec.encodeLength(data.length); 212 213 outBytes.write(tag); 215 outBytes.write(len, 0, len.length); 216 outBytes.write(data, 0, data.length); 217 218 return outBytes.toByteArray(); 219 } 220 221 222 223 private byte[] encodeVector() 224 { 225 ByteArrayOutputStream outBytes = new ByteArrayOutputStream(); 226 227 int numElements = sequence.size(); 228 for (int i = 0; i < numElements; ++i) 229 { 230 byte[] nextBytes = ((SNMPObject)(sequence.elementAt(i))).getBEREncoding(); 231 outBytes.write(nextBytes, 0, nextBytes.length); 232 } 233 234 return outBytes.toByteArray(); 235 } 236 237 238 239 protected void extractFromBEREncoding(byte[] enc) 240 throws SNMPBadValueException 241 { 242 Vector newVector = new Vector(); 243 244 int totalLength = enc.length; 245 int position = 0; 246 247 while (position < totalLength) 248 { 249 SNMPTLV nextTLV = SNMPBERCodec.extractNextTLV(enc, position); 250 newVector.insertElementAt(SNMPBERCodec.extractEncoding(nextTLV), newVector.size()); 251 position += nextTLV.totalLength; 252 } 253 254 sequence = newVector; 255 256 } 257 258 259 260 261 262 266 267 public String toString() 268 { 269 StringBuffer valueStringBuffer = new StringBuffer ("("); 270 271 272 for (int i = 0; i < sequence.size(); ++i) 273 { 274 valueStringBuffer.append(" "); 275 valueStringBuffer.append(((SNMPObject)sequence.elementAt(i)).toString()); 276 valueStringBuffer.append(" "); 277 } 278 279 valueStringBuffer.append(")"); 280 return valueStringBuffer.toString(); 281 } 282 283 284 285 } | Popular Tags |