Cleanup: remove unused Command abstractions

This commit is contained in:
David Kuettel
2013-04-10 15:16:04 -07:00
parent bd9d6ce3ae
commit ac7ebc90da
4 changed files with 0 additions and 130 deletions
@@ -1,72 +0,0 @@
// Copyright 2011 Google Inc. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package com.google.typography.font.compression;
import com.google.common.io.ByteStreams;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
/**
* This is a simple wrapper to run a commandline as a pipe. It's not
* particularly efficient, robust, or featureful.
*/
public class Command {
private class StreamConsumer extends Thread {
private final InputStream is;
private byte[] result = null;
public StreamConsumer(InputStream is) {
this.is = is;
}
@Override
public void run() {
try {
result = ByteStreams.toByteArray(is);
} catch (IOException e) {
// TODO: handle this well
}
}
public byte[] getResult() {
return result;
}
}
private final ProcessBuilder processBuilder;
public Command(String[] args) {
processBuilder = new ProcessBuilder(args);
}
public CommandResult execute(byte[] input) throws CommandException {
Process process;
try {
process = processBuilder.start();
} catch (IOException e) {
throw new CommandException("exec failed");
}
StreamConsumer sc = new StreamConsumer(process.getInputStream());
sc.start();
OutputStream stdin = process.getOutputStream();
try {
stdin.write(input);
stdin.close();
} catch (IOException e) {
throw new CommandException("error writing input");
}
try {
process.waitFor();
sc.join();
} catch (InterruptedException e) {
throw new CommandException("interrupted");
}
return new CommandResult(sc.getResult());
}
}
@@ -1,12 +0,0 @@
// Copyright 2011 Google Inc. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package com.google.typography.font.compression;
public class CommandException extends Exception {
public CommandException(String message) {
super(message);
}
}
@@ -1,18 +0,0 @@
// Copyright 2011 Google Inc. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package com.google.typography.font.compression;
public final class CommandResult {
private final byte[] stdout;
public CommandResult(byte[] stdout) {
this.stdout = stdout;
}
public byte[] getStdout() {
return stdout;
}
}
@@ -1,28 +0,0 @@
// Copyright 2011 Google Inc. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package com.google.typography.font.compression;
import java.io.IOException;
/**
* A simple test for the command mechanism. Quick and dirty run with:
* java -cp 'build/classes:lib/guava-11.0.1.jar' com/google/typography/font/compression/TestCommand
*/
public class TestCommand {
public static void main(String[] args) throws IOException {
String[] commandArgs = {"/usr/bin/lzma"};
byte[] input = new byte[16384];
try {
CommandResult result = new Command(commandArgs).execute(input);
byte[] output = result.getStdout();
for (int i = 0; i < output.length; i++) {
System.out.printf("%02x\n", output[i] & 0xff);
}
} catch (CommandException e) {
e.printStackTrace();
};
}
}