1 /*
2  * Copyright (c) 1995, 1997, Oracle and/or its affiliates. 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.  Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 
26 
27 package sun.misc;
28 import java.io.PrintStream;
29 import java.io.OutputStream;
30 import java.io.IOException;
31 
32 /**
33  * This class encodes a buffer into the classic: "Hexadecimal Dump" format of
34  * the past. It is useful for analyzing the contents of binary buffers.
35  * The format produced is as follows:
36  * <pre>
37  * xxxx: 00 11 22 33 44 55 66 77   88 99 aa bb cc dd ee ff ................
38  * </pre>
39  * Where xxxx is the offset into the buffer in 16 byte chunks, followed
40  * by ascii coded hexadecimal bytes followed by the ASCII representation of
41  * the bytes or '.' if they are not valid bytes.
42  *
43  * @author      Chuck McManis
44  */
45 
46 public class HexDumpEncoder extends CharacterEncoder {
47 
48     private int offset;
49     private int thisLineLength;
50     private int currentByte;
51     private byte thisLine[] = new byte[16];
52 
hexDigit(PrintStream p, byte x)53     static void hexDigit(PrintStream p, byte x) {
54         char c;
55 
56         c = (char) ((x >> 4) & 0xf);
57         if (c > 9)
58             c = (char) ((c-10) + 'A');
59         else
60             c = (char)(c + '0');
61         p.write(c);
62         c = (char) (x & 0xf);
63         if (c > 9)
64             c = (char)((c-10) + 'A');
65         else
66             c = (char)(c + '0');
67         p.write(c);
68     }
69 
bytesPerAtom()70     protected int bytesPerAtom() {
71         return (1);
72     }
73 
bytesPerLine()74     protected int bytesPerLine() {
75         return (16);
76     }
77 
encodeBufferPrefix(OutputStream o)78     protected void encodeBufferPrefix(OutputStream o) throws IOException {
79         offset = 0;
80         super.encodeBufferPrefix(o);
81     }
82 
encodeLinePrefix(OutputStream o, int len)83     protected void encodeLinePrefix(OutputStream o, int len) throws IOException {
84         hexDigit(pStream, (byte)((offset >>> 8) & 0xff));
85         hexDigit(pStream, (byte)(offset & 0xff));
86         pStream.print(": ");
87         currentByte = 0;
88         thisLineLength = len;
89     }
90 
encodeAtom(OutputStream o, byte buf[], int off, int len)91     protected void encodeAtom(OutputStream o, byte buf[], int off, int len) throws IOException {
92         thisLine[currentByte] = buf[off];
93         hexDigit(pStream, buf[off]);
94         pStream.print(" ");
95         currentByte++;
96         if (currentByte == 8)
97             pStream.print("  ");
98     }
99 
encodeLineSuffix(OutputStream o)100     protected void encodeLineSuffix(OutputStream o) throws IOException {
101         if (thisLineLength < 16) {
102             for (int i = thisLineLength; i < 16; i++) {
103                 pStream.print("   ");
104                 if (i == 7)
105                     pStream.print("  ");
106             }
107         }
108         pStream.print(" ");
109         for (int i = 0; i < thisLineLength; i++) {
110             if ((thisLine[i] < ' ') || (thisLine[i] > 'z')) {
111                 pStream.print(".");
112             } else {
113                 pStream.write(thisLine[i]);
114             }
115         }
116         pStream.println();
117         offset += thisLineLength;
118     }
119 
120 }
121