Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/misc/HexDumpEncoder.java
38829 views
/*1* Copyright (c) 1995, 1997, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/242526package sun.misc;27import java.io.PrintStream;28import java.io.OutputStream;29import java.io.IOException;3031/**32* This class encodes a buffer into the classic: "Hexadecimal Dump" format of33* the past. It is useful for analyzing the contents of binary buffers.34* The format produced is as follows:35* <pre>36* xxxx: 00 11 22 33 44 55 66 77 88 99 aa bb cc dd ee ff ................37* </pre>38* Where xxxx is the offset into the buffer in 16 byte chunks, followed39* by ascii coded hexadecimal bytes followed by the ASCII representation of40* the bytes or '.' if they are not valid bytes.41*42* @author Chuck McManis43*/4445public class HexDumpEncoder extends CharacterEncoder {4647private int offset;48private int thisLineLength;49private int currentByte;50private byte thisLine[] = new byte[16];5152static void hexDigit(PrintStream p, byte x) {53char c;5455c = (char) ((x >> 4) & 0xf);56if (c > 9)57c = (char) ((c-10) + 'A');58else59c = (char)(c + '0');60p.write(c);61c = (char) (x & 0xf);62if (c > 9)63c = (char)((c-10) + 'A');64else65c = (char)(c + '0');66p.write(c);67}6869protected int bytesPerAtom() {70return (1);71}7273protected int bytesPerLine() {74return (16);75}7677protected void encodeBufferPrefix(OutputStream o) throws IOException {78offset = 0;79super.encodeBufferPrefix(o);80}8182protected void encodeLinePrefix(OutputStream o, int len) throws IOException {83hexDigit(pStream, (byte)((offset >>> 8) & 0xff));84hexDigit(pStream, (byte)(offset & 0xff));85pStream.print(": ");86currentByte = 0;87thisLineLength = len;88}8990protected void encodeAtom(OutputStream o, byte buf[], int off, int len) throws IOException {91thisLine[currentByte] = buf[off];92hexDigit(pStream, buf[off]);93pStream.print(" ");94currentByte++;95if (currentByte == 8)96pStream.print(" ");97}9899protected void encodeLineSuffix(OutputStream o) throws IOException {100if (thisLineLength < 16) {101for (int i = thisLineLength; i < 16; i++) {102pStream.print(" ");103if (i == 7)104pStream.print(" ");105}106}107pStream.print(" ");108for (int i = 0; i < thisLineLength; i++) {109if ((thisLine[i] < ' ') || (thisLine[i] > 'z')) {110pStream.print(".");111} else {112pStream.write(thisLine[i]);113}114}115pStream.println();116offset += thisLineLength;117}118119}120121122