001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Dennis Ushakov
019: * @version $Revision$
020: */package javax.swing;
021:
022: import java.awt.Component;
023: import java.io.FilterInputStream;
024: import java.io.IOException;
025: import java.io.InputStream;
026: import java.io.InterruptedIOException;
027:
028: public class ProgressMonitorInputStream extends FilterInputStream {
029: private ProgressMonitor progressMonitor;
030: private int progress;
031: private int max;
032:
033: public ProgressMonitorInputStream(final Component parentComponent,
034: final Object message, final InputStream in) {
035: super (in);
036: max = getAvailable();
037: progressMonitor = new ProgressMonitor(parentComponent, message,
038: null, 0, max);
039: }
040:
041: public ProgressMonitor getProgressMonitor() {
042: return progressMonitor;
043: }
044:
045: public int read() throws IOException {
046: checkCancel();
047: int result = super .read();
048: updateMonitor(1);
049: return result;
050: }
051:
052: public int read(final byte[] b) throws IOException {
053: checkCancel();
054: int result = super .read(b);
055: updateMonitor(result);
056: return result;
057: }
058:
059: public int read(final byte[] b, final int off, final int len)
060: throws IOException {
061: checkCancel();
062: int result = super .read(b, off, len);
063: updateMonitor(result);
064: return result;
065: }
066:
067: public long skip(final long n) throws IOException {
068: long result = super .skip(n);
069: updateMonitor(result);
070: return result;
071: }
072:
073: public void close() throws IOException {
074: progressMonitor.close();
075: super .close();
076: }
077:
078: public void reset() throws IOException {
079: progressMonitor.setProgress(0);
080: super .reset();
081: }
082:
083: private void checkCancel() throws InterruptedIOException {
084: if (progressMonitor.isCanceled()) {
085: throw new InterruptedIOException();
086: }
087: }
088:
089: private void updateMonitor(final long bytesRead) throws IOException {
090: progress += bytesRead;
091: max = progress + getAvailable();
092: progressMonitor.setMaximum(max);
093: progressMonitor.setProgress(progress);
094: }
095:
096: private int getAvailable() {
097: try {
098: return available();
099: } catch (IOException e) {
100: return 0;
101: }
102: }
103: }
|