1. /*
  2. * Copyright 2002,2004 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.apache.commons.io;
  17. import java.io.IOException;
  18. import java.io.OutputStream;
  19. /**
  20. * Dumps data in hexadecimal format.
  21. *
  22. * Derived from a HexDump utility I wrote in June 2001.
  23. *
  24. * Taken from the POI project.
  25. *
  26. * @author Scott Sanders (sanders at apache dot org)
  27. * @author Marc Johnson
  28. * @version $Revision: 1.8 $ $Date: 2004/02/23 04:35:59 $
  29. */
  30. public class HexDump {
  31. /**
  32. * Instances should NOT be constructed in standard programming.
  33. */
  34. public HexDump() { }
  35. /**
  36. * dump an array of bytes to an OutputStream
  37. *
  38. * @param data the byte array to be dumped
  39. * @param offset its offset, whatever that might mean
  40. * @param stream the OutputStream to which the data is to be
  41. * written
  42. * @param index initial index into the byte array
  43. *
  44. * @exception IOException is thrown if anything goes wrong writing
  45. * the data to stream
  46. * @exception ArrayIndexOutOfBoundsException if the index is
  47. * outside the data array's bounds
  48. * @exception IllegalArgumentException if the output stream is
  49. * null
  50. */
  51. public static void dump(byte[] data, long offset,
  52. OutputStream stream, int index)
  53. throws IOException, ArrayIndexOutOfBoundsException,
  54. IllegalArgumentException {
  55. if ((index < 0) || (index >= data.length)) {
  56. throw new ArrayIndexOutOfBoundsException(
  57. "illegal index: " + index + " into array of length "
  58. + data.length);
  59. }
  60. if (stream == null) {
  61. throw new IllegalArgumentException("cannot write to nullstream");
  62. }
  63. long display_offset = offset + index;
  64. StringBuffer buffer = new StringBuffer(74);
  65. for (int j = index; j < data.length; j += 16) {
  66. int chars_read = data.length - j;
  67. if (chars_read > 16) {
  68. chars_read = 16;
  69. }
  70. buffer.append(dump(display_offset)).append(' ');
  71. for (int k = 0; k < 16; k++) {
  72. if (k < chars_read) {
  73. buffer.append(dump(data[k + j]));
  74. } else {
  75. buffer.append(" ");
  76. }
  77. buffer.append(' ');
  78. }
  79. for (int k = 0; k < chars_read; k++) {
  80. if ((data[k + j] >= ' ') && (data[k + j] < 127)) {
  81. buffer.append((char) data[k + j]);
  82. } else {
  83. buffer.append('.');
  84. }
  85. }
  86. buffer.append(EOL);
  87. stream.write(buffer.toString().getBytes());
  88. stream.flush();
  89. buffer.setLength(0);
  90. display_offset += chars_read;
  91. }
  92. }
  93. /** line-separator (initializes to "line.separator" system property. */
  94. public static final String EOL =
  95. System.getProperty("line.separator");
  96. private static final StringBuffer _lbuffer = new StringBuffer(8);
  97. private static final StringBuffer _cbuffer = new StringBuffer(2);
  98. private static final char _hexcodes[] =
  99. {
  100. '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D',
  101. 'E', 'F'
  102. };
  103. private static final int _shifts[] =
  104. {
  105. 28, 24, 20, 16, 12, 8, 4, 0
  106. };
  107. private static StringBuffer dump(long value) {
  108. _lbuffer.setLength(0);
  109. for (int j = 0; j < 8; j++) {
  110. _lbuffer
  111. .append(_hexcodes[((int) (value >> _shifts[j])) & 15]);
  112. }
  113. return _lbuffer;
  114. }
  115. private static StringBuffer dump(byte value) {
  116. _cbuffer.setLength(0);
  117. for (int j = 0; j < 2; j++) {
  118. _cbuffer.append(_hexcodes[(value >> _shifts[j + 6]) & 15]);
  119. }
  120. return _cbuffer;
  121. }
  122. }