001: /*
002: * SNMP Package
003: *
004: * Copyright (C) 2004, Jonathan Sevy <jsevy@mcs.drexel.edu>
005: *
006: * This is free software. Redistribution and use in source and binary forms, with
007: * or without modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * 1. Redistributions of source code must retain the above copyright notice, this
011: * list of conditions and the following disclaimer.
012: * 2. Redistributions in binary form must reproduce the above copyright notice,
013: * this list of conditions and the following disclaimer in the documentation
014: * and/or other materials provided with the distribution.
015: * 3. The name of the author may not be used to endorse or promote products
016: * derived from this software without specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
019: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
020: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
021: * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
022: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
023: * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
024: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
025: * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
026: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
027: *
028: */
029:
030: package snmp;
031:
032: import java.io.*;
033:
034: /**
035: * Class representing a general string of octets.
036: */
037:
038: public class SNMPOctetString extends SNMPObject {
039: protected byte[] data;
040: protected byte tag = SNMPBERCodec.SNMPOCTETSTRING;
041:
042: /**
043: * Create a zero-length octet string.
044: */
045:
046: public SNMPOctetString() {
047: data = new byte[0];
048: }
049:
050: /**
051: * Create an octet string from the bytes of the supplied String.
052: */
053:
054: public SNMPOctetString(String stringData) {
055: this .data = stringData.getBytes();
056: }
057:
058: /**
059: * Create an octet string from the supplied byte array. The array may be either
060: * user-supplied, or part of a retrieved BER encoding. Note that the BER encoding
061: * of the data of an octet string is just the raw bytes.
062: */
063:
064: public SNMPOctetString(byte[] enc) {
065: extractFromBEREncoding(enc);
066: }
067:
068: /**
069: * Return the array of raw bytes.
070: */
071:
072: public Object getValue() {
073: return data;
074: }
075:
076: /**
077: * Used to set the value from a byte array.
078: * @throws SNMPBadValueException Indicates an incorrect object type supplied.
079: */
080:
081: public void setValue(Object data) throws SNMPBadValueException {
082: if (data instanceof byte[])
083: this .data = (byte[]) data;
084: else if (data instanceof String)
085: this .data = ((String) data).getBytes();
086: else
087: throw new SNMPBadValueException(
088: " Octet String: bad object supplied to set value ");
089: }
090:
091: /**
092: * Returns the BER encoding for the octet string. Note the the "value" part of the
093: * BER type,length,value triple is just the sequence of raw bytes.
094: */
095:
096: protected byte[] getBEREncoding() {
097:
098: ByteArrayOutputStream outBytes = new ByteArrayOutputStream();
099:
100: // calculate encoding for length of data
101: byte[] len = SNMPBERCodec.encodeLength(data.length);
102:
103: // encode T,L,V info
104: outBytes.write(tag);
105: outBytes.write(len, 0, len.length);
106: outBytes.write(data, 0, data.length);
107:
108: return outBytes.toByteArray();
109: }
110:
111: protected void extractFromBEREncoding(byte[] enc) {
112: data = new byte[enc.length];
113:
114: // copy data
115: for (int i = 0; i < enc.length; i++) {
116: data[i] = enc[i];
117: }
118: }
119:
120: /**
121: * Checks the embedded arrays for equality.
122: */
123:
124: public boolean equals(Object other) {
125: // false if other is null
126: if (other == null) {
127: return false;
128: }
129:
130: // check first to see that they're both of the same class
131: if (!this .getClass().equals(other.getClass())) {
132: return false;
133: }
134:
135: SNMPOctetString otherSNMPObject = (SNMPOctetString) other;
136:
137: // see if their embedded arrays are equal
138: if (java.util.Arrays.equals((byte[]) this .getValue(),
139: (byte[]) otherSNMPObject.getValue())) {
140: return true;
141: } else {
142: return false;
143: }
144: }
145:
146: /**
147: * Generates a hash value so SNMP Octet String subclasses can be used in Hashtables.
148: */
149:
150: public int hashCode() {
151: int hash = 0;
152:
153: // generate a hashcode from the embedded array
154: for (int i = 0; i < data.length; i++) {
155: hash += data[i];
156: hash += (hash << 10);
157: hash ^= (hash >> 6);
158: }
159:
160: hash += (hash << 3);
161: hash ^= (hash >> 11);
162: hash += (hash << 15);
163:
164: return hash;
165: }
166:
167: /**
168: * Returns a String constructed from the raw bytes. If the bytes contain non-printable
169: * ASCII characters, tant pis! (Though it's fun when the bell rings!)
170: */
171:
172: public String toString() {
173: String returnString;
174:
175: /*
176: if ((data.length == 4) || (data.length == 6))
177: {
178: returnString = new String();
179:
180: int convert = data[0];
181: if (convert < 0)
182: convert += 256;
183: returnString += convert;
184:
185: for (int i = 1; i < data.length; i++)
186: {
187: convert = data[i];
188: if (convert < 0)
189: convert += 256;
190: returnString += "." + convert;
191: }
192: }
193: else
194: returnString = new String(data);
195: */
196:
197: /*
198: byte[] converted = new byte[data.length];
199:
200: for (int i = 0; i < data.length; i++)
201: {
202: if (data[i] == 0)
203: converted[i] = 0x20; // space character
204: else
205: converted[i] = data[i];
206: }
207:
208: returnString = new String(converted);
209: */
210:
211: returnString = new String(data);
212:
213: return returnString;
214:
215: }
216:
217: private String hexByte(byte b) {
218: int pos = b;
219: if (pos < 0)
220: pos += 256;
221: String returnString = new String();
222: returnString += Integer.toHexString(pos / 16);
223: returnString += Integer.toHexString(pos % 16);
224: return returnString;
225: }
226:
227: /**
228: * Returns a space-separated hex string corresponding to the raw bytes.
229: */
230:
231: public String toHexString() {
232: StringBuffer returnStringBuffer = new StringBuffer();
233:
234: for (int i = 0; i < data.length; i++) {
235: returnStringBuffer.append(hexByte(data[i]) + " ");
236: }
237:
238: return returnStringBuffer.toString();
239:
240: }
241:
242: }
|