|
| 1 | +import java.io.*; |
| 2 | +import java.util.*; |
| 3 | + |
| 4 | +//---------------------------------------------------------------------------- |
| 5 | +/** |
| 6 | + * Hex view of part of a file. |
| 7 | + * |
| 8 | + * <p> |
| 9 | + * Written by CHEN Qingcan, Spring 2020, Foshan China <br> |
| 10 | + * Open source under WTFPL (Do What The Fuck You Want To Public License) http://www.wtfpl.net |
| 11 | + * |
| 12 | + * <p> |
| 13 | + * Run as script via Java 11: <br> |
| 14 | + * <code> |
| 15 | + * java FilePartHex.java |
| 16 | + * </code> |
| 17 | + */ |
| 18 | +public final class FilePartHex { |
| 19 | + |
| 20 | + static final BufferedReader stdin = new BufferedReader (new InputStreamReader (System.in)); |
| 21 | + static final PrintStream stdout = System.out; |
| 22 | + static final PrintStream stderr = System.err; |
| 23 | + static final String HR = "--------"; |
| 24 | + |
| 25 | + static Config config = null; |
| 26 | + static class Config { |
| 27 | + String where; |
| 28 | + Integer offset = 0; |
| 29 | + Integer length = 0x10 * 0x10; |
| 30 | + |
| 31 | + //-------------------------------------------------------------------- |
| 32 | + static Config getInstance (final String... args) throws IOException { |
| 33 | + return args.length > 0 ? fromArgs (args) : fromStdIn (); |
| 34 | + } |
| 35 | + |
| 36 | + //-------------------------------------------------------------------- |
| 37 | + /** Get configuration from command line arguments. */ |
| 38 | + static Config fromArgs (final String... args) { |
| 39 | + var config = new Config (); |
| 40 | + if (args.length >= 1) { |
| 41 | + config.where = args[0]; |
| 42 | + } |
| 43 | + try { |
| 44 | + if (args.length >= 2) { |
| 45 | + config.offset = Integer.valueOf (args[1]); |
| 46 | + } |
| 47 | + if (args.length >= 3) { |
| 48 | + config.length = Integer.valueOf (args[2]); |
| 49 | + } |
| 50 | + } catch (NumberFormatException e) { |
| 51 | + stderr.println ("Argument offset and length should be a number."); |
| 52 | + } |
| 53 | + return config; |
| 54 | + } |
| 55 | + |
| 56 | + //-------------------------------------------------------------------- |
| 57 | + /** Get configuration from standard input. */ |
| 58 | + private static Config fromStdIn () throws IOException { |
| 59 | + var config = new Config (); |
| 60 | + config.where = stdinLine ("", "File path: "); |
| 61 | + try { config.offset = Integer.valueOf (stdinLine ("0", "Offset (default to 0): ")); } |
| 62 | + catch (NumberFormatException e) { /* ignore */ } |
| 63 | + try { config.length = Integer.valueOf (stdinLine ("256", "Length (default to 256): ")); } |
| 64 | + catch (NumberFormatException e) { /* ignore */ } |
| 65 | + return config; |
| 66 | + } |
| 67 | + |
| 68 | + //-------------------------------------------------------------------- |
| 69 | + /** Prompt and then read a line from standard input. Return default value if empty input. */ |
| 70 | + static String stdinLine (final String defaultLine, final String format, final Object... args) |
| 71 | + throws IOException { |
| 72 | + stdout.printf (format, args); |
| 73 | + String line = stdin.readLine ().trim (); |
| 74 | + return line.length () > 0 ? line : defaultLine; |
| 75 | + } |
| 76 | + |
| 77 | + } |
| 78 | + |
| 79 | + //------------------------------------------------------------------------ |
| 80 | + /** Program entry */ |
| 81 | + public static void main (final String... args) { |
| 82 | + try { |
| 83 | + config = Config.getInstance (args); |
| 84 | + var r = input (); |
| 85 | + output (r); |
| 86 | + } catch (IOException e) { |
| 87 | + stderr.println (e.getMessage ()); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + //------------------------------------------------------------------------ |
| 92 | + /** Read bytes from file */ |
| 93 | + private static byte[] input () throws FileNotFoundException, IOException { |
| 94 | + try (var in = new FileInputStream (config.where)) { |
| 95 | + var r = new byte [config.length]; |
| 96 | + in.skip (config.offset); |
| 97 | + int n = in.read (r, 0, config.length); |
| 98 | + return Arrays.copyOf (r, n > 0 ? n : 0); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + //------------------------------------------------------------------------ |
| 103 | + /** Bytes in hex to standard output. */ |
| 104 | + private static void output (final byte[] r) { |
| 105 | + // table header |
| 106 | + stdout.println (config.where); |
| 107 | + stdout.printf ("offset %d (%X in hex) length %d (%X in hex)%n", |
| 108 | + config.offset, config.offset, config.length, config.length); |
| 109 | + |
| 110 | + // column header |
| 111 | + stdout.print ("\t "); |
| 112 | + for (int i = 0 ; i < 0x10 ; i++) { |
| 113 | + stdout.printf ("%02X ", i); |
| 114 | + } |
| 115 | + stdout.printf ("%n\t "); |
| 116 | + for (int i = 0 ; i < 0x10 * 3 ; i++) { |
| 117 | + stdout.print ('-'); |
| 118 | + } |
| 119 | + |
| 120 | + var visible = new StringBuilder (); |
| 121 | + for (int i = 0 ; i < r.length ; i++) { |
| 122 | + if (i % 0x10 == 0) { |
| 123 | + // line header |
| 124 | + stdout.println (visible.toString ()); |
| 125 | + visible.setLength (0); |
| 126 | + stdout.printf ("%02X\t| ", config.offset + i); |
| 127 | + } |
| 128 | + |
| 129 | + stdout.printf ("%02X ", r[i]); |
| 130 | + visible.append (r[i] >= 0x20 && r[i] <= 0x7E ? (char) r[i] : '.'); |
| 131 | + } |
| 132 | + stdout.println (visible.toString ()); |
| 133 | + } |
| 134 | + |
| 135 | +} |
0 commit comments