KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snmp4j > smi > TransportIpAddress


1 /*_############################################################################
2   _##
3   _## SNMP4J - TransportIpAddress.java
4   _##
5   _## Copyright 2003-2007 Frank Fock and Jochen Katz (SNMP4J.org)
6   _##
7   _## Licensed under the Apache License, Version 2.0 (the "License");
8   _## you may not use this file except in compliance with the License.
9   _## You may obtain a copy of the License at
10   _##
11   _## http://www.apache.org/licenses/LICENSE-2.0
12   _##
13   _## Unless required by applicable law or agreed to in writing, software
14   _## distributed under the License is distributed on an "AS IS" BASIS,
15   _## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   _## See the License for the specific language governing permissions and
17   _## limitations under the License.
18   _##
19   _##########################################################################*/

20
21
22
23 package org.snmp4j.smi;
24
25 import java.util.StringTokenizer JavaDoc;
26 import java.io.IOException JavaDoc;
27 import org.snmp4j.asn1.BERInputStream;
28 import java.io.OutputStream JavaDoc;
29 import java.net.InetAddress JavaDoc;
30 import java.net.UnknownHostException JavaDoc;
31 import org.snmp4j.log.LogFactory;
32 import org.snmp4j.log.LogAdapter;
33
34 /**
35  * The <code>TransportIpAddress</code> is the abstract base class for all
36  * transport addresses on top of IP network addresses.
37  *
38  * @author Frank Fock
39  * @version 1.5
40  */

41 public abstract class TransportIpAddress extends IpAddress {
42
43   private static final LogAdapter logger =
44       LogFactory.getLogger(TransportIpAddress.class);
45
46   static final long serialVersionUID = 695596530250216972L;
47
48   protected int port = 0;
49
50   public int getPort() {
51     return port;
52   }
53
54   public void setPort(int port) {
55     if ((port < 0) || (port > 65535)) {
56       throw new IllegalArgumentException JavaDoc("Illegal port specified: " + port);
57     }
58     this.port = port;
59   }
60
61   public boolean isValid() {
62     return super.isValid() && (port >= 0) && (port <= 65535);
63   }
64
65   public int compareTo(Object JavaDoc o) {
66     int result = super.compareTo(o);
67     if (result == 0) {
68       return (port - ((TransportIpAddress) o).getPort());
69     }
70     return result;
71   }
72
73   public boolean equals(Object JavaDoc o) {
74     if (o instanceof TransportIpAddress) {
75       return (super.equals(o) && ((TransportIpAddress)o).getPort() == port);
76     }
77     return false;
78   }
79
80   public boolean parseAddress(String JavaDoc address) {
81     try {
82       StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(address, "/");
83       String JavaDoc addr = st.nextToken();
84       String JavaDoc port = st.nextToken();
85       if (super.parseAddress(addr)) {
86         this.port = Integer.parseInt(port);
87         return true;
88       }
89       return false;
90     }
91     catch (Exception JavaDoc ex) {
92       return false;
93     }
94   }
95
96   public static Address parse(String JavaDoc address) {
97     try {
98       UdpAddress a = new UdpAddress();
99       if (a.parseAddress(address)) {
100         return a;
101       }
102     }
103     catch (Exception JavaDoc ex) {
104     }
105     return null;
106   }
107
108   public String JavaDoc toString() {
109     return super.toString()+"/"+port;
110   }
111
112   public int hashCode() {
113     return super.hashCode()^2 + port;
114   }
115
116   /**
117    * Sets this transport address from an OcetString containing the address
118    * value in format as specified by the TRANSPORT-ADDRESS-MIB.
119    * @param transportAddress
120    * an OctetString containing the IP address bytes and the two port bytes
121    * in network byte order.
122    * @throws UnknownHostException
123    * if the address is invalid.
124    */

125   public void setTransportAddress(OctetString transportAddress) throws
126       UnknownHostException JavaDoc {
127     OctetString inetAddr =
128         transportAddress.substring(0, transportAddress.length()-2);
129     setInetAddress(InetAddress.getByAddress(inetAddr.getValue()));
130     port = ((transportAddress.get(transportAddress.length()-2) & 0xFF) << 8);
131     port += (transportAddress.get(transportAddress.length()-1) & 0xFF);
132   }
133
134   /**
135    * Returns the address value as a byte array.
136    * @return
137    * a byte array with IP address bytes and two additional bytes containing
138    * the port in network byte order.
139    * @since 1.5
140    */

141   public byte[] getValue() {
142     byte[] addr = getInetAddress().getAddress();
143     byte[] retval = new byte[addr.length+2];
144     System.arraycopy(addr, 0, retval, 0, addr.length);
145     retval[addr.length] = (byte)((port >> 8) & 0xFF);
146     retval[addr.length+1] = (byte)(port & 0xFF);
147     return retval;
148   }
149
150   public void decodeBER(BERInputStream inputStream) throws IOException JavaDoc {
151     OctetString os = new OctetString();
152     os.decodeBER(inputStream);
153     try {
154       setTransportAddress(os);
155     }
156     catch (Exception JavaDoc ex) {
157       String JavaDoc txt = "Wrong encoding of TransportAddress";
158       logger.error(txt);
159       throw new IOException JavaDoc(txt+": "+ex.getMessage());
160     }
161   }
162
163   public void encodeBER(OutputStream JavaDoc outputStream) throws IOException JavaDoc {
164     OctetString os = new OctetString(getValue());
165     os.encodeBER(outputStream);
166   }
167
168   public int getBERLength() {
169     return getInetAddress().getAddress().length + 2;
170   }
171
172   public int getBERPayloadLength() {
173     return getBERLength();
174   }
175
176   public int getSyntax() {
177     return SMIConstants.SYNTAX_OCTET_STRING;
178   }
179
180 }
181
Popular Tags