Skip to content

Commit

Permalink
+ file-part-hex
Browse files Browse the repository at this point in the history
  • Loading branch information
ActiveVolcano committed Mar 31, 2020
1 parent 2eff764 commit ebea734
Show file tree
Hide file tree
Showing 6 changed files with 204 additions and 5 deletions.
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
| English |

# Introduction
Handy scripts collection for handling files.
Handy scripts for handling files.

Usage instructions are placed under each folder.

Expand All @@ -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

- - - -

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

可用于重启前先记录,重启后对照记录核对服务是否齐全,让重启工作轻松点。

# file-part-hex
十六进制显示文件部分内容。
135 changes: 135 additions & 0 deletions file-part-hex/FilePartHex.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
import java.io.*;
import java.util.*;

//----------------------------------------------------------------------------
/**
* Hex view of part of a file.
*
* <p>
* Written by CHEN Qingcan, Spring 2020, Foshan China <br>
* Open source under WTFPL (Do What The Fuck You Want To Public License) http://www.wtfpl.net
*
* <p>
* Run as script via Java 11: <br>
* <code>
* java FilePartHex.java
* </code>
*/
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 ());
}

}
49 changes: 49 additions & 0 deletions file-part-hex/README.md
Original file line number Diff line number Diff line change
@@ -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。
9 changes: 9 additions & 0 deletions file-part-hex/file-part-hex.bat
Original file line number Diff line number Diff line change
@@ -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 %*
2 changes: 2 additions & 0 deletions file-part-hex/file-part-hex.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/usr/bin/bash
java FilePartHex.java $*
3 changes: 1 addition & 2 deletions find-charset-files/FindCharsetFiles.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down

0 comments on commit ebea734

Please sign in to comment.