Skip to content

Commit ebea734

Browse files
committed
+ file-part-hex
1 parent 2eff764 commit ebea734

File tree

6 files changed

+204
-5
lines changed

6 files changed

+204
-5
lines changed

README.md

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
| English |
22

33
# Introduction
4-
Handy scripts collection for handling files.
4+
Handy scripts for handling files.
55

66
Usage instructions are placed under each folder.
77

@@ -20,9 +20,11 @@ Useful to make a record before reboot, and confirm after reboot.
2020

2121
Make life of reboot easier.
2222

23+
# file-part-hex
24+
Hex view of part of a file.
25+
2326
# Roadmap
24-
1. file-part-hex: Hex view of part of a file.
25-
2. compare-2-folders
27+
compare-2-folders
2628

2729
- - - -
2830

@@ -45,3 +47,6 @@ Make life of reboot easier.
4547
收集 Linux 运行状态,包括进程、网络、挂载点、环境变量,写入 linux.status 文件夹。
4648

4749
可用于重启前先记录,重启后对照记录核对服务是否齐全,让重启工作轻松点。
50+
51+
# file-part-hex
52+
十六进制显示文件部分内容。

file-part-hex/FilePartHex.java

+135
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
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+
}

file-part-hex/README.md

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
| English |
2+
3+
# file-part-hex
4+
Hex view of part of a file.
5+
6+
# System Requirements
7+
Java ≥ 11
8+
9+
# Usage
10+
If no command line argument provided, the script will prompt to ask.
11+
12+
Windows:
13+
```dos
14+
file-part-hex.bat [file] [offset] [length]
15+
```
16+
17+
Linux:
18+
```bash
19+
./file-part-hex.sh [file] [offset] [length]
20+
```
21+
22+
# Further Development
23+
Preview window supports MBCS (Multi-Byte Character Set), like GB2312, GBK, UTF-8, UTF-16.
24+
25+
- - - -
26+
27+
| Chinese | 中文 |
28+
29+
# file-part-hex
30+
十六进制显示文件部分内容。
31+
32+
# 系统需求
33+
Java ≥ 11
34+
35+
# 用法
36+
如无命令行参数,脚本将提示输入。
37+
38+
Windows:
39+
```dos
40+
file-part-hex.bat [file] [offset] [length]
41+
```
42+
43+
Linux:
44+
```bash
45+
./file-part-hex.sh [file] [offset] [length]
46+
```
47+
48+
# 展望
49+
支持多字节字符 (MBCS) 的预览,如 GB2312、GBK、UTF-8、UTF-16。

file-part-hex/file-part-hex.bat

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
@echo off
2+
if defined JAVA_HOME (
3+
set JAVA=%JAVA_HOME%\bin\java
4+
) else if defined JRE_HOME (
5+
set JAVA=%JRE_HOME%\bin\java
6+
) else (
7+
set JAVA=java
8+
)
9+
%JAVA% FilePartHex.java %*

file-part-hex/file-part-hex.sh

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/usr/bin/bash
2+
java FilePartHex.java $*

find-charset-files/FindCharsetFiles.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,7 @@ public String toString () {
4545

4646
//--------------------------------------------------------------------
4747
/** Get configuration from standard input. */
48-
static Config fromStdIn ()
49-
throws IOException {
48+
static Config fromStdIn () throws IOException {
5049
var config = new Config ();
5150
String line;
5251

0 commit comments

Comments
 (0)