am ccd8ed53: Support stdin and stdout for compress/uncompress

* commit 'ccd8ed53b33a5f91961f40d576abd0a91b87a66d':
  Support stdin and stdout for compress/uncompress
main
Jean Chalard 2012-08-08 22:37:24 -07:00 committed by Android Git Automerger
commit a6d37f4336
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);
} }
} }