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 import org.eclipse.core.runtime.IStatus; 20 import org.eclipse.core.runtime.OperationCanceledException; 21 import org.eclipse.team.internal.core.*; 22 import org.eclipse.team.internal.core.Policy; 23 import org.eclipse.team.internal.core.TeamPlugin; 24 25 34 public class PollingInputStream extends FilterInputStream { 35 private static final boolean DEBUG = Policy.DEBUG_STREAMS; 36 private int numAttempts; 37 private IProgressMonitor monitor; 38 private boolean cancellable; 39 40 47 public PollingInputStream(InputStream in, int numAttempts, IProgressMonitor monitor) { 48 super(in); 49 this.numAttempts = numAttempts; 50 this.monitor = monitor; 51 this.cancellable = true; 52 } 53 54 64 public void close() throws IOException { 65 int attempts = 0; 66 try { 67 readPendingInput(); 68 } catch (IOException e) { 69 TeamPlugin.log(IStatus.ERROR, e.getMessage(), e); 72 } finally { 73 boolean stop = false; 74 while (!stop) { 75 try { 76 in.close(); 77 stop = true; 78 } catch (InterruptedIOException e) { 79 if (checkCancellation()) throw new OperationCanceledException(); 80 if (++attempts == numAttempts) 81 throw new InterruptedIOException (Messages.PollingInputStream_closeTimeout); 82 if (DEBUG) System.out.println("close retry=" + attempts); } 84 } 85 } 86 } 87 88 95 public int read() throws IOException { 96 int attempts = 0; 97 for (;;) { 98 if (checkCancellation()) throw new OperationCanceledException(); 99 try { 100 return in.read(); 101 } catch (InterruptedIOException e) { 102 if (++attempts == numAttempts) 103 throw new InterruptedIOException (Messages.PollingInputStream_readTimeout); 104 if (DEBUG) System.out.println("read retry=" + attempts); } 106 } 107 } 108 109 116 public int read(byte[] buffer, int off, int len) throws IOException { 117 int attempts = 0; 118 for (;;) { 119 if (checkCancellation()) throw new OperationCanceledException(); 120 try { 121 return in.read(buffer, off, len); 122 } catch (InterruptedIOException e) { 123 if (e.bytesTransferred != 0) return e.bytesTransferred; if (++attempts == numAttempts) 125 throw new InterruptedIOException (Messages.PollingInputStream_readTimeout); 126 if (DEBUG) System.out.println("read retry=" + attempts); } 128 } 129 } 130 131 138 public long skip(long count) throws IOException { 139 int attempts = 0; 140 for (;;) { 141 if (checkCancellation()) throw new OperationCanceledException(); 142 try { 143 return in.skip(count); 144 } catch (InterruptedIOException e) { 145 if (e.bytesTransferred != 0) return e.bytesTransferred; if (++attempts == numAttempts) 147 throw new InterruptedIOException (Messages.PollingInputStream_readTimeout); 148 if (DEBUG) System.out.println("read retry=" + attempts); } 150 } 151 } 152 153 157 protected void readPendingInput() throws IOException { 158 byte[] buffer= new byte[2048]; 159 while (true) { 160 int available = in.available(); 161 if (available < 1) break; 162 if (available > buffer.length) available = buffer.length; 163 if (in.read(buffer, 0, available) < 1) break; 164 } 165 } 166 167 173 public void setIsCancellable(boolean cancellable) { 174 this.cancellable = cancellable; 175 } 176 177 183 private boolean checkCancellation() { 184 if(cancellable) { 185 return monitor.isCanceled(); 186 } else { 187 return false; 188 } 189 } 190 } 191 | Popular Tags |