001: //
002: // Copyright 1998 CDS Networks, Inc., Medford Oregon
003: //
004: // All rights reserved.
005: //
006: // Redistribution and use in source and binary forms, with or without
007: // modification, are permitted provided that the following conditions are met:
008: // 1. Redistributions of source code must retain the above copyright
009: // notice, this list of conditions and the following disclaimer.
010: // 2. Redistributions in binary form must reproduce the above copyright
011: // notice, this list of conditions and the following disclaimer in the
012: // documentation and/or other materials provided with the distribution.
013: // 3. All advertising materials mentioning features or use of this software
014: // must display the following acknowledgement:
015: // This product includes software developed by CDS Networks, Inc.
016: // 4. The name of CDS Networks, Inc. may not be used to endorse or promote
017: // products derived from this software without specific prior
018: // written permission.
019: //
020: // THIS SOFTWARE IS PROVIDED BY CDS NETWORKS, INC. ``AS IS'' AND
021: // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
022: // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
023: // ARE DISCLAIMED. IN NO EVENT SHALL CDS NETWORKS, INC. BE LIABLE
024: // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
025: // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
026: // OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
027: // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
028: // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
029: // OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
030: // SUCH DAMAGE.
031: //
032:
033: package com.internetcds.util;
034:
035: /**
036: * A simple class to convert a raw buffer to a hex dump
037: *
038: * @version $Id: HexDump.java,v 1.2 2007-10-19 13:23:55 sinisa Exp $
039: * @author Craig Spannring
040: */
041: public class HexDump {
042: public static final String cvsVersion = "$Id: HexDump.java,v 1.2 2007-10-19 13:23:55 sinisa Exp $";
043:
044: static String byteToHexString(byte b) {
045: return intToHexString(b, 2, '0');
046: } /* byteToHexString() */
047:
048: static String intToHexString(int num, int width, char fill) {
049: String result = "";
050: int i;
051:
052: if (num == 0) {
053: result = "0";
054: width--;
055: } else {
056: while (num != 0 && width > 0) {
057: String tmp = Integer.toHexString(num & 0xf);
058: result = tmp + result;
059: num = (num >> 4);
060: width--;
061: }
062: }
063: for (; width > 0; width--) {
064: result = fill + result;
065: }
066: return result;
067: } /* intToHexString() */
068:
069: public static String hexDump(byte data[]) {
070: return hexDump(data, data.length);
071: }
072:
073: public static String hexDump(byte data[], int length) {
074: String str;
075: int i;
076: int j;
077: final int bytesPerLine = 16;
078: String result = "";
079:
080: for (i = 0; i < length; i += bytesPerLine) {
081: // print the offset as a 4 digit hex number
082: result = result + intToHexString(i, 4, '0') + " ";
083:
084: // print each byte in hex
085: for (j = i; j < length && (j - i) < bytesPerLine; j++) {
086: result = result + byteToHexString(data[j]) + " ";
087: }
088:
089: // skip over to the ascii dump column
090: for (; 0 != (j % bytesPerLine); j++) {
091: result = result + " ";
092: }
093: result = result + " |";
094:
095: // print each byte in ascii
096: for (j = i; j < length && (j - i) < bytesPerLine; j++) {
097: if (((data[j] & 0xff) > 0x001f)
098: && ((data[j] & 0xff) < 0x007f)) {
099: Character ch = new Character((char) data[j]);
100: result = result + ch;
101: } else {
102: result = result + ".";
103: }
104: }
105: result = result + "|\n";
106: }
107: return result;
108: } /* hexDump() */
109: }
|