001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.sql.framework.ui.utils;
042:
043: public class BinaryToStringConverter {
044:
045: static class ConversionConstants {
046: int radix; // the base radix
047: int width; // number of chars used to represent byte
048:
049: ConversionConstants(int w, int r) {
050: width = w;
051: radix = r;
052: }
053: }
054:
055: public static final int BINARY = 2;
056: public static final int DECIMAL = 10;
057: public static final int HEX = 16;
058:
059: public static final int OCTAL = 8;
060:
061: static ConversionConstants decimal = new ConversionConstants(3, 10);
062: static ConversionConstants hex = new ConversionConstants(2, 16);
063: private static ConversionConstants binary = new ConversionConstants(
064: 8, 2);
065: private static ConversionConstants octal = new ConversionConstants(
066: 3, 8);
067:
068: /**
069: * List of characters considered "printable".
070: */
071: private static String printable = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ`~!@#$%^&*()-_=+[{]}\\|;:'\",<.>/?";
072:
073: /**
074: * Convert from an array of Bytes into a string.
075: */
076: public static String convertToString(Byte[] data, int base,
077: boolean showAscii) {
078:
079: if (data == null) {
080: return null;
081: }
082:
083: StringBuilder buf = new StringBuilder(20);
084: ConversionConstants convConst = getConstants(base);
085:
086: // Convert each byte and put into string buffer
087: for (int i = 0; i < data.length; i++) {
088: int value = data[i].byteValue();
089: String s = null;
090:
091: // if user wants to see ASCII chars as characters,
092: // see if this is one that should be displayed that way
093: if (showAscii) {
094: if (printable.indexOf((char) value) > -1) {
095: s = new Character((char) value)
096: + " "
097: .substring(10 - (convConst.width - 1));
098: }
099: }
100:
101: // if user is not looking for ASCII chars, or if this one is one that
102: // is not printable, then convert it into numeric form
103: if (s == null) {
104: switch (base) {
105: case DECIMAL:
106: // convert signed to unsigned
107: if (value < 0) {
108: value = 256 + value;
109: }
110: s = Integer.toString(value);
111: break;
112: case OCTAL:
113: s = Integer.toOctalString(value);
114: break;
115: case BINARY:
116: s = Integer.toBinaryString(value);
117: break;
118: case HEX: // fall through to default
119: default:
120: s = Integer.toHexString(value);
121: }
122: // some formats (e.g. hex & octal) extend a negative number to multiple
123: // places (e.g. FC becomes FFFC), so chop off extra stuff in front
124: if (s.length() > convConst.width)
125: s = s.substring(s.length() - convConst.width);
126:
127: // front pad with zeros and add to output
128: if (s.length() < convConst.width) {
129: buf.append("00000000"
130: .substring(8 - (convConst.width - s
131: .length())));
132: }
133: }
134: buf.append(s);
135: buf.append(" "); // always add spaces at end for consistancy
136: }
137: return buf.toString();
138: }
139:
140: /**
141: * Get the constants to use for the given base.
142: */
143: private static ConversionConstants getConstants(int base) {
144: switch (base) {
145: case DECIMAL:
146: return decimal;
147: case OCTAL:
148: return octal;
149: case BINARY:
150: return binary;
151: case HEX: // default to hex if unknown base passed in
152: default:
153: return hex;
154: }
155: }
156:
157: /**
158: * Do not allow any instances to be created.
159: */
160: private BinaryToStringConverter() {
161: }
162: }
|