1 20 21 package org.snmp4j; 22 23 import org.snmp4j.smi.OctetString; 24 import org.snmp4j.asn1.BER; 25 import java.io.OutputStream ; 26 import java.io.IOException ; 27 import org.snmp4j.asn1.BERInputStream; 28 import org.snmp4j.asn1.BER.MutableByte; 29 30 36 37 public class ScopedPDU extends PDU { 38 39 private static final long serialVersionUID = 4343157159110407279L; 40 41 private OctetString contextEngineID = new OctetString(); 42 private OctetString contextName = new OctetString(); 43 44 47 public ScopedPDU() { 48 } 49 50 55 public ScopedPDU(ScopedPDU other) { 56 super(other); 57 this.contextEngineID = (OctetString) other.contextEngineID.clone(); 58 this.contextName = (OctetString) other.contextName.clone(); 59 } 60 61 67 public void setContextEngineID(OctetString contextEngineID) { 68 if (contextEngineID == null) { 69 throw new NullPointerException ("Context engine ID must not be null"); 70 } 71 this.contextEngineID = contextEngineID; 72 } 73 74 79 public OctetString getContextEngineID() { 80 return contextEngineID; 81 } 82 83 88 public void setContextName(OctetString contextName) { 89 if (contextName == null) { 90 throw new NullPointerException ("Context name must not be null"); 91 } 92 this.contextName = contextName; 93 } 94 95 100 public OctetString getContextName() { 101 return contextName; 102 } 103 104 public int getBERLength() { 105 int length = getBERPayloadLength(); 106 length += 1 + BER.getBERLengthOfLength(length); 107 return length; 108 } 109 110 public int getBERPayloadLength() { 111 int length = super.getBERLength(); 112 int cid = (contextEngineID == null) ? 0 : contextEngineID.length(); 113 int cn = (contextName == null) ? 0 : contextName.length(); 114 length += BER.getBERLengthOfLength(cid) + 1 115 + cid + BER.getBERLengthOfLength(cn) + 1 + cn; 116 return length; 117 } 118 119 public void encodeBER(OutputStream outputStream) throws IOException { 120 BER.encodeHeader(outputStream, BER.SEQUENCE, getBERPayloadLength()); 121 contextEngineID.encodeBER(outputStream); 122 contextName.encodeBER(outputStream); 123 super.encodeBER(outputStream); 124 } 125 126 127 128 public Object clone() { 129 return new ScopedPDU(this); 130 } 131 132 139 public void decodeBER(BERInputStream inputStream) throws IOException { 140 MutableByte mutableByte = new MutableByte(); 141 int length = BER.decodeHeader(inputStream, mutableByte); 142 long startPos = inputStream.getPosition(); 143 contextEngineID.decodeBER(inputStream); 144 contextName.decodeBER(inputStream); 145 super.decodeBER(inputStream); 146 if (BER.isCheckSequenceLength()) { 147 BER.checkSequenceLength(length, 148 (int) (inputStream.getPosition() - startPos), 149 this); 150 } 151 } 152 153 158 public String toString() { 159 StringBuffer buf = new StringBuffer (); 160 buf.append(getTypeString(type)); 161 buf.append("[reqestID="); 162 buf.append(requestID); 163 buf.append(", errorStatus="); 164 buf.append(errorStatus); 165 buf.append(", errorIndex="); 166 buf.append(errorIndex); 167 buf.append(", VBS["); 168 for (int i = 0; i < variableBindings.size(); i++) { 169 buf.append(variableBindings.get(i)); 170 if (i + 1 < variableBindings.size()) { 171 buf.append("; "); 172 } 173 } 174 buf.append("]]"); 175 return buf.toString(); 176 } 177 178 } 179 | Popular Tags |