Source Code Cross Referenced for SNMPNSAPAddress.java in  » Net » Java-SNMP » snmp » 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 » Java SNMP » snmp 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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.util.*;
033:
034:        /**
035:         *    Defines class for holding physical 6-byte addresses.
036:         */
037:
038:        public class SNMPNSAPAddress extends SNMPOctetString {
039:            // length limited to 6 octets
040:
041:            /**
042:             *    Initialize address to 0.0.0.0.0.0.
043:             */
044:
045:            public SNMPNSAPAddress() {
046:                tag = SNMPBERCodec.SNMPNSAPADDRESS;
047:
048:                // initialize to 0.0.0.0.0.0
049:                data = new byte[6];
050:                for (int i = 0; i < 6; i++)
051:                    data[i] = 0;
052:            }
053:
054:            public SNMPNSAPAddress(String string) throws SNMPBadValueException {
055:                tag = SNMPBERCodec.SNMPNSAPADDRESS;
056:
057:                data = parseNSAPAddress(string);
058:            }
059:
060:            /** 
061:             *    Used to initialize from the BER encoding, as received in a response from 
062:             *     an SNMP device responding to an SNMPGetRequest, or from a supplied byte array
063:             *    containing the address components.
064:             *     @throws SNMPBadValueException Indicates an invalid array supplied: must have length 6.
065:             */
066:
067:            public SNMPNSAPAddress(byte[] enc) throws SNMPBadValueException {
068:                tag = SNMPBERCodec.SNMPNSAPADDRESS;
069:
070:                if (enc.length == 6) {
071:                    data = enc;
072:                } else // wrong size
073:                {
074:                    throw new SNMPBadValueException(
075:                            " NSAPAddress: bad BER encoding supplied to set value ");
076:                }
077:            }
078:
079:            /** 
080:             *    Used to set the value from a byte array containing the address.
081:             *     @throws SNMPBadValueException Indicates an incorrect object type supplied, or array of
082:             *    incorrect size.
083:             */
084:
085:            public void setValue(Object newAddress)
086:                    throws SNMPBadValueException {
087:                if ((newAddress instanceof  byte[])
088:                        && (((byte[]) newAddress).length == 6))
089:                    data = (byte[]) newAddress;
090:                else if (newAddress instanceof  String) {
091:                    data = parseNSAPAddress((String) newAddress);
092:                } else
093:                    throw new SNMPBadValueException(
094:                            " NSAPAddress: bad length byte string supplied to set value ");
095:            }
096:
097:            /** 
098:             *     Return pretty-printed (dash-separated) address.
099:             */
100:
101:            public String toString() {
102:                StringBuffer returnStringBuffer = new StringBuffer();
103:
104:                if (data.length > 0) {
105:                    int convert = data[0];
106:                    if (convert < 0)
107:                        convert += 256;
108:                    returnStringBuffer.append(Integer.toHexString(convert));
109:
110:                    for (int i = 1; i < data.length; i++) {
111:                        convert = data[i];
112:                        if (convert < 0)
113:                            convert += 256;
114:                        returnStringBuffer.append("-");
115:                        returnStringBuffer.append(Integer.toHexString(convert));
116:                    }
117:                }
118:
119:                return returnStringBuffer.toString();
120:            }
121:
122:            private byte[] parseNSAPAddress(String addressString)
123:                    throws SNMPBadValueException {
124:                try {
125:                    StringTokenizer st = new StringTokenizer(addressString,
126:                            " .-"); // break on spaces, dots or dashes
127:                    int size = 0;
128:
129:                    while (st.hasMoreTokens()) {
130:                        // figure out how many values are in string
131:                        size++;
132:                        st.nextToken();
133:                    }
134:
135:                    if (size != 6) {
136:                        throw new SNMPBadValueException(
137:                                " NSAPAddress: wrong number of components supplied to set value ");
138:                    }
139:
140:                    byte[] returnBytes = new byte[size];
141:
142:                    st = new StringTokenizer(addressString, " .-");
143:
144:                    for (int i = 0; i < size; i++) {
145:                        int addressComponent = (Integer.parseInt(
146:                                st.nextToken(), 16));
147:                        if ((addressComponent < 0) || (addressComponent > 255))
148:                            throw new SNMPBadValueException(
149:                                    " NSAPAddress: invalid component supplied to set value ");
150:                        returnBytes[i] = (byte) addressComponent;
151:                    }
152:
153:                    return returnBytes;
154:
155:                } catch (NumberFormatException e) {
156:                    throw new SNMPBadValueException(
157:                            " NSAPAddress: invalid component supplied to set value ");
158:                }
159:
160:            }
161:
162:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.