home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 November / Chip_1998-11_cd.bin / tema / Cafe / jfc.bin / ProgressMonitorInputStream.java < prev    next >
Text File  |  1998-02-26  |  4KB  |  112 lines

  1. /*
  2.  * @(#)ProgressMonitorInputStream.java    1.8 98/01/30
  3.  * 
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  */
  20.  
  21. package com.sun.java.swing;
  22.  
  23. import java.io.*;
  24. import java.awt.Component;
  25.  
  26. /** Monitors the progress of reading from some InputStream.  Normally invoked
  27.  * in roughly this form:
  28.  * <pre>
  29.  * InputStream in = new BufferedInputStream(
  30.  *            new ProgressMonitorInputStream(
  31.  *                     parentComponent,
  32.  *                    "Reading "+fileName,
  33.  *                     new FileInputStream(fileName)));
  34.  * </pre><p>
  35.  * This creates a progress monitor to monitor the progress of reading
  36.  * the input stream.  If it's taking a while, a ProgressDialog will
  37.  * be popped up to inform the user.  If the user hits the Cancel button
  38.  * an InterruptedIOException will be thrown on the next read.
  39.  * All the right cleanup is done when the stream is closed.
  40.  * @see ProgressMonitor
  41.  * @see JOptionPane
  42.  * @author James Gosling
  43.  */
  44. public class ProgressMonitorInputStream extends FilterInputStream {
  45.     private int nread = 0;
  46.     private int size = 0;
  47.     private ProgressMonitor monitor;
  48.     /** @param message Descriptive text to be placed in the dialog box
  49.      *        if one is popped up.
  50.      * @param parentComponent The component triggering the operation
  51.      *        being monitored.
  52.      * @param in The input stream to be monitored.
  53.      */
  54.     public ProgressMonitorInputStream(Component parentComponent, Object message, InputStream in) {
  55.         super(in);
  56.         try { size = in.available(); }
  57.         catch(IOException ioe) { size = 0; }
  58.         monitor = new ProgressMonitor(parentComponent,message,null,0,size);
  59.     }
  60.     /* Get the ProgressMonitor object being used by this stream.  Normally
  61.      * this isn't needed unless you want to do something like change the
  62.      * descriptive text partway through reading the file.
  63.      */
  64.     public ProgressMonitor getProgressMonitor() { return monitor; }
  65.     public int read() throws IOException {
  66.         int c = in.read();
  67.         if (c>=0) monitor.setProgress(nread++);
  68.         if (monitor.isCanceled()) {
  69.             InterruptedIOException x = new InterruptedIOException("progress");
  70.             x.bytesTransferred = nread;
  71.             throw x;
  72.         }
  73.         return c;
  74.     }
  75.     public int read(byte b[]) throws IOException {
  76.         int nr = in.read(b);
  77.         if (nr>0) monitor.setProgress(nread+=nr);
  78.         if (monitor.isCanceled()) {
  79.             InterruptedIOException x = new InterruptedIOException("progress");
  80.             x.bytesTransferred = nread;
  81.             throw x;
  82.         }
  83.         return nr;
  84.     }
  85.     public int read(byte b[],
  86.                   int off,
  87.                   int len) throws IOException {
  88.         int nr = in.read(b, off, len);
  89.         if (nr>0) monitor.setProgress(nread+=nr);
  90.         if (monitor.isCanceled()) {
  91.             InterruptedIOException x = new InterruptedIOException("progress");
  92.             x.bytesTransferred = nread;
  93.             throw x;
  94.         }
  95.         return nr;
  96.     }
  97.     public long skip(long n) throws IOException {
  98.         long nr = in.skip(n);
  99.         if (nr>0) monitor.setProgress(nread+=nr);
  100.         return nr;
  101.     }
  102.     public void close() throws IOException {
  103.         in.close();
  104.         monitor.close();
  105.     }
  106.     public synchronized void reset() throws IOException {
  107.         in.reset();
  108.         nread = size-in.available();
  109.         monitor.setProgress(nread);
  110.     }
  111. }
  112.