jdk/src/share/classes/javax/swing/ProgressMonitorInputStream.java
author ant
Thu, 17 May 2012 21:48:57 +0400
changeset 12661 6cf8b7116579
parent 5506 202f599c92aa
child 20455 f6f9a0c2796b
permissions -rw-r--r--
7125044: [macosx] Test failure because Component.transferFocus() works differently in applet and application. Summary: forward port from 7u4 Reviewed-by: art
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 1997, 1999, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
package javax.swing;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.awt.Component;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * Monitors the progress of reading from some InputStream. This ProgressMonitor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * is normally invoked in roughly this form:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * InputStream in = new BufferedInputStream(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *                          new ProgressMonitorInputStream(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *                                  parentComponent,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *                                  "Reading " + fileName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *                                  new FileInputStream(fileName)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * </pre><p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * This creates a progress monitor to monitor the progress of reading
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * the input stream.  If it's taking a while, a ProgressDialog will
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * be popped up to inform the user.  If the user hits the Cancel button
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * an InterruptedIOException will be thrown on the next read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * All the right cleanup is done when the stream is closed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * For further documentation and examples see
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * <a href="http://java.sun.com/docs/books/tutorial/uiswing/components/progress.html">How to Monitor Progress</a>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * a section in <em>The Java Tutorial.</em>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * @see ProgressMonitor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * @see JOptionPane
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * @author James Gosling
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
public class ProgressMonitorInputStream extends FilterInputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    private ProgressMonitor monitor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    private int             nread = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    private int             size = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * Constructs an object to monitor the progress of an input stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * @param message Descriptive text to be placed in the dialog box
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     *                if one is popped up.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * @param parentComponent The component triggering the operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     *                        being monitored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * @param in The input stream to be monitored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    public ProgressMonitorInputStream(Component parentComponent,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                                      Object message,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                                      InputStream in) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        super(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            size = in.available();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        catch(IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            size = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        monitor = new ProgressMonitor(parentComponent, message, null, 0, size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * Get the ProgressMonitor object being used by this stream. Normally
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * this isn't needed unless you want to do something like change the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * descriptive text partway through reading the file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * @return the ProgressMonitor object used by this object
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    public ProgressMonitor getProgressMonitor() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        return monitor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * Overrides <code>FilterInputStream.read</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * to update the progress monitor after the read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    public int read() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        int c = in.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        if (c >= 0) monitor.setProgress(++nread);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        if (monitor.isCanceled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            InterruptedIOException exc =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                                    new InterruptedIOException("progress");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            exc.bytesTransferred = nread;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            throw exc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        return c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * Overrides <code>FilterInputStream.read</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * to update the progress monitor after the read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    public int read(byte b[]) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        int nr = in.read(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        if (nr > 0) monitor.setProgress(nread += nr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        if (monitor.isCanceled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            InterruptedIOException exc =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                                    new InterruptedIOException("progress");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            exc.bytesTransferred = nread;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            throw exc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        return nr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * Overrides <code>FilterInputStream.read</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * to update the progress monitor after the read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    public int read(byte b[],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                    int off,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                    int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        int nr = in.read(b, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        if (nr > 0) monitor.setProgress(nread += nr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        if (monitor.isCanceled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            InterruptedIOException exc =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                                    new InterruptedIOException("progress");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            exc.bytesTransferred = nread;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            throw exc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        return nr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * Overrides <code>FilterInputStream.skip</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * to update the progress monitor after the skip.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    public long skip(long n) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        long nr = in.skip(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        if (nr > 0) monitor.setProgress(nread += nr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        return nr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * Overrides <code>FilterInputStream.close</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * to close the progress monitor as well as the stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    public void close() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        in.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        monitor.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * Overrides <code>FilterInputStream.reset</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * to reset the progress monitor as well as the stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    public synchronized void reset() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        in.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        nread = size - in.available();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        monitor.setProgress(nread);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
}