1. /*
  2. * @(#)ProgressMonitorInputStream.java 1.16 00/02/02
  3. *
  4. * Copyright 1997-2000 Sun Microsystems, Inc. All Rights Reserved.
  5. *
  6. * This software is the proprietary information of Sun Microsystems, Inc.
  7. * Use is subject to license terms.
  8. *
  9. */
  10. package javax.swing;
  11. import java.io.*;
  12. import java.awt.Component;
  13. /**
  14. * Monitors the progress of reading from some InputStream. This ProgressMonitor
  15. * is normally invoked in roughly this form:
  16. * <pre>
  17. * InputStream in = new BufferedInputStream(
  18. * new ProgressMonitorInputStream(
  19. * parentComponent,
  20. * "Reading " + fileName,
  21. * new FileInputStream(fileName)));
  22. * </pre><p>
  23. * This creates a progress monitor to monitor the progress of reading
  24. * the input stream. If it's taking a while, a ProgressDialog will
  25. * be popped up to inform the user. If the user hits the Cancel button
  26. * an InterruptedIOException will be thrown on the next read.
  27. * All the right cleanup is done when the stream is closed.
  28. *
  29. *
  30. * <p>
  31. *
  32. * For further documentation and examples see
  33. * <a href="http://java.sun.com/docs/books/tutorial/uiswing/components/progress.html">How to Monitor Progress</a>,
  34. * a section in <em>The Java Tutorial.</em>
  35. *
  36. * @see ProgressMonitor
  37. * @see JOptionPane
  38. * @author James Gosling
  39. * @version 1.16 02/02/00
  40. */
  41. public class ProgressMonitorInputStream extends FilterInputStream
  42. {
  43. private ProgressMonitor monitor;
  44. private int nread = 0;
  45. private int size = 0;
  46. /**
  47. * Constructs an object to monitor the progress of an input stream.
  48. *
  49. * @param message Descriptive text to be placed in the dialog box
  50. * if one is popped up.
  51. * @param parentComponent The component triggering the operation
  52. * being monitored.
  53. * @param in The input stream to be monitored.
  54. */
  55. public ProgressMonitorInputStream(Component parentComponent,
  56. Object message,
  57. InputStream in) {
  58. super(in);
  59. try {
  60. size = in.available();
  61. }
  62. catch(IOException ioe) {
  63. size = 0;
  64. }
  65. monitor = new ProgressMonitor(parentComponent, message, null, 0, size);
  66. }
  67. /**
  68. * Get the ProgressMonitor object being used by this stream. Normally
  69. * this isn't needed unless you want to do something like change the
  70. * descriptive text partway through reading the file.
  71. * @return the ProgressMonitor object used by this object
  72. */
  73. public ProgressMonitor getProgressMonitor() {
  74. return monitor;
  75. }
  76. /**
  77. * Overrides <code>FilterInputStream.read</code>
  78. * to update the progress monitor after the read.
  79. */
  80. public int read() throws IOException {
  81. int c = in.read();
  82. if (c >= 0) monitor.setProgress(++nread);
  83. if (monitor.isCanceled()) {
  84. InterruptedIOException exc =
  85. new InterruptedIOException("progress");
  86. exc.bytesTransferred = nread;
  87. throw exc;
  88. }
  89. return c;
  90. }
  91. /**
  92. * Overrides <code>FilterInputStream.read</code>
  93. * to update the progress monitor after the read.
  94. */
  95. public int read(byte b[]) throws IOException {
  96. int nr = in.read(b);
  97. if (nr > 0) monitor.setProgress(nread += nr);
  98. if (monitor.isCanceled()) {
  99. InterruptedIOException exc =
  100. new InterruptedIOException("progress");
  101. exc.bytesTransferred = nread;
  102. throw exc;
  103. }
  104. return nr;
  105. }
  106. /**
  107. * Overrides <code>FilterInputStream.read</code>
  108. * to update the progress monitor after the read.
  109. */
  110. public int read(byte b[],
  111. int off,
  112. int len) throws IOException {
  113. int nr = in.read(b, off, len);
  114. if (nr > 0) monitor.setProgress(nread += nr);
  115. if (monitor.isCanceled()) {
  116. InterruptedIOException exc =
  117. new InterruptedIOException("progress");
  118. exc.bytesTransferred = nread;
  119. throw exc;
  120. }
  121. return nr;
  122. }
  123. /**
  124. * Overrides <code>FilterInputStream.skip</code>
  125. * to update the progress monitor after the skip.
  126. */
  127. public long skip(long n) throws IOException {
  128. long nr = in.skip(n);
  129. if (nr > 0) monitor.setProgress(nread += nr);
  130. return nr;
  131. }
  132. /**
  133. * Overrides <code>FilterInputStream.close</code>
  134. * to close the progress monitor as well as the stream.
  135. */
  136. public void close() throws IOException {
  137. in.close();
  138. monitor.close();
  139. }
  140. /**
  141. * Overrides <code>FilterInputStream.reset</code>
  142. * to reset the progress monitor as well as the stream.
  143. */
  144. public synchronized void reset() throws IOException {
  145. in.reset();
  146. nread = size - in.available();
  147. monitor.setProgress(nread);
  148. }
  149. }