From ebea7346bbff34d8eb6348f0a22dccc75a41d066 Mon Sep 17 00:00:00 2001 From: ActiveVolcano Date: Tue, 31 Mar 2020 17:20:49 +0800 Subject: [PATCH] + file-part-hex --- README.md | 11 +- file-part-hex/FilePartHex.java | 135 +++++++++++++++++++++++ file-part-hex/README.md | 49 ++++++++ file-part-hex/file-part-hex.bat | 9 ++ file-part-hex/file-part-hex.sh | 2 + find-charset-files/FindCharsetFiles.java | 3 +- 6 files changed, 204 insertions(+), 5 deletions(-) create mode 100644 file-part-hex/FilePartHex.java create mode 100644 file-part-hex/README.md create mode 100644 file-part-hex/file-part-hex.bat create mode 100644 file-part-hex/file-part-hex.sh diff --git a/README.md b/README.md index b4fb80b..4b53b22 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ | English | # Introduction -Handy scripts collection for handling files. +Handy scripts for handling files. Usage instructions are placed under each folder. @@ -20,9 +20,11 @@ Useful to make a record before reboot, and confirm after reboot. Make life of reboot easier. +# file-part-hex +Hex view of part of a file. + # Roadmap -1. file-part-hex: Hex view of part of a file. -2. compare-2-folders +compare-2-folders - - - - @@ -45,3 +47,6 @@ Make life of reboot easier. 收集 Linux 运行状态,包括进程、网络、挂载点、环境变量,写入 linux.status 文件夹。 可用于重启前先记录,重启后对照记录核对服务是否齐全,让重启工作轻松点。 + +# file-part-hex +十六进制显示文件部分内容。 diff --git a/file-part-hex/FilePartHex.java b/file-part-hex/FilePartHex.java new file mode 100644 index 0000000..716a31f --- /dev/null +++ b/file-part-hex/FilePartHex.java @@ -0,0 +1,135 @@ +import java.io.*; +import java.util.*; + +//---------------------------------------------------------------------------- +/** + * Hex view of part of a file. + * + *

+ * Written by CHEN Qingcan, Spring 2020, Foshan China
+ * Open source under WTFPL (Do What The Fuck You Want To Public License) http://www.wtfpl.net + * + *

+ * Run as script via Java 11:
+ * + * java FilePartHex.java + * + */ +public final class FilePartHex { + + static final BufferedReader stdin = new BufferedReader (new InputStreamReader (System.in)); + static final PrintStream stdout = System.out; + static final PrintStream stderr = System.err; + static final String HR = "--------"; + + static Config config = null; + static class Config { + String where; + Integer offset = 0; + Integer length = 0x10 * 0x10; + + //-------------------------------------------------------------------- + static Config getInstance (final String... args) throws IOException { + return args.length > 0 ? fromArgs (args) : fromStdIn (); + } + + //-------------------------------------------------------------------- + /** Get configuration from command line arguments. */ + static Config fromArgs (final String... args) { + var config = new Config (); + if (args.length >= 1) { + config.where = args[0]; + } + try { + if (args.length >= 2) { + config.offset = Integer.valueOf (args[1]); + } + if (args.length >= 3) { + config.length = Integer.valueOf (args[2]); + } + } catch (NumberFormatException e) { + stderr.println ("Argument offset and length should be a number."); + } + return config; + } + + //-------------------------------------------------------------------- + /** Get configuration from standard input. */ + private static Config fromStdIn () throws IOException { + var config = new Config (); + config.where = stdinLine ("", "File path: "); + try { config.offset = Integer.valueOf (stdinLine ("0", "Offset (default to 0): ")); } + catch (NumberFormatException e) { /* ignore */ } + try { config.length = Integer.valueOf (stdinLine ("256", "Length (default to 256): ")); } + catch (NumberFormatException e) { /* ignore */ } + return config; + } + + //-------------------------------------------------------------------- + /** Prompt and then read a line from standard input. Return default value if empty input. */ + static String stdinLine (final String defaultLine, final String format, final Object... args) + throws IOException { + stdout.printf (format, args); + String line = stdin.readLine ().trim (); + return line.length () > 0 ? line : defaultLine; + } + + } + + //------------------------------------------------------------------------ + /** Program entry */ + public static void main (final String... args) { + try { + config = Config.getInstance (args); + var r = input (); + output (r); + } catch (IOException e) { + stderr.println (e.getMessage ()); + } + } + + //------------------------------------------------------------------------ + /** Read bytes from file */ + private static byte[] input () throws FileNotFoundException, IOException { + try (var in = new FileInputStream (config.where)) { + var r = new byte [config.length]; + in.skip (config.offset); + int n = in.read (r, 0, config.length); + return Arrays.copyOf (r, n > 0 ? n : 0); + } + } + + //------------------------------------------------------------------------ + /** Bytes in hex to standard output. */ + private static void output (final byte[] r) { + // table header + stdout.println (config.where); + stdout.printf ("offset %d (%X in hex) length %d (%X in hex)%n", + config.offset, config.offset, config.length, config.length); + + // column header + stdout.print ("\t "); + for (int i = 0 ; i < 0x10 ; i++) { + stdout.printf ("%02X ", i); + } + stdout.printf ("%n\t "); + for (int i = 0 ; i < 0x10 * 3 ; i++) { + stdout.print ('-'); + } + + var visible = new StringBuilder (); + for (int i = 0 ; i < r.length ; i++) { + if (i % 0x10 == 0) { + // line header + stdout.println (visible.toString ()); + visible.setLength (0); + stdout.printf ("%02X\t| ", config.offset + i); + } + + stdout.printf ("%02X ", r[i]); + visible.append (r[i] >= 0x20 && r[i] <= 0x7E ? (char) r[i] : '.'); + } + stdout.println (visible.toString ()); + } + +} diff --git a/file-part-hex/README.md b/file-part-hex/README.md new file mode 100644 index 0000000..f37835e --- /dev/null +++ b/file-part-hex/README.md @@ -0,0 +1,49 @@ +| English | + +# file-part-hex +Hex view of part of a file. + +# System Requirements +Java ≥ 11 + +# Usage +If no command line argument provided, the script will prompt to ask. + +Windows: +```dos +file-part-hex.bat [file] [offset] [length] +``` + +Linux: +```bash +./file-part-hex.sh [file] [offset] [length] +``` + +# Further Development +Preview window supports MBCS (Multi-Byte Character Set), like GB2312, GBK, UTF-8, UTF-16. + +- - - - + +| Chinese | 中文 | + +# file-part-hex +十六进制显示文件部分内容。 + +# 系统需求 +Java ≥ 11 + +# 用法 +如无命令行参数,脚本将提示输入。 + +Windows: +```dos +file-part-hex.bat [file] [offset] [length] +``` + +Linux: +```bash +./file-part-hex.sh [file] [offset] [length] +``` + +# 展望 +支持多字节字符 (MBCS) 的预览,如 GB2312、GBK、UTF-8、UTF-16。 diff --git a/file-part-hex/file-part-hex.bat b/file-part-hex/file-part-hex.bat new file mode 100644 index 0000000..99b686f --- /dev/null +++ b/file-part-hex/file-part-hex.bat @@ -0,0 +1,9 @@ +@echo off +if defined JAVA_HOME ( + set JAVA=%JAVA_HOME%\bin\java +) else if defined JRE_HOME ( + set JAVA=%JRE_HOME%\bin\java +) else ( + set JAVA=java +) +%JAVA% FilePartHex.java %* diff --git a/file-part-hex/file-part-hex.sh b/file-part-hex/file-part-hex.sh new file mode 100644 index 0000000..ce6917d --- /dev/null +++ b/file-part-hex/file-part-hex.sh @@ -0,0 +1,2 @@ +#!/usr/bin/bash +java FilePartHex.java $* diff --git a/find-charset-files/FindCharsetFiles.java b/find-charset-files/FindCharsetFiles.java index 1a8f2e6..0af2a64 100644 --- a/find-charset-files/FindCharsetFiles.java +++ b/find-charset-files/FindCharsetFiles.java @@ -45,8 +45,7 @@ public String toString () { //-------------------------------------------------------------------- /** Get configuration from standard input. */ - static Config fromStdIn () - throws IOException { + static Config fromStdIn () throws IOException { var config = new Config (); String line;