001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.commons.io; 018 019import java.io.IOException; 020import java.io.OutputStream; 021import java.io.OutputStreamWriter; 022import java.nio.charset.Charset; 023import java.util.Objects; 024 025import org.apache.commons.io.output.CloseShieldOutputStream; 026 027/** 028 * Dumps data in hexadecimal format. 029 * <p> 030 * Provides a single function to take an array of bytes and display it 031 * in hexadecimal form. 032 * </p> 033 * <p> 034 * Provenance: POI. 035 * </p> 036 */ 037public class HexDump { 038 039 /** 040 * The line-separator (initializes to "line.separator" system property). 041 * 042 * @deprecated Use {@link System#lineSeparator()}. 043 */ 044 @Deprecated 045 public static final String EOL = System.lineSeparator(); 046 047 private static final char[] HEX_CODES = 048 { 049 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 050 'A', 'B', 'C', 'D', 'E', 'F' 051 }; 052 053 private static final int[] SHIFTS = 054 { 055 28, 24, 20, 16, 12, 8, 4, 0 056 }; 057 058 /** 059 * Dumps an array of bytes to an Appendable. The output is formatted 060 * for human inspection, with a hexadecimal offset followed by the 061 * hexadecimal values of the next 16 bytes of data and the printable ASCII 062 * characters (if any) that those bytes represent printed per each line 063 * of output. 064 * 065 * @param data the byte array to be dumped 066 * @param appendable the Appendable to which the data is to be written 067 * 068 * @throws IOException is thrown if anything goes wrong writing 069 * the data to appendable 070 * @throws NullPointerException if the output appendable is null 071 * 072 * @since 2.12.0 073 */ 074 public static void dump(final byte[] data, final Appendable appendable) 075 throws IOException { 076 dump(data, 0, appendable, 0, data.length); 077 } 078 079 /** 080 * Dumps an array of bytes to an Appendable. The output is formatted 081 * for human inspection, with a hexadecimal offset followed by the 082 * hexadecimal values of the next 16 bytes of data and the printable ASCII 083 * characters (if any) that those bytes represent printed per each line 084 * of output. 085 * <p> 086 * The offset argument specifies the start offset of the data array 087 * within a larger entity like a file or an incoming stream. For example, 088 * if the data array contains the third kibibyte of a file, then the 089 * offset argument should be set to 2048. The offset value printed 090 * at the beginning of each line indicates where in that larger entity 091 * the first byte on that line is located. 092 * </p> 093 * 094 * @param data the byte array to be dumped 095 * @param offset offset of the byte array within a larger entity 096 * @param appendable the Appendable to which the data is to be written 097 * @param index initial index into the byte array 098 * @param length number of bytes to dump from the array 099 * 100 * @throws IOException is thrown if anything goes wrong writing 101 * the data to appendable 102 * @throws ArrayIndexOutOfBoundsException if the index or length is 103 * outside the data array's bounds 104 * @throws NullPointerException if the output appendable is null 105 * 106 * @since 2.12.0 107 */ 108 public static void dump(final byte[] data, final long offset, 109 final Appendable appendable, final int index, 110 final int length) 111 throws IOException, ArrayIndexOutOfBoundsException { 112 Objects.requireNonNull(appendable, "appendable"); 113 if (index < 0 || index >= data.length) { 114 throw new ArrayIndexOutOfBoundsException( 115 "illegal index: " + index + " into array of length " 116 + data.length); 117 } 118 long display_offset = offset + index; 119 final StringBuilder buffer = new StringBuilder(74); 120 121 // TODO Use Objects.checkFromIndexSize(index, length, data.length) when upgrading to JDK9 122 if (length < 0 || index + length > data.length) { 123 throw new ArrayIndexOutOfBoundsException(String.format("Range [%s, %<s + %s) out of bounds for length %s", index, length, data.length)); 124 } 125 126 final int endIndex = index + length; 127 128 for (int j = index; j < endIndex; j += 16) { 129 int chars_read = endIndex - j; 130 131 if (chars_read > 16) { 132 chars_read = 16; 133 } 134 dump(buffer, display_offset).append(' '); 135 for (int k = 0; k < 16; k++) { 136 if (k < chars_read) { 137 dump(buffer, data[k + j]); 138 } else { 139 buffer.append(" "); 140 } 141 buffer.append(' '); 142 } 143 for (int k = 0; k < chars_read; k++) { 144 if (data[k + j] >= ' ' && data[k + j] < 127) { 145 buffer.append((char) data[k + j]); 146 } else { 147 buffer.append('.'); 148 } 149 } 150 buffer.append(System.lineSeparator()); 151 appendable.append(buffer); 152 buffer.setLength(0); 153 display_offset += chars_read; 154 } 155 } 156 157 /** 158 * Dumps an array of bytes to an OutputStream. The output is formatted 159 * for human inspection, with a hexadecimal offset followed by the 160 * hexadecimal values of the next 16 bytes of data and the printable ASCII 161 * characters (if any) that those bytes represent printed per each line 162 * of output. 163 * <p> 164 * The offset argument specifies the start offset of the data array 165 * within a larger entity like a file or an incoming stream. For example, 166 * if the data array contains the third kibibyte of a file, then the 167 * offset argument should be set to 2048. The offset value printed 168 * at the beginning of each line indicates where in that larger entity 169 * the first byte on that line is located. 170 * </p> 171 * <p> 172 * All bytes between the given index (inclusive) and the end of the 173 * data array are dumped. 174 * </p> 175 * 176 * @param data the byte array to be dumped 177 * @param offset offset of the byte array within a larger entity 178 * @param stream the OutputStream to which the data is to be 179 * written 180 * @param index initial index into the byte array 181 * 182 * @throws IOException is thrown if anything goes wrong writing 183 * the data to stream 184 * @throws ArrayIndexOutOfBoundsException if the index is 185 * outside the data array's bounds 186 * @throws NullPointerException if the output stream is null 187 */ 188 @SuppressWarnings("resource") // Caller closes stream 189 public static void dump(final byte[] data, final long offset, 190 final OutputStream stream, final int index) 191 throws IOException, ArrayIndexOutOfBoundsException { 192 Objects.requireNonNull(stream, "stream"); 193 194 try (OutputStreamWriter out = new OutputStreamWriter(CloseShieldOutputStream.wrap(stream), Charset.defaultCharset())) { 195 dump(data, offset, out, index, data.length - index); 196 } 197 } 198 199 /** 200 * Dumps a byte value into a StringBuilder. 201 * 202 * @param builder the StringBuilder to dump the value in 203 * @param value the byte value to be dumped 204 * @return StringBuilder containing the dumped value. 205 */ 206 private static StringBuilder dump(final StringBuilder builder, final byte value) { 207 for (int j = 0; j < 2; j++) { 208 builder.append(HEX_CODES[value >> SHIFTS[j + 6] & 15]); 209 } 210 return builder; 211 } 212 213 /** 214 * Dumps a long value into a StringBuilder. 215 * 216 * @param builder the StringBuilder to dump the value in 217 * @param value the long value to be dumped 218 * @return StringBuilder containing the dumped value. 219 */ 220 private static StringBuilder dump(final StringBuilder builder, final long value) { 221 for (int j = 0; j < 8; j++) { 222 builder.append(HEX_CODES[(int) (value >> SHIFTS[j]) & 15]); 223 } 224 return builder; 225 } 226 227 /** 228 * Instances should NOT be constructed in standard programming. 229 */ 230 public HexDump() { 231 } 232 233}