001: /*
002: * @(#)HexDumpEncoder.java 1.18 06/10/10
003: *
004: * Copyright 1990-2006 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: *
026: */
027:
028: package sun.misc;
029:
030: import java.io.PrintStream;
031: import java.io.OutputStream;
032: import java.io.IOException;
033:
034: /**
035: * This class encodes a buffer into the classic: "Hexadecimal Dump" format of
036: * the past. It is useful for analyzing the contents of binary buffers.
037: * The format produced is as follows:
038: * <pre>
039: * xxxx: 00 11 22 33 44 55 66 77 88 99 aa bb cc dd ee ff ................
040: * </pre>
041: * Where xxxx is the offset into the buffer in 16 byte chunks, followed
042: * by ascii coded hexadecimal bytes followed by the ASCII representation of
043: * the bytes or '.' if they are not valid bytes.
044: *
045: * @version 1.12, 02/02/00
046: * @author Chuck McManis
047: */
048:
049: public class HexDumpEncoder extends CharacterEncoder {
050:
051: private int offset;
052: private int this LineLength;
053: private int currentByte;
054: private byte this Line[] = new byte[16];
055:
056: static void hexDigit(PrintStream p, byte x) {
057: char c;
058:
059: c = (char) ((x >> 4) & 0xf);
060: if (c > 9)
061: c = (char) ((c - 10) + 'A');
062: else
063: c = (char) (c + '0');
064: p.write(c);
065: c = (char) (x & 0xf);
066: if (c > 9)
067: c = (char) ((c - 10) + 'A');
068: else
069: c = (char) (c + '0');
070: p.write(c);
071: }
072:
073: protected int bytesPerAtom() {
074: return (1);
075: }
076:
077: protected int bytesPerLine() {
078: return (16);
079: }
080:
081: protected void encodeBufferPrefix(OutputStream o)
082: throws IOException {
083: offset = 0;
084: super .encodeBufferPrefix(o);
085: }
086:
087: protected void encodeLinePrefix(OutputStream o, int len)
088: throws IOException {
089: hexDigit(pStream, (byte) ((offset >>> 8) & 0xff));
090: hexDigit(pStream, (byte) (offset & 0xff));
091: pStream.print(": ");
092: currentByte = 0;
093: this LineLength = len;
094: }
095:
096: protected void encodeAtom(OutputStream o, byte buf[], int off,
097: int len) throws IOException {
098: this Line[currentByte] = buf[off];
099: hexDigit(pStream, buf[off]);
100: pStream.print(" ");
101: currentByte++;
102: if (currentByte == 8)
103: pStream.print(" ");
104: }
105:
106: protected void encodeLineSuffix(OutputStream o) throws IOException {
107: if (this LineLength < 16) {
108: for (int i = this LineLength; i < 16; i++) {
109: pStream.print(" ");
110: if (i == 7)
111: pStream.print(" ");
112: }
113: }
114: pStream.print(" ");
115: for (int i = 0; i < this LineLength; i++) {
116: if ((this Line[i] < ' ') || (this Line[i] > 'z')) {
117: pStream.print(".");
118: } else {
119: pStream.write(thisLine[i]);
120: }
121: }
122: pStream.println();
123: offset += thisLineLength;
124: }
125:
126: }
|