0N/A/*
2362N/A * Copyright (c) 1995, 1997, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A
0N/Apackage sun.misc;
0N/Aimport java.io.PrintStream;
0N/Aimport java.io.OutputStream;
0N/Aimport java.io.IOException;
0N/A
0N/A/**
0N/A * This class encodes a buffer into the classic: "Hexadecimal Dump" format of
0N/A * the past. It is useful for analyzing the contents of binary buffers.
0N/A * The format produced is as follows:
0N/A * <pre>
0N/A * xxxx: 00 11 22 33 44 55 66 77 88 99 aa bb cc dd ee ff ................
0N/A * </pre>
0N/A * Where xxxx is the offset into the buffer in 16 byte chunks, followed
0N/A * by ascii coded hexadecimal bytes followed by the ASCII representation of
0N/A * the bytes or '.' if they are not valid bytes.
0N/A *
0N/A * @author Chuck McManis
0N/A */
0N/A
0N/Apublic class HexDumpEncoder extends CharacterEncoder {
0N/A
0N/A private int offset;
0N/A private int thisLineLength;
0N/A private int currentByte;
0N/A private byte thisLine[] = new byte[16];
0N/A
0N/A static void hexDigit(PrintStream p, byte x) {
0N/A char c;
0N/A
0N/A c = (char) ((x >> 4) & 0xf);
0N/A if (c > 9)
0N/A c = (char) ((c-10) + 'A');
0N/A else
0N/A c = (char)(c + '0');
0N/A p.write(c);
0N/A c = (char) (x & 0xf);
0N/A if (c > 9)
0N/A c = (char)((c-10) + 'A');
0N/A else
0N/A c = (char)(c + '0');
0N/A p.write(c);
0N/A }
0N/A
0N/A protected int bytesPerAtom() {
0N/A return (1);
0N/A }
0N/A
0N/A protected int bytesPerLine() {
0N/A return (16);
0N/A }
0N/A
0N/A protected void encodeBufferPrefix(OutputStream o) throws IOException {
0N/A offset = 0;
0N/A super.encodeBufferPrefix(o);
0N/A }
0N/A
0N/A protected void encodeLinePrefix(OutputStream o, int len) throws IOException {
0N/A hexDigit(pStream, (byte)((offset >>> 8) & 0xff));
0N/A hexDigit(pStream, (byte)(offset & 0xff));
0N/A pStream.print(": ");
0N/A currentByte = 0;
0N/A thisLineLength = len;
0N/A }
0N/A
0N/A protected void encodeAtom(OutputStream o, byte buf[], int off, int len) throws IOException {
0N/A thisLine[currentByte] = buf[off];
0N/A hexDigit(pStream, buf[off]);
0N/A pStream.print(" ");
0N/A currentByte++;
0N/A if (currentByte == 8)
0N/A pStream.print(" ");
0N/A }
0N/A
0N/A protected void encodeLineSuffix(OutputStream o) throws IOException {
0N/A if (thisLineLength < 16) {
0N/A for (int i = thisLineLength; i < 16; i++) {
0N/A pStream.print(" ");
0N/A if (i == 7)
0N/A pStream.print(" ");
0N/A }
0N/A }
0N/A pStream.print(" ");
0N/A for (int i = 0; i < thisLineLength; i++) {
0N/A if ((thisLine[i] < ' ') || (thisLine[i] > 'z')) {
0N/A pStream.print(".");
0N/A } else {
0N/A pStream.write(thisLine[i]);
0N/A }
0N/A }
0N/A pStream.println();
0N/A offset += thisLineLength;
0N/A }
0N/A
0N/A}