1 11 package org.eclipse.team.internal.core.streams; 12 13 import java.io.FilterInputStream ; 14 import java.io.IOException ; 15 import java.io.InputStream ; 16 import java.io.InterruptedIOException ; 17 18 import org.eclipse.core.runtime.IProgressMonitor; 19 20 28 public abstract class ProgressMonitorInputStream extends FilterInputStream { 29 private IProgressMonitor monitor; 30 private int updateIncrement; 31 private long bytesTotal; 32 private long bytesRead = 0; 33 private long lastUpdate = -1; 34 private long nextUpdate = 0; 35 36 43 public ProgressMonitorInputStream(InputStream in, long bytesTotal, int updateIncrement, IProgressMonitor monitor) { 44 super(in); 45 this.bytesTotal = bytesTotal; 46 this.updateIncrement = updateIncrement; 47 this.monitor = monitor; 48 update(true); 49 } 50 51 protected abstract void updateMonitor(long bytesRead, long size, IProgressMonitor monitor); 52 53 58 public void close() throws IOException { 59 try { 60 in.close(); 61 } finally { 62 update(true); 63 } 64 } 65 66 73 public int read() throws IOException { 74 int b = in.read(); 75 if (b != -1) { 76 bytesRead += 1; 77 update(false); 78 } 79 return b; 80 } 81 82 89 public int read(byte[] buffer, int offset, int length) throws IOException { 90 try { 91 int count = in.read(buffer, offset, length); 92 if (count != -1) { 93 bytesRead += count; 94 update(false); 95 } 96 return count; 97 } catch (InterruptedIOException e) { 98 bytesRead += e.bytesTransferred; 99 update(false); 100 throw e; 101 } 102 } 103 104 111 public long skip(long amount) throws IOException { 112 try { 113 long count = in.skip(amount); 114 bytesRead += count; 115 update(false); 116 return count; 117 } catch (InterruptedIOException e) { 118 bytesRead += e.bytesTransferred; 119 update(false); 120 throw e; 121 } 122 } 123 124 127 public boolean markSupported() { 128 return false; 129 } 130 131 private void update(boolean now) { 132 if (bytesRead >= nextUpdate || now) { 133 nextUpdate = bytesRead - (bytesRead % updateIncrement); 134 if (nextUpdate != lastUpdate) updateMonitor(nextUpdate, bytesTotal, monitor); 135 lastUpdate = nextUpdate; 136 nextUpdate += updateIncrement; 137 } 138 } 139 } 140 | Popular Tags |