Source Code Cross Referenced for IpAddress.java in  » Net » snmp4j » org » snmp4j » smi » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Net » snmp4j » org.snmp4j.smi 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*_############################################################################
002:          _## 
003:          _##  SNMP4J - IpAddress.java  
004:          _## 
005:          _##  Copyright (C) 2003-2008  Frank Fock and Jochen Katz (SNMP4J.org)
006:          _##  
007:          _##  Licensed under the Apache License, Version 2.0 (the "License");
008:          _##  you may not use this file except in compliance with the License.
009:          _##  You may obtain a copy of the License at
010:          _##  
011:          _##      http://www.apache.org/licenses/LICENSE-2.0
012:          _##  
013:          _##  Unless required by applicable law or agreed to in writing, software
014:          _##  distributed under the License is distributed on an "AS IS" BASIS,
015:          _##  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016:          _##  See the License for the specific language governing permissions and
017:          _##  limitations under the License.
018:          _##  
019:          _##########################################################################*/
020:
021:        package org.snmp4j.smi;
022:
023:        import java.io.*;
024:        import java.net.*;
025:        import org.snmp4j.asn1.BER;
026:        import org.snmp4j.asn1.BERInputStream;
027:        import org.snmp4j.log.LogFactory;
028:        import org.snmp4j.log.LogAdapter;
029:
030:        /**
031:         * The <code>IpAddress</code> class represents an IPv4 address SNMP variable.
032:         *
033:         * @author Frank Fock
034:         * @version 1.7
035:         * @since 1.0
036:         */
037:        public class IpAddress extends SMIAddress {
038:
039:            private static final long serialVersionUID = -146846354059565449L;
040:
041:            private static final LogAdapter logger = LogFactory
042:                    .getLogger(AbstractVariable.class);
043:
044:            private static final byte[] IPANYADDRESS = { 0, 0, 0, 0 };
045:
046:            public static final InetAddress ANY_IPADDRESS = createAnyAddress();
047:
048:            private java.net.InetAddress inetAddress;
049:
050:            /**
051:             * Creates a <code>0.0.0.0</code> IP address.
052:             */
053:            public IpAddress() {
054:                this .inetAddress = ANY_IPADDRESS;
055:            }
056:
057:            /**
058:             * Creates an IP address from an <code>InetAddress</code>
059:             * @param address
060:             *    an <code>InetAddress</code> instance (must not necessarily be a IPv4
061:             *    address).
062:             */
063:            public IpAddress(InetAddress address) {
064:                if (address == null) {
065:                    throw new NullPointerException();
066:                }
067:                this .inetAddress = address;
068:            }
069:
070:            /**
071:             * Create an IP address from an address string.
072:             * @param address
073:             *    an IP address String.
074:             * @see Address#parseAddress(String address)
075:             */
076:            public IpAddress(String address) {
077:                if (!parseAddress(address)) {
078:                    throw new IllegalArgumentException(address);
079:                }
080:            }
081:
082:            public int getSyntax() {
083:                return SMIConstants.SYNTAX_IPADDRESS;
084:            }
085:
086:            public boolean isValid() {
087:                return (inetAddress != null);
088:            }
089:
090:            public String toString() {
091:                String addressString = inetAddress.toString();
092:                return addressString.substring(addressString.indexOf('/') + 1);
093:            }
094:
095:            public int hashCode() {
096:                if (inetAddress != null) {
097:                    return inetAddress.hashCode();
098:                }
099:                return 0;
100:            }
101:
102:            /**
103:             * Parses an IP address string and returns the corresponding
104:             * <code>IpAddress</code> instance.
105:             * @param address
106:             *    an IP address string which may be a host name or a numerical IP address.
107:             * @return
108:             *    an <code>IpAddress</code> instance or <code>null</code> if
109:             *    <code>address</code> cannot not be parsed.
110:             * @see Address#parseAddress(String address)
111:             */
112:            public static Address parse(String address) {
113:                try {
114:                    InetAddress addr = InetAddress.getByName(address);
115:                    return new IpAddress(addr);
116:                } catch (Exception ex) {
117:                    logger.error("Unable to parse IpAddress from: " + address,
118:                            ex);
119:                    return null;
120:                }
121:            }
122:
123:            public boolean parseAddress(String address) {
124:                try {
125:                    inetAddress = InetAddress.getByName(address);
126:                    return true;
127:                } catch (UnknownHostException uhex) {
128:                    return false;
129:                }
130:            }
131:
132:            public int compareTo(Object o) {
133:                OctetString a = new OctetString(inetAddress.getAddress());
134:                return a.compareTo(new OctetString(((IpAddress) o)
135:                        .getInetAddress().getAddress()));
136:            }
137:
138:            public boolean equals(Object o) {
139:                if (!(o instanceof  IpAddress)) {
140:                    return false;
141:                }
142:                return (compareTo(o) == 0);
143:            }
144:
145:            public void decodeBER(BERInputStream inputStream)
146:                    throws java.io.IOException {
147:                BER.MutableByte type = new BER.MutableByte();
148:                byte[] value = BER.decodeString(inputStream, type);
149:                if (type.getValue() != BER.IPADDRESS) {
150:                    throw new IOException(
151:                            "Wrong type encountered when decoding Counter: "
152:                                    + type.getValue());
153:                }
154:                if (value.length != 4) {
155:                    throw new IOException(
156:                            "IpAddress encoding error, wrong length: "
157:                                    + value.length);
158:                }
159:                inetAddress = InetAddress.getByAddress(value);
160:            }
161:
162:            public void encodeBER(OutputStream outputStream)
163:                    throws java.io.IOException {
164:                byte[] address = new byte[4];
165:                System.arraycopy(inetAddress.getAddress(), 0, address, 0, 4);
166:                BER.encodeString(outputStream, BER.IPADDRESS, inetAddress
167:                        .getAddress());
168:            }
169:
170:            public int getBERLength() {
171:                return 6;
172:            }
173:
174:            public void setAddress(byte[] rawValue) throws UnknownHostException {
175:                this .inetAddress = InetAddress.getByAddress(rawValue);
176:            }
177:
178:            public void setInetAddress(java.net.InetAddress inetAddress) {
179:                this .inetAddress = inetAddress;
180:            }
181:
182:            public InetAddress getInetAddress() {
183:                return inetAddress;
184:            }
185:
186:            private static InetAddress createAnyAddress() {
187:                try {
188:                    return InetAddress.getByAddress(IPANYADDRESS);
189:                } catch (Exception ex) {
190:                    logger.error("Unable to create any IpAddress: "
191:                            + ex.getMessage(), ex);
192:                }
193:                return null;
194:            }
195:
196:            public Object clone() {
197:                return new IpAddress(inetAddress);
198:            }
199:
200:            public int toInt() {
201:                throw new UnsupportedOperationException();
202:            }
203:
204:            public long toLong() {
205:                throw new UnsupportedOperationException();
206:            }
207:
208:            public OID toSubIndex(boolean impliedLength) {
209:                byte[] address = new byte[4];
210:                System.arraycopy(inetAddress.getAddress(), 0, address, 0, 4);
211:                OID subIndex = new OID(new int[4]);
212:                for (int i = 0; i < address.length; i++) {
213:                    subIndex.set(i, address[i] & 0xFF);
214:                }
215:                return subIndex;
216:            }
217:
218:            public void fromSubIndex(OID subIndex, boolean impliedLength) {
219:                byte[] rawValue = new byte[4];
220:                for (int i = 0; i < rawValue.length; i++) {
221:                    rawValue[i] = (byte) (subIndex.get(i) & 0xFF);
222:                }
223:                try {
224:                    setAddress(rawValue);
225:                } catch (UnknownHostException ex) {
226:                    ex.printStackTrace();
227:                }
228:            }
229:
230:            public void setValue(String value) {
231:                if (!parseAddress(value)) {
232:                    throw new IllegalArgumentException(value
233:                            + " cannot be parsed by " + getClass().getName());
234:                }
235:            }
236:
237:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.