Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package foo.bar;
- import java.io.IOException;
- import java.io.InputStream;
- import org.apache.commons.io.IOUtils;
- public class SystemBzip2InputStream extends InputStream {
- final InputStream in;
- final Process exec;
- IOException readException = null;
- SystemBzip2InputStream(final InputStream in) throws IOException{
- this.in = in;
- exec = Runtime.getRuntime().exec(new String[]{"bzip2","-dc"});
- final Thread reader = new Thread(new Runnable() {
- public void run() {
- try {
- IOUtils.copy(in, exec.getOutputStream());
- exec.getOutputStream().close();
- } catch (IOException e) {
- readException = e;
- }
- }
- });
- reader.start();
- }
- @Override
- public int read() throws IOException {
- checkInput();
- return exec.getInputStream().read();
- }
- private void checkInput() throws IOException {
- if(readException != null) {
- throw new IOException(readException);
- }
- }
- @Override
- public void close() throws IOException {
- super.close();
- in.close();
- exec.destroy();
- }
- @Override
- public int read(byte[] b) throws IOException {
- checkInput();
- return exec.getInputStream().read(b);
- }
- @Override
- public int read(byte[] b, int off, int len) throws IOException {
- checkInput();
- return exec.getInputStream().read(b, off, len);
- }
- @Override
- public long skip(long n) throws IOException {
- checkInput();
- return exec.getInputStream().skip(n);
- }
- @Override
- public int available() throws IOException {
- checkInput();
- return exec.getInputStream().available();
- }
- @Override
- public synchronized void mark(int readlimit) {
- exec.getInputStream().mark(readlimit);
- }
- @Override
- public synchronized void reset() throws IOException {
- checkInput();
- exec.getInputStream().reset();
- }
- @Override
- public boolean markSupported() {
- return exec.getInputStream().markSupported();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement