/modules/enterprise/comm/src/main/java/org/rhq/enterprise/communications/util/DumpBytes.java

https://github.com/ccrouch/rhq · Java · 373 lines · 137 code · 36 blank · 200 comment · 23 complexity · 1d59fd81348d8b991726d59b79c4497f MD5 · raw file

  1. /*
  2. * RHQ Management Platform
  3. * Copyright (C) 2005-2008 Red Hat, Inc.
  4. * All rights reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation version 2 of the License.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. */
  19. package org.rhq.enterprise.communications.util;
  20. import java.io.File;
  21. import java.io.FileInputStream;
  22. /**
  23. * Dumps hexadecimal, decimal, octal and binary representations of any given File, String or byte array. For example,
  24. * the different representations for the string "helloworld" will show:
  25. *
  26. * <p>Hexadecimal:</p>
  27. *
  28. * <PRE>
  29. * 68 65 6c 6c 6f 77 6f 72 6c 64 helloworld
  30. * </PRE>
  31. *
  32. * <p>Decimal:</p>
  33. *
  34. * <PRE>
  35. * 104 101 108 108 111 119 111 114 hellowor
  36. * 108 100 ld
  37. * </PRE>
  38. *
  39. * <p>Octal:</p>
  40. *
  41. * <PRE>
  42. * 150 145 154 154 157 167 hellow
  43. * 157 162 154 144 orld
  44. * </PRE>
  45. *
  46. * <p>Binary:</p>
  47. *
  48. * <PRE>
  49. * 1101000 1100101 1101100 1101100 1101111 hello
  50. * 1110111 1101111 1110010 1101100 1100100 world
  51. * </PRE>
  52. *
  53. * @author John Mazzitelli
  54. */
  55. public class DumpBytes {
  56. /**
  57. * Hexadecimal base (16).
  58. */
  59. public static final int BASE_HEX = 16;
  60. /**
  61. * Decimal base (10).
  62. */
  63. public static final int BASE_DEC = 10;
  64. /**
  65. * Octal base (8).
  66. */
  67. public static final int BASE_OCT = 8;
  68. /**
  69. * Binary base (2).
  70. */
  71. public static final int BASE_BIN = 2;
  72. /**
  73. * Converts the given byte array to a numerical format representation.
  74. *
  75. * @param bytes data to be converted to a particular numerical format
  76. * @param cols number of columns the output data will have
  77. * @param format numerical base the output will be shown as - e.g. {@link #BASE_HEX} is hex
  78. *
  79. * @return formatted representation of the given byte array.
  80. *
  81. * @throws IllegalArgumentException if format is invalid
  82. */
  83. public static String dumpData(byte[] bytes, int cols, int format) {
  84. int num_col_width;
  85. int char_col_width;
  86. StringBuffer result = new StringBuffer(1024);
  87. String nums = "";
  88. String chars = "";
  89. String byte_str;
  90. if ((format < 2) || (format > 16)) {
  91. throw new IllegalArgumentException("format=" + format);
  92. } else if (format < 3) // leave enough room for 8 binary digits and a space
  93. {
  94. num_col_width = 9;
  95. char_col_width = 1;
  96. } else if (format < 8) {
  97. num_col_width = 6;
  98. char_col_width = 1;
  99. } else if (format < 10) // leave enough room for 4 octal digits and a space
  100. {
  101. num_col_width = 5;
  102. char_col_width = 1;
  103. } else if (format < 16) // leave enough room for 3 decimal digits and a space
  104. {
  105. num_col_width = 4;
  106. char_col_width = 1;
  107. } else { // leave enough room for 2 hexadecimal digits and a space
  108. num_col_width = 3;
  109. char_col_width = 1;
  110. }
  111. for (int i = 0; i < bytes.length; i++) {
  112. if (((i % cols) == 0) && (i != 0)) {
  113. result.append(nums);
  114. result.append(" ");
  115. result.append(chars);
  116. result.append('\n');
  117. nums = "";
  118. chars = "";
  119. }
  120. byte_str = Integer.toString(bytes[i] & 0x000000FF, format);
  121. nums += padFront(byte_str, num_col_width);
  122. chars += padFront(toCharString(bytes[i]), char_col_width);
  123. }
  124. result.append(padBack(nums, (cols * num_col_width)));
  125. result.append(" ");
  126. result.append(padBack(chars, (cols * char_col_width)));
  127. result.append('\n');
  128. return result.toString();
  129. }
  130. /**
  131. * Converts the given String to a numerical format representation.
  132. *
  133. * @param data String to be converted to a particular numerical format
  134. * @param cols number of columns the output data will have
  135. * @param format numerical base the output will be shown as (e.g. 16 is hex)
  136. *
  137. * @return Formatted representation of the given String.
  138. */
  139. public static String dumpData(String data, int cols, int format) {
  140. return dumpData(data.getBytes(), cols, format);
  141. }
  142. /**
  143. * Reads the given File and converts its contents to a numerical format representation.
  144. *
  145. * @param file File whose contents are to be converted to a particular numerical format
  146. * @param cols number of columns the output data will have
  147. * @param format numerical base the output will be shown as (e.g. 16 is hex)
  148. *
  149. * @return formatted representation of the given String or null if the file was not readable.
  150. */
  151. public static String dumpData(File file, int cols, int format) {
  152. FileInputStream fis;
  153. byte[] file_contents;
  154. int num_bytes;
  155. try {
  156. file_contents = new byte[(int) file.length()];
  157. fis = new FileInputStream(file);
  158. try {
  159. num_bytes = fis.read(file_contents);
  160. } finally {
  161. fis.close();
  162. }
  163. if (num_bytes != file_contents.length) {
  164. throw new IllegalStateException(num_bytes + "!=" + file_contents.length);
  165. }
  166. return dumpData(file_contents, cols, format);
  167. } catch (Exception e) {
  168. return null;
  169. }
  170. }
  171. /**
  172. * Converts the contents of the given File to binary format.
  173. *
  174. * @param f file whose contents is to be converted
  175. *
  176. * @return Formatted representation of the file contents
  177. */
  178. public static String dumpBinData(File f) {
  179. return dumpData(f, 7, BASE_BIN);
  180. }
  181. /**
  182. * Converts the given byte array to binary format.
  183. *
  184. * @param data byte array to be converted
  185. *
  186. * @return Formatted representation of data
  187. */
  188. public static String dumpBinData(byte[] data) {
  189. return dumpData(data, 7, BASE_BIN);
  190. }
  191. /**
  192. * Converts the given String to binary format.
  193. *
  194. * @param data String to be converted
  195. *
  196. * @return Formatted representation of data
  197. */
  198. public static String dumpBinData(String data) {
  199. return dumpData(data, 7, BASE_BIN);
  200. }
  201. /**
  202. * Converts the contents of the given File to octal format.
  203. *
  204. * @param f file whose contents is to be converted
  205. *
  206. * @return Formatted representation of the file contents
  207. */
  208. public static String dumpOctData(File f) {
  209. return dumpData(f, 9, BASE_OCT);
  210. }
  211. /**
  212. * Converts the given byte array to octal format.
  213. *
  214. * @param data byte array to be converted
  215. *
  216. * @return Formatted representation of data
  217. */
  218. public static String dumpOctData(byte[] data) {
  219. return dumpData(data, 9, BASE_OCT);
  220. }
  221. /**
  222. * Converts the given String to octal format.
  223. *
  224. * @param data String to be converted
  225. *
  226. * @return Formatted representation of data
  227. */
  228. public static String dumpOctData(String data) {
  229. return dumpData(data, 9, BASE_OCT);
  230. }
  231. /**
  232. * Converts the contents of the given File to decimal format.
  233. *
  234. * @param f file whose contents is to be converted
  235. *
  236. * @return Formatted representation of the file contents
  237. */
  238. public static String dumpDecData(File f) {
  239. return dumpData(f, 12, BASE_DEC);
  240. }
  241. /**
  242. * Converts the given byte array to decimal format.
  243. *
  244. * @param data byte array to be converted
  245. *
  246. * @return Formatted representation of data
  247. */
  248. public static String dumpDecData(byte[] data) {
  249. return dumpData(data, 12, BASE_DEC);
  250. }
  251. /**
  252. * Converts the given String to decimal format.
  253. *
  254. * @param data String to be converted
  255. *
  256. * @return Formatted representation of data
  257. */
  258. public static String dumpDecData(String data) {
  259. return dumpData(data, 12, BASE_DEC);
  260. }
  261. /**
  262. * Converts the contents of the given File to hexadecimal format.
  263. *
  264. * @param f file whose contents is to be converted
  265. *
  266. * @return Formatted representation of the file contents
  267. */
  268. public static String dumpHexData(File f) {
  269. return dumpData(f, 15, BASE_HEX);
  270. }
  271. /**
  272. * Converts the given byte array to hexadecimal format.
  273. *
  274. * @param data byte array to be converted
  275. *
  276. * @return Formatted representation of data
  277. */
  278. public static String dumpHexData(byte[] data) {
  279. return dumpData(data, 15, BASE_HEX);
  280. }
  281. /**
  282. * Converts the given String to hexadecimal format.
  283. *
  284. * @param data String to be converted
  285. *
  286. * @return Formatted representation of data
  287. */
  288. public static String dumpHexData(String data) {
  289. return dumpData(data, 15, BASE_HEX);
  290. }
  291. /**
  292. * Appends whitespace to the end of the given string.
  293. *
  294. * @param s string to append with whitespace
  295. * @param len number of spaces to append
  296. *
  297. * @return appended string
  298. */
  299. private static String padBack(String s, int len) {
  300. String spaces = "";
  301. for (int i = 0; i < (len - s.length()); i++) {
  302. spaces += " ";
  303. }
  304. return (s + spaces);
  305. }
  306. /**
  307. * Prepends whitespace to the given string.
  308. *
  309. * @param s string to prepend whitespace
  310. * @param len number of spaces to prepend
  311. *
  312. * @return string with prepended whitespace
  313. */
  314. private static String padFront(String s, int len) {
  315. String spaces = "";
  316. for (int i = 0; i < (len - s.length()); i++) {
  317. spaces += " ";
  318. }
  319. return (spaces + s);
  320. }
  321. /**
  322. * Returns a string representation of the given byte. Whitespace will be represented with a " " and control
  323. * characters will be represented with a "." character.
  324. *
  325. * @param b byte to convert to a string
  326. *
  327. * @return string version of the given byte
  328. */
  329. private static String toCharString(byte b) {
  330. if (Character.isWhitespace((char) b)) {
  331. return " ";
  332. } else if (Character.isISOControl((char) b)) {
  333. return ".";
  334. }
  335. return "" + (char) b;
  336. }
  337. }