Skip to content

Commit

Permalink
+ base64-file
Browse files Browse the repository at this point in the history
  • Loading branch information
ActiveVolcano committed Apr 9, 2020
1 parent 6a7fa85 commit e503748
Show file tree
Hide file tree
Showing 7 changed files with 266 additions and 0 deletions.
106 changes: 106 additions & 0 deletions base64-file/Base64File.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import java.io.*;
import java.nio.file.*;
import java.util.*;

//----------------------------------------------------------------------------
/**
* Base64 to 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 Base64File.java
* </code>
*/
public final class Base64File {

static final BufferedReader stdin = new BufferedReader (new InputStreamReader (System.in));
static final PrintStream stdout = System.out;
static final PrintStream stderr = System.err;

static Config config = null;
static class Config {
String from;
Path to;

//--------------------------------------------------------------------
static Config getInstance (final String... args) throws IOException {
return args.length >= 2 ? fromArgs (args) : fromStdIn ();
}

//--------------------------------------------------------------------
/** Get configuration from command line arguments. */
private static Config fromArgs (final String... args) {
assert args != null && args.length >= 2;
var config = new Config ();
config.from = args[0];
config.to = Paths.get (args[1]);
return config;
}

//--------------------------------------------------------------------
/** Get configuration from standard input. */
private static Config fromStdIn () throws IOException {
var config = new Config ();

// from
stdout.print ("Base64 string or input file name: ");
config.from = stdin.readLine ().trim ();

// to
String prompt;
for (prompt = "" ; prompt.length () <= 0 ; prompt = stdin.readLine ().trim ()) {
stdout.print ("Output file name: ");
}
config.to = Paths.get (prompt);

return config;
}

}

//------------------------------------------------------------------------
/** Program entry */
public static void main (final String... args) {
try {
config = Config.getInstance (args);
var base64s = input ();
output (base64s);

} catch (IOException e) {
stderr.println (e.getMessage ());
}
}

//------------------------------------------------------------------------
private static String input () throws IOException {
var path = Paths.get (config.from);
if (Files.isReadable (path)) {
stdout.println ("Path found. Input treated as file name. Read Base64 string from it...");
return Files.readString (path).trim ();

} else {
stdout.println ("Path not found. Input treated as Base64 string.");
return config.from;
}
}

//------------------------------------------------------------------------
private static void output (final String base64s) throws IOException {
try {
var decoder = Base64.getDecoder();
byte[] base64 = decoder.decode (base64s);
Files.write (config.to, base64);
stdout.printf ("%d bytes written to output file %s%n", base64.length, config.to);

} catch (IllegalArgumentException e) {
stderr.println ("Input not in Base64 format: " + e.getMessage ());
System.exit (1);
}
}

}
93 changes: 93 additions & 0 deletions base64-file/FileBase64.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import java.io.*;
import java.nio.file.*;
import java.util.*;

//----------------------------------------------------------------------------
/**
* File to Base64.
*
* <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 FileBase64.java
* </code>
*/
public final class FileBase64 {

static final BufferedReader stdin = new BufferedReader (new InputStreamReader (System.in));
static final PrintStream stdout = System.out;
static final PrintStream stderr = System.err;

static Config config = null;
static class Config {
Path from;
/** empty stands for output to console not to file */
String to = "";

//--------------------------------------------------------------------
static Config getInstance (final String... args) throws IOException {
return args.length >= 1 ? fromArgs (args) : fromStdIn ();
}

//--------------------------------------------------------------------
/** Get configuration from command line arguments. */
private static Config fromArgs (final String... args) {
assert args != null && args.length >= 1;
var config = new Config ();
config.from = Paths.get (args[0]);
if (args.length >= 2) {
config.to = args[1];
}
return config;
}

//--------------------------------------------------------------------
/** Get configuration from standard input. */
private static Config fromStdIn () throws IOException {
var config = new Config ();

// from
String prompt;
for (prompt = "" ; prompt.length () <= 0 ; prompt = stdin.readLine ().trim ()) {
stdout.print ("Input file name: ");
}
config.from = Paths.get (prompt);

// to
stdout.print ("Output file name (empty to console): ");
config.to = stdin.readLine ().trim ();

return config;
}

}

//------------------------------------------------------------------------
/** Program entry */
public static void main (final String... args) {
try {
config = Config.getInstance (args);
var b = Files.readAllBytes (config.from);
output (b);

} catch (IOException e) {
stderr.println (e.getMessage ());
}
}

//------------------------------------------------------------------------
private static void output (final byte[] b) throws IOException {
var base64s = Base64.getEncoder().encodeToString (b);
if (config.to.length () <= 0) {
stdout.println (base64s);
} else {
Files.writeString (Path.of (config.to), base64s);
stdout.printf ("%d bytes written to output file %s%n", base64s.length (), config.to);
}
}

}
47 changes: 47 additions & 0 deletions base64-file/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
| English |

# base64-file
Base64 to file and reverse.

# System Requirements
Java ≥ 11

# Usage
If no command line argument provided, it prompts to ask.

Windows:
```dos
base64-file.bat [input Base64 string or file name] [output file name]
file-base64.bat [input file name] [output file name or empty to console]
```

Linux:
```bash
./base64-file.sh [input Base64 string or file name] [output file name]
./file-base64.sh [input file name] [output file name or empty to console]
```

- - - -

| Chinese | 中文 |

# base64-file
Base64 转成文件,及其反方向转换。

# 系统需求
Java ≥ 11

# 用法
如无命令行参数,脚本将提示输入。

Windows:
```dos
base64-file.bat [Base64 文字或输入文件名] [输出文件名]
file-base64.bat [输入文件名] [输出文件名或留空表示输出至控制台]
```

Linux:
```bash
./base64-file.sh [Base64 文字或输入文件名] [输出文件名]
./file-base64.sh [输入文件名] [输出文件名或留空表示输出至控制台]
```
9 changes: 9 additions & 0 deletions base64-file/base64-file.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% Base64File.java %*
1 change: 1 addition & 0 deletions base64-file/base64-file.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
java Base64File.java $*
9 changes: 9 additions & 0 deletions base64-file/file-base64.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% FileBase64.java %*
1 change: 1 addition & 0 deletions base64-file/file-base64.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
java FileBase64.java $*

0 comments on commit e503748

Please sign in to comment.