blob: 2cda4b207c4966ff62acc67a68ee1637f461fcc2 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2001-2006 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25package com.sun.jmx.snmp.internal;
26
27import com.sun.jmx.snmp.SnmpDefinitions;
28/**
29 * Utility class used to deal with various data representations.
30 * <p><b>This API is a Sun Microsystems internal API and is subject
31 * to change without notice.</b></p>
32 * @since 1.5
33 */
34public class SnmpTools implements SnmpDefinitions {
35
36 /**
37 * Translates a binary representation in an ASCII one. The returned string is an hexadecimal string starting with 0x.
38 * @param data Binary to translate.
39 * @return Translated binary.
40 */
41 static public String binary2ascii(byte[] data, int length)
42 {
43 if(data == null) return null;
44 final int size = (length * 2) + 2;
45 byte[] asciiData = new byte[size];
46 asciiData[0] = (byte) '0';
47 asciiData[1] = (byte) 'x';
48 for (int i=0; i < length; i++) {
49 int j = i*2;
50 int v = (data[i] & 0xf0);
51 v = v >> 4;
52 if (v < 10)
53 asciiData[j+2] = (byte) ('0' + v);
54 else
55 asciiData[j+2] = (byte) ('A' + (v - 10));
56 v = ((data[i] & 0xf));
57 if (v < 10)
58 asciiData[j+1+2] = (byte) ('0' + v);
59 else
60 asciiData[j+1+2] = (byte) ('A' + (v - 10));
61 }
62 return new String(asciiData);
63 }
64
65 /**
66 * Translates a binary representation in an ASCII one. The returned string is an hexadecimal string starting with 0x.
67 * @param data Binary to translate.
68 * @return Translated binary.
69 */
70 static public String binary2ascii(byte[] data)
71 {
72 return binary2ascii(data, data.length);
73 }
74 /**
75 * Translates a stringified representation in a binary one. The passed string is an hexadecimal one starting with 0x.
76 * @param str String to translate.
77 * @return Translated string.
78 */
79 static public byte[] ascii2binary(String str) {
80 if(str == null) return null;
81 String val = str.substring(2);
82
83 int size = val.length();
84 byte []buf = new byte[size/2];
85 byte []p = val.getBytes();
86
87 for(int i = 0; i < (size / 2); i++)
88 {
89 int j = i * 2;
90 byte v = 0;
91 if (p[j] >= '0' && p[j] <= '9') {
92 v = (byte) ((p[j] - '0') << 4);
93 }
94 else if (p[j] >= 'a' && p[j] <= 'f') {
95 v = (byte) ((p[j] - 'a' + 10) << 4);
96 }
97 else if (p[j] >= 'A' && p[j] <= 'F') {
98 v = (byte) ((p[j] - 'A' + 10) << 4);
99 }
100 else
101 throw new Error("BAD format :" + str);
102
103 if (p[j+1] >= '0' && p[j+1] <= '9') {
104 //System.out.println("ascii : " + p[j+1]);
105 v += (p[j+1] - '0');
106 //System.out.println("binary : " + v);
107 }
108 else if (p[j+1] >= 'a' && p[j+1] <= 'f') {
109 //System.out.println("ascii : " + p[j+1]);
110 v += (p[j+1] - 'a' + 10);
111 //System.out.println("binary : " + v+1);
112 }
113 else if (p[j+1] >= 'A' && p[j+1] <= 'F') {
114 //System.out.println("ascii : " + p[j+1]);
115 v += (p[j+1] - 'A' + 10);
116 //System.out.println("binary : " + v);
117 }
118 else
119 throw new Error("BAD format :" + str);
120
121 buf[i] = v;
122 }
123 return buf;
124 }
125}