Advertisement
Guest User

bzip2

a guest
Mar 18th, 2012
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.88 KB | None | 0 0
  1. package foo.bar;
  2.  
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5.  
  6. import org.apache.commons.io.IOUtils;
  7.  
  8. public class SystemBzip2InputStream extends InputStream {
  9.  
  10.     final InputStream in;
  11.     final Process exec;
  12.     IOException readException = null;
  13.  
  14.     SystemBzip2InputStream(final InputStream in) throws IOException{
  15.         this.in = in;
  16.         exec = Runtime.getRuntime().exec(new String[]{"bzip2","-dc"});
  17.         final Thread reader = new Thread(new Runnable() {
  18.            
  19.             public void run() {
  20.                 try {
  21.                     IOUtils.copy(in, exec.getOutputStream());
  22.                     exec.getOutputStream().close();
  23.                 } catch (IOException e) {
  24.                     readException = e;
  25.                 }
  26.             }
  27.         });
  28.         reader.start();
  29.     }
  30.  
  31.     @Override
  32.     public int read() throws IOException {
  33.         checkInput();
  34.         return exec.getInputStream().read();
  35.     }
  36.  
  37.     private void checkInput() throws IOException {
  38.         if(readException != null) {
  39.             throw new IOException(readException);
  40.         }
  41.     }
  42.  
  43.     @Override
  44.     public void close() throws IOException {
  45.         super.close();
  46.         in.close();
  47.         exec.destroy();
  48.     }
  49.  
  50.     @Override
  51.     public int read(byte[] b) throws IOException {
  52.         checkInput();
  53.         return exec.getInputStream().read(b);
  54.     }
  55.  
  56.     @Override
  57.     public int read(byte[] b, int off, int len) throws IOException {
  58.         checkInput();
  59.         return exec.getInputStream().read(b, off, len);
  60.     }
  61.  
  62.     @Override
  63.     public long skip(long n) throws IOException {
  64.         checkInput();
  65.         return exec.getInputStream().skip(n);
  66.     }
  67.  
  68.     @Override
  69.     public int available() throws IOException {
  70.         checkInput();
  71.         return exec.getInputStream().available();
  72.     }
  73.  
  74.     @Override
  75.     public synchronized void mark(int readlimit) {
  76.         exec.getInputStream().mark(readlimit);
  77.     }
  78.  
  79.     @Override
  80.     public synchronized void reset() throws IOException {
  81.         checkInput();
  82.         exec.getInputStream().reset();
  83.     }
  84.  
  85.     @Override
  86.     public boolean markSupported() {
  87.         return exec.getInputStream().markSupported();
  88.     }
  89.  
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement