InputStream wrapper to read files w/ sudo

This commit is contained in:
Doug MacEachern 2005-12-08 20:45:32 +00:00
parent 082c64e00c
commit d9a2f6f284
1 changed files with 68 additions and 0 deletions

View File

@ -0,0 +1,68 @@
package net.hyperic.sigar;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
/**
* InputStream wrapper for 'sudo cat filename'
*/
public class SudoFileInputStream extends InputStream {
private Process process;
private InputStream is;
public SudoFileInputStream(String file)
throws IOException, FileNotFoundException {
this(new File(file));
}
public SudoFileInputStream(File file)
throws IOException, FileNotFoundException {
if (!file.exists()) {
throw new FileNotFoundException();
}
String[] args = {
"sudo", "cat", file.toString()
};
this.process = Runtime.getRuntime().exec(args);
this.is = this.process.getInputStream();
}
public void close() throws IOException {
this.process.destroy();
}
/* passthru all other InputStream methods */
public int read() throws IOException {
return this.is.read();
}
public int read(byte b[]) throws IOException {
return this.is.read(b);
}
public int read(byte b[], int off, int len) throws IOException {
return this.is.read(b, off, len);
}
public long skip(long n) throws IOException {
return this.is.skip(n);
}
public int available() throws IOException {
return this.is.available();
}
public synchronized void mark(int readlimit) {
this.is.mark(readlimit);
}
public synchronized void reset() throws IOException {
this.is.reset();
}
public boolean markSupported() {
return this.is.markSupported();
}
}