1 20 21 package org.snmp4j.smi; 22 23 import java.io.Serializable ; 24 import org.snmp4j.asn1.*; 25 import java.io.IOException ; 26 import java.io.OutputStream ; 27 28 35 public class VariableBinding 36 implements Serializable , BERSerializable, Cloneable { 37 38 private static final long serialVersionUID = 1032709950031514113L; 39 40 private OID oid; 41 private Variable variable; 42 43 44 47 public VariableBinding() { 48 oid = new OID(); 49 this.variable = new Null(); 50 } 51 52 58 public VariableBinding(OID oid) { 59 setOid(oid); 60 this.variable = new Null(); 61 } 62 63 70 public VariableBinding(OID oid, Variable variable) { 71 setOid(oid); 72 setVariable(variable); 73 } 74 75 80 public OID getOid() { 81 return oid; 82 } 83 84 89 public void setOid(OID oid) { 90 if (oid == null) { 91 throw new IllegalArgumentException ( 92 "OID of a VariableBinding must not be null"); 93 } 94 this.oid = oid; 95 } 96 97 102 public void setVariable(Variable variable) { 103 if (variable == null) { 104 throw new IllegalArgumentException ( 105 "Variable of a VariableBinding must not be null"); 106 } 107 this.variable = variable; 108 } 109 110 115 public Variable getVariable() { 116 return variable; 117 } 118 119 124 public final int getSyntax() { 125 return variable.getSyntax(); 126 } 127 128 140 public boolean isException() { 141 return variable.isException(); 142 } 143 144 public final int getBERPayloadLength() { 145 return oid.getBERLength() + variable.getBERLength(); 146 } 147 148 public final int getBERLength() { 149 int length = getBERPayloadLength(); 150 length += BER.getBERLengthOfLength(length) + 1; 152 return length; 153 } 154 155 public final void decodeBER(BERInputStream inputStream) throws IOException { 156 BER.MutableByte type = new BER.MutableByte(); 157 int length = BER.decodeHeader(inputStream, type); 158 long startPos = inputStream.getPosition(); 159 if (type.getValue() != BER.SEQUENCE) { 160 throw new IOException ("Invalid sequence encoding: " + type.getValue()); 161 } 162 oid.decodeBER(inputStream); 163 variable = AbstractVariable.createFromBER(inputStream); 164 if (BER.isCheckSequenceLength()) { 165 BER.checkSequenceLength(length, 166 (int) (inputStream.getPosition() - startPos), 167 this); 168 } 169 } 170 171 public final void encodeBER(OutputStream outputStream) throws IOException { 172 int length = getBERPayloadLength(); 173 BER.encodeHeader(outputStream, BER.SEQUENCE, 174 length); 175 oid.encodeBER(outputStream); 176 variable.encodeBER(outputStream); 177 } 178 179 184 public String toString() { 185 return oid.toString()+" = "+variable; 186 } 187 188 public Object clone() { 189 return new VariableBinding((OID)oid.clone(), (Variable)variable.clone()); 190 } 191 } 192 193 | Popular Tags |