FileDocCategorySizeDatePackage
GZipThread.javaAPI DocExample1723Sat Sep 09 19:52:38 BST 2000None

GZipThread

public class GZipThread extends Thread

Fields Summary
private List
pool
private static int
filesCompressed
Constructors Summary
public GZipThread(List pool)


     
    this.pool = pool;
  
Methods Summary
private static synchronized voidincrementFilesCompressed()

    filesCompressed++;
  
public voidrun()

    
    while (filesCompressed != GZipAllFiles.getNumberOfFilesToBeCompressed()) {
    
      File input = null;
      
      synchronized (pool) {         
        while (pool.isEmpty()) {
          if (filesCompressed == GZipAllFiles.getNumberOfFilesToBeCompressed()) return;
          try {
            pool.wait();
          }
          catch (InterruptedException e) {
          }
        }

        input = (File) pool.remove(pool.size()-1); 
      
      }
    
      // don't compress an already compressed file
      if (!input.getName().endsWith(".gz")) { 
      
        try {
          InputStream in = new FileInputStream(input);
          in = new BufferedInputStream(in);
          
          File output = new File(input.getParent(), input.getName() + ".gz");
          if (!output.exists()) { // Don't overwrite an existing file
            OutputStream out = new FileOutputStream(output);
            out = new GZIPOutputStream(out);
            out = new BufferedOutputStream(out);
            int b;
            while ((b = in.read()) != -1) out.write(b);
            out.flush();
            out.close();
            incrementFilesCompressed();
            in.close();
          }
        }
        catch (IOException e) {
          System.err.println(e);
        }
        
      } // end if 
  
    } // end while