You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

HexDump.java 9.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.util;
  16. import java.io.IOException;
  17. import java.io.OutputStream;
  18. import java.io.OutputStreamWriter;
  19. import java.nio.charset.Charset;
  20. import java.nio.charset.StandardCharsets;
  21. /**
  22. * dump data in hexadecimal format
  23. */
  24. @Internal
  25. public final class HexDump {
  26. public static final String EOL = System.getProperty("line.separator");
  27. public static final Charset UTF8 = StandardCharsets.UTF_8;
  28. private HexDump() {
  29. // all static methods, so no need for a public constructor
  30. }
  31. /**
  32. * dump an array of bytes to an OutputStream
  33. *
  34. * @param data the byte array to be dumped
  35. * @param offset its offset, whatever that might mean
  36. * @param stream the OutputStream to which the data is to be
  37. * written
  38. * @param index initial index into the byte array
  39. * @param length number of characters to output
  40. *
  41. * @throws IOException is thrown if anything goes wrong writing
  42. * the data to stream
  43. * @throws ArrayIndexOutOfBoundsException if the index is
  44. * outside the data array's bounds
  45. * @throws IllegalArgumentException if the output stream is
  46. * null
  47. */
  48. public static void dump(final byte [] data, final long offset,
  49. final OutputStream stream, final int index, final int length)
  50. throws IOException, ArrayIndexOutOfBoundsException, IllegalArgumentException {
  51. if (stream == null) {
  52. throw new IllegalArgumentException("cannot write to nullstream");
  53. }
  54. OutputStreamWriter osw = new OutputStreamWriter(stream, UTF8);
  55. osw.write(dump(data, offset, index, length));
  56. osw.flush();
  57. }
  58. /**
  59. * dump an array of bytes to an OutputStream
  60. *
  61. * @param data the byte array to be dumped
  62. * @param offset its offset, whatever that might mean
  63. * @param stream the OutputStream to which the data is to be
  64. * written
  65. * @param index initial index into the byte array
  66. *
  67. * @throws IOException is thrown if anything goes wrong writing
  68. * the data to stream
  69. * @throws ArrayIndexOutOfBoundsException if the index is
  70. * outside the data array's bounds
  71. * @throws IllegalArgumentException if the output stream is
  72. * null
  73. */
  74. public synchronized static void dump(final byte [] data, final long offset,
  75. final OutputStream stream, final int index)
  76. throws IOException, ArrayIndexOutOfBoundsException, IllegalArgumentException {
  77. dump(data, offset, stream, index, Integer.MAX_VALUE);
  78. }
  79. /**
  80. * dump an array of bytes to a String
  81. *
  82. * @param data the byte array to be dumped
  83. * @param offset its offset, whatever that might mean
  84. * @param index initial index into the byte array
  85. *
  86. * @throws ArrayIndexOutOfBoundsException if the index is
  87. * outside the data array's bounds
  88. * @return output string
  89. */
  90. public static String dump(final byte [] data, final long offset, final int index) {
  91. return dump(data, offset, index, Integer.MAX_VALUE);
  92. }
  93. /**
  94. * dump an array of bytes to a String
  95. *
  96. * @param data the byte array to be dumped
  97. * @param offset its offset, whatever that might mean
  98. * @param index initial index into the byte array
  99. * @param length number of characters to output
  100. *
  101. * @throws ArrayIndexOutOfBoundsException if the index is
  102. * outside the data array's bounds
  103. * @return output string
  104. */
  105. public static String dump(final byte [] data, final long offset, final int index, final int length) {
  106. if (data == null || data.length == 0) {
  107. return "No Data"+EOL;
  108. }
  109. int data_length = (length == Integer.MAX_VALUE || length < 0 || index+length < 0)
  110. ? data.length
  111. : Math.min(data.length,index+length);
  112. if ((index < 0) || (index >= data.length)) {
  113. String err = "illegal index: "+index+" into array of length "+data.length;
  114. throw new ArrayIndexOutOfBoundsException(err);
  115. }
  116. long display_offset = offset + index;
  117. StringBuilder buffer = new StringBuilder(74);
  118. for (int j = index; j < data_length; j += 16) {
  119. int chars_read = data_length - j;
  120. if (chars_read > 16) {
  121. chars_read = 16;
  122. }
  123. writeHex(buffer, display_offset, 8, "");
  124. for (int k = 0; k < 16; k++) {
  125. if (k < chars_read) {
  126. writeHex(buffer, data[ k + j ], 2, " ");
  127. } else {
  128. buffer.append(" ");
  129. }
  130. }
  131. buffer.append(' ');
  132. for (int k = 0; k < chars_read; k++) {
  133. buffer.append(toAscii(data[ k + j ]));
  134. }
  135. buffer.append(EOL);
  136. display_offset += chars_read;
  137. }
  138. return buffer.toString();
  139. }
  140. public static char toAscii(int dataB) {
  141. char charB = (char)(dataB & 0xFF);
  142. if (Character.isISOControl(charB)) {
  143. return '.';
  144. }
  145. switch (charB) {
  146. // printable, but not compilable with current compiler encoding
  147. case 0xFF:
  148. case 0xDD:
  149. charB = '.';
  150. break;
  151. default:
  152. break;
  153. }
  154. return charB;
  155. }
  156. /**
  157. * Converts the parameter to a hex value.
  158. *
  159. * @param value The value to convert
  160. * @return A String representing the array of bytes
  161. */
  162. public static String toHex(final byte[] value)
  163. {
  164. StringBuilder retVal = new StringBuilder();
  165. retVal.append('[');
  166. if (value != null && value.length > 0)
  167. {
  168. for(int x = 0; x < value.length; x++)
  169. {
  170. if (x>0) {
  171. retVal.append(", ");
  172. }
  173. retVal.append(toHex(value[x]));
  174. }
  175. }
  176. retVal.append(']');
  177. return retVal.toString();
  178. }
  179. /**
  180. * Converts the parameter to a hex value.
  181. *
  182. * @param value The value to convert
  183. * @return The result right padded with 0
  184. */
  185. public static String toHex(short value) {
  186. StringBuilder sb = new StringBuilder(4);
  187. writeHex(sb, value & 0xFFFF, 4, "");
  188. return sb.toString();
  189. }
  190. /**
  191. * Converts the parameter to a hex value.
  192. *
  193. * @param value The value to convert
  194. * @return The result right padded with 0
  195. */
  196. public static String toHex(byte value) {
  197. StringBuilder sb = new StringBuilder(2);
  198. writeHex(sb, value & 0xFF, 2, "");
  199. return sb.toString();
  200. }
  201. /**
  202. * Converts the parameter to a hex value.
  203. *
  204. * @param value The value to convert
  205. * @return The result right padded with 0
  206. */
  207. public static String toHex(int value) {
  208. StringBuilder sb = new StringBuilder(8);
  209. writeHex(sb, value & 0xFFFFFFFFL, 8, "");
  210. return sb.toString();
  211. }
  212. /**
  213. * Converts the parameter to a hex value.
  214. *
  215. * @param value The value to convert
  216. * @return The result right padded with 0
  217. */
  218. public static String toHex(long value) {
  219. StringBuilder sb = new StringBuilder(16);
  220. writeHex(sb, value, 16, "");
  221. return sb.toString();
  222. }
  223. /**
  224. * @return string of 16 (zero padded) uppercase hex chars and prefixed with '0x'
  225. */
  226. public static String longToHex(long value) {
  227. StringBuilder sb = new StringBuilder(18);
  228. writeHex(sb, value, 16, "0x");
  229. return sb.toString();
  230. }
  231. /**
  232. * @return string of 8 (zero padded) uppercase hex chars and prefixed with '0x'
  233. */
  234. public static String intToHex(int value) {
  235. StringBuilder sb = new StringBuilder(10);
  236. writeHex(sb, value & 0xFFFFFFFFL, 8, "0x");
  237. return sb.toString();
  238. }
  239. /**
  240. * @return string of 4 (zero padded) uppercase hex chars and prefixed with '0x'
  241. */
  242. public static String shortToHex(int value) {
  243. StringBuilder sb = new StringBuilder(6);
  244. writeHex(sb, value & 0xFFFFL, 4, "0x");
  245. return sb.toString();
  246. }
  247. /**
  248. * @return string of 2 (zero padded) uppercase hex chars and prefixed with '0x'
  249. */
  250. public static String byteToHex(int value) {
  251. StringBuilder sb = new StringBuilder(4);
  252. writeHex(sb, value & 0xFFL, 2, "0x");
  253. return sb.toString();
  254. }
  255. /**
  256. * @see Integer#toHexString(int)
  257. * @see Long#toHexString(long)
  258. */
  259. private static void writeHex(StringBuilder sb, long value, int nDigits, String prefix) {
  260. sb.append(prefix);
  261. char[] buf = new char[nDigits];
  262. long acc = value;
  263. for(int i=nDigits-1; i>=0; i--) {
  264. int digit = Math.toIntExact(acc & 0x0F);
  265. buf[i] = (char) (digit < 10 ? ('0' + digit) : ('A' + digit - 10));
  266. acc >>>= 4;
  267. }
  268. sb.append(buf);
  269. }
  270. }