1 16 package org.apache.commons.io; 17 18 import java.io.IOException ; 19 20 import org.apache.commons.io.output.ByteArrayOutputStream; 21 22 import junit.framework.TestCase; 23 24 25 30 31 public class HexDumpTest extends TestCase { 32 33 38 39 public HexDumpTest(String name) { 40 super(name); 41 } 42 43 private char toHex(int n) { 44 char[] hexChars = 45 { 46 '0', '1', '2', '3', '4', '5', '6', '7', 47 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' 48 }; 49 50 return hexChars[n % 16]; 51 } 52 53 58 59 public void testDump() 60 throws IOException { 61 byte[] testArray = new byte[256]; 62 63 for (int j = 0; j < 256; j++) { 64 testArray[j] = (byte) j; 65 } 66 ByteArrayOutputStream stream = new ByteArrayOutputStream(); 67 68 HexDump.dump(testArray, 0, stream, 0); 69 byte[] outputArray = new byte[16 * (73 + HexDump.EOL.length())]; 70 71 for (int j = 0; j < 16; j++) { 72 int offset = (73 + HexDump.EOL.length()) * j; 73 74 outputArray[offset++] = (byte) '0'; 75 outputArray[offset++] = (byte) '0'; 76 outputArray[offset++] = (byte) '0'; 77 outputArray[offset++] = (byte) '0'; 78 outputArray[offset++] = (byte) '0'; 79 outputArray[offset++] = (byte) '0'; 80 outputArray[offset++] = (byte) toHex(j); 81 outputArray[offset++] = (byte) '0'; 82 outputArray[offset++] = (byte) ' '; 83 for (int k = 0; k < 16; k++) { 84 outputArray[offset++] = (byte) toHex(j); 85 outputArray[offset++] = (byte) toHex(k); 86 outputArray[offset++] = (byte) ' '; 87 } 88 for (int k = 0; k < 16; k++) { 89 outputArray[offset++] = (byte) toAscii((j * 16) + k); 90 } 91 System.arraycopy(HexDump.EOL.getBytes(), 0, outputArray, offset, 92 HexDump.EOL.getBytes().length); 93 } 94 byte[] actualOutput = stream.toByteArray(); 95 96 assertEquals("array size mismatch", outputArray.length, 97 actualOutput.length); 98 for (int j = 0; j < outputArray.length; j++) { 99 assertEquals("array[ " + j + "] mismatch", outputArray[j], 100 actualOutput[j]); 101 } 102 103 stream = new ByteArrayOutputStream(); 105 HexDump.dump(testArray, 0x10000000, stream, 0); 106 outputArray = new byte[16 * (73 + HexDump.EOL.length())]; 107 for (int j = 0; j < 16; j++) { 108 int offset = (73 + HexDump.EOL.length()) * j; 109 110 outputArray[offset++] = (byte) '1'; 111 outputArray[offset++] = (byte) '0'; 112 outputArray[offset++] = (byte) '0'; 113 outputArray[offset++] = (byte) '0'; 114 outputArray[offset++] = (byte) '0'; 115 outputArray[offset++] = (byte) '0'; 116 outputArray[offset++] = (byte) toHex(j); 117 outputArray[offset++] = (byte) '0'; 118 outputArray[offset++] = (byte) ' '; 119 for (int k = 0; k < 16; k++) { 120 outputArray[offset++] = (byte) toHex(j); 121 outputArray[offset++] = (byte) toHex(k); 122 outputArray[offset++] = (byte) ' '; 123 } 124 for (int k = 0; k < 16; k++) { 125 outputArray[offset++] = (byte) toAscii((j * 16) + k); 126 } 127 System.arraycopy(HexDump.EOL.getBytes(), 0, outputArray, offset, 128 HexDump.EOL.getBytes().length); 129 } 130 actualOutput = stream.toByteArray(); 131 assertEquals("array size mismatch", outputArray.length, 132 actualOutput.length); 133 for (int j = 0; j < outputArray.length; j++) { 134 assertEquals("array[ " + j + "] mismatch", outputArray[j], 135 actualOutput[j]); 136 } 137 138 stream = new ByteArrayOutputStream(); 140 HexDump.dump(testArray, 0xFF000000, stream, 0); 141 outputArray = new byte[16 * (73 + HexDump.EOL.length())]; 142 for (int j = 0; j < 16; j++) { 143 int offset = (73 + HexDump.EOL.length()) * j; 144 145 outputArray[offset++] = (byte) 'F'; 146 outputArray[offset++] = (byte) 'F'; 147 outputArray[offset++] = (byte) '0'; 148 outputArray[offset++] = (byte) '0'; 149 outputArray[offset++] = (byte) '0'; 150 outputArray[offset++] = (byte) '0'; 151 outputArray[offset++] = (byte) toHex(j); 152 outputArray[offset++] = (byte) '0'; 153 outputArray[offset++] = (byte) ' '; 154 for (int k = 0; k < 16; k++) { 155 outputArray[offset++] = (byte) toHex(j); 156 outputArray[offset++] = (byte) toHex(k); 157 outputArray[offset++] = (byte) ' '; 158 } 159 for (int k = 0; k < 16; k++) { 160 outputArray[offset++] = (byte) toAscii((j * 16) + k); 161 } 162 System.arraycopy(HexDump.EOL.getBytes(), 0, outputArray, offset, 163 HexDump.EOL.getBytes().length); 164 } 165 actualOutput = stream.toByteArray(); 166 assertEquals("array size mismatch", outputArray.length, 167 actualOutput.length); 168 for (int j = 0; j < outputArray.length; j++) { 169 assertEquals("array[ " + j + "] mismatch", outputArray[j], 170 actualOutput[j]); 171 } 172 173 stream = new ByteArrayOutputStream(); 175 HexDump.dump(testArray, 0x10000000, stream, 0x81); 176 outputArray = new byte[(8 * (73 + HexDump.EOL.length())) - 1]; 177 for (int j = 0; j < 8; j++) { 178 int offset = (73 + HexDump.EOL.length()) * j; 179 180 outputArray[offset++] = (byte) '1'; 181 outputArray[offset++] = (byte) '0'; 182 outputArray[offset++] = (byte) '0'; 183 outputArray[offset++] = (byte) '0'; 184 outputArray[offset++] = (byte) '0'; 185 outputArray[offset++] = (byte) '0'; 186 outputArray[offset++] = (byte) toHex(j + 8); 187 outputArray[offset++] = (byte) '1'; 188 outputArray[offset++] = (byte) ' '; 189 for (int k = 0; k < 16; k++) { 190 int index = 0x81 + (j * 16) + k; 191 192 if (index < 0x100) { 193 outputArray[offset++] = (byte) toHex(index / 16); 194 outputArray[offset++] = (byte) toHex(index); 195 } else { 196 outputArray[offset++] = (byte) ' '; 197 outputArray[offset++] = (byte) ' '; 198 } 199 outputArray[offset++] = (byte) ' '; 200 } 201 for (int k = 0; k < 16; k++) { 202 int index = 0x81 + (j * 16) + k; 203 204 if (index < 0x100) { 205 outputArray[offset++] = (byte) toAscii(index); 206 } 207 } 208 System.arraycopy(HexDump.EOL.getBytes(), 0, outputArray, offset, 209 HexDump.EOL.getBytes().length); 210 } 211 actualOutput = stream.toByteArray(); 212 assertEquals("array size mismatch", outputArray.length, 213 actualOutput.length); 214 for (int j = 0; j < outputArray.length; j++) { 215 assertEquals("array[ " + j + "] mismatch", outputArray[j], 216 actualOutput[j]); 217 } 218 219 try { 221 HexDump.dump(testArray, 0x10000000, new ByteArrayOutputStream(), 222 -1); 223 fail("should have caught ArrayIndexOutOfBoundsException on negative index"); 224 } catch (ArrayIndexOutOfBoundsException ignored_exception) { 225 226 } 228 229 try { 231 HexDump.dump(testArray, 0x10000000, new ByteArrayOutputStream(), 232 testArray.length); 233 fail("should have caught ArrayIndexOutOfBoundsException on large index"); 234 } catch (ArrayIndexOutOfBoundsException ignored_exception) { 235 236 } 238 239 try { 241 HexDump.dump(testArray, 0x10000000, null, 0); 242 fail("should have caught IllegalArgumentException on negative index"); 243 } catch (IllegalArgumentException ignored_exception) { 244 245 } 247 } 248 249 private char toAscii(int c) { 250 char rval = '.'; 251 252 if ((c >= 32) && (c <= 126)) { 253 rval = (char) c; 254 } 255 return rval; 256 } 257 258 263 264 public static void main(String [] ignored_args) { 265 System.out.println("Testing io.HexDump functionality"); 266 junit.textui.TestRunner.run(HexDumpTest.class); 267 } 268 } 269 | Popular Tags |