From ccd8ed53b33a5f91961f40d576abd0a91b87a66d Mon Sep 17 00:00:00 2001 From: Jean Chalard Date: Thu, 9 Aug 2012 13:33:16 +0900 Subject: [PATCH] Support stdin and stdout for compress/uncompress This will improve flexibility in dictionary generation by allowing pipelined commands. Change-Id: Ia07bdc82c8bd740b5dfee6b5fd1a067f4b8b7ba5 --- .../inputmethod/latin/dicttool/Compress.java | 38 +++++++++++-------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/tools/dicttool/src/android/inputmethod/latin/dicttool/Compress.java b/tools/dicttool/src/android/inputmethod/latin/dicttool/Compress.java index a76ec50e0..3cb0a12c4 100644 --- a/tools/dicttool/src/android/inputmethod/latin/dicttool/Compress.java +++ b/tools/dicttool/src/android/inputmethod/latin/dicttool/Compress.java @@ -46,46 +46,52 @@ public class Compress { static public class Compressor extends Dicttool.Command { public static final String COMMAND = "compress"; - private static final String SUFFIX = ".compressed"; + public static final String STDIN_OR_STDOUT = "-"; public Compressor() { } public String getHelp() { - return "compress : Compresses a file using gzip compression"; + return COMMAND + " : " + + "Compresses a file using gzip compression"; } public void run() throws IOException { - if (mArgs.length < 1) { - throw new RuntimeException("Not enough arguments for command " + COMMAND); + if (mArgs.length > 2) { + throw new RuntimeException("Too many arguments for command " + COMMAND); } - final String inFilename = mArgs[0]; - final String outFilename = inFilename + SUFFIX; - final FileInputStream input = new FileInputStream(new File(inFilename)); - final FileOutputStream output = new FileOutputStream(new File(outFilename)); + final String inFilename = mArgs.length >= 1 ? mArgs[0] : STDIN_OR_STDOUT; + final String outFilename = mArgs.length >= 2 ? mArgs[1] : STDIN_OR_STDOUT; + final InputStream input = inFilename.equals(STDIN_OR_STDOUT) ? System.in + : 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)); } } static public class Uncompressor extends Dicttool.Command { public static final String COMMAND = "uncompress"; - private static final String SUFFIX = ".uncompressed"; + public static final String STDIN_OR_STDOUT = "-"; public Uncompressor() { } public String getHelp() { - return "uncompress : Uncompresses a file compressed with gzip compression"; + return COMMAND + " : " + + "Uncompresses a file compressed with gzip compression"; } public void run() throws IOException { - if (mArgs.length < 1) { - throw new RuntimeException("Not enough arguments for command " + COMMAND); + if (mArgs.length > 2) { + throw new RuntimeException("Too many arguments for command " + COMMAND); } - final String inFilename = mArgs[0]; - final String outFilename = inFilename + SUFFIX; - final FileInputStream input = new FileInputStream(new File(inFilename)); - final FileOutputStream output = new FileOutputStream(new File(outFilename)); + final String inFilename = mArgs.length >= 1 ? mArgs[0] : STDIN_OR_STDOUT; + final String outFilename = mArgs.length >= 2 ? mArgs[1] : STDIN_OR_STDOUT; + final InputStream input = inFilename.equals(STDIN_OR_STDOUT) ? System.in + : 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); } }