Support stdin and stdout for compress/uncompress

This will improve flexibility in dictionary generation by
allowing pipelined commands.

Change-Id: Ia07bdc82c8bd740b5dfee6b5fd1a067f4b8b7ba5
main
Jean Chalard 2012-08-09 13:33:16 +09:00
parent 9abea49dd4
commit ccd8ed53b3
1 changed files with 22 additions and 16 deletions

View File

@ -46,46 +46,52 @@ public class Compress {
static public class Compressor extends Dicttool.Command { static public class Compressor extends Dicttool.Command {
public static final String COMMAND = "compress"; public static final String COMMAND = "compress";
private static final String SUFFIX = ".compressed"; public static final String STDIN_OR_STDOUT = "-";
public Compressor() { public Compressor() {
} }
public String getHelp() { public String getHelp() {
return "compress <filename>: Compresses a file using gzip compression"; return COMMAND + " <src_filename> <dst_filename>: "
+ "Compresses a file using gzip compression";
} }
public void run() throws IOException { public void run() throws IOException {
if (mArgs.length < 1) { if (mArgs.length > 2) {
throw new RuntimeException("Not enough arguments for command " + COMMAND); throw new RuntimeException("Too many arguments for command " + COMMAND);
} }
final String inFilename = mArgs[0]; final String inFilename = mArgs.length >= 1 ? mArgs[0] : STDIN_OR_STDOUT;
final String outFilename = inFilename + SUFFIX; final String outFilename = mArgs.length >= 2 ? mArgs[1] : STDIN_OR_STDOUT;
final FileInputStream input = new FileInputStream(new File(inFilename)); final InputStream input = inFilename.equals(STDIN_OR_STDOUT) ? System.in
final FileOutputStream output = new FileOutputStream(new File(outFilename)); : new FileInputStream(new File(inFilename));
final OutputStream output = outFilename.equals(STDIN_OR_STDOUT) ? System.out
: new FileOutputStream(new File(outFilename));
copy(input, new GZIPOutputStream(output)); copy(input, new GZIPOutputStream(output));
} }
} }
static public class Uncompressor extends Dicttool.Command { static public class Uncompressor extends Dicttool.Command {
public static final String COMMAND = "uncompress"; public static final String COMMAND = "uncompress";
private static final String SUFFIX = ".uncompressed"; public static final String STDIN_OR_STDOUT = "-";
public Uncompressor() { public Uncompressor() {
} }
public String getHelp() { public String getHelp() {
return "uncompress <filename>: Uncompresses a file compressed with gzip compression"; return COMMAND + " <src_filename> <dst_filename>: "
+ "Uncompresses a file compressed with gzip compression";
} }
public void run() throws IOException { public void run() throws IOException {
if (mArgs.length < 1) { if (mArgs.length > 2) {
throw new RuntimeException("Not enough arguments for command " + COMMAND); throw new RuntimeException("Too many arguments for command " + COMMAND);
} }
final String inFilename = mArgs[0]; final String inFilename = mArgs.length >= 1 ? mArgs[0] : STDIN_OR_STDOUT;
final String outFilename = inFilename + SUFFIX; final String outFilename = mArgs.length >= 2 ? mArgs[1] : STDIN_OR_STDOUT;
final FileInputStream input = new FileInputStream(new File(inFilename)); final InputStream input = inFilename.equals(STDIN_OR_STDOUT) ? System.in
final FileOutputStream output = new FileOutputStream(new File(outFilename)); : new FileInputStream(new File(inFilename));
final OutputStream output = outFilename.equals(STDIN_OR_STDOUT) ? System.out
: new FileOutputStream(new File(outFilename));
copy(new GZIPInputStream(input), output); copy(new GZIPInputStream(input), output);
} }
} }