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