1 11 package org.eclipse.team.internal.core.streams; 12 13 import java.io.FilterOutputStream ; 14 import java.io.IOException ; 15 import java.io.InterruptedIOException ; 16 import java.io.OutputStream ; 17 18 import org.eclipse.core.runtime.IProgressMonitor; 19 import org.eclipse.core.runtime.OperationCanceledException; 20 import org.eclipse.team.internal.core.Messages; 21 import org.eclipse.team.internal.core.Policy; 22 23 32 public class PollingOutputStream extends FilterOutputStream { 33 private static final boolean DEBUG = Policy.DEBUG_STREAMS; 34 private int numAttempts; 35 private IProgressMonitor monitor; 36 private boolean cancellable; 37 38 45 public PollingOutputStream(OutputStream out, int numAttempts, IProgressMonitor monitor) { 46 super(out); 47 this.numAttempts = numAttempts; 48 this.monitor = monitor; 49 this.cancellable = true; 50 } 51 52 59 public void write(int b) throws IOException { 60 int attempts = 0; 61 for (;;) { 62 if (checkCancellation()) throw new OperationCanceledException(); 63 try { 64 out.write(b); 65 return; 66 } catch (InterruptedIOException e) { 67 if (++attempts == numAttempts) 68 throw new InterruptedIOException (Messages.PollingOutputStream_writeTimeout); 69 if (DEBUG) System.out.println("write retry=" + attempts); } 71 } 72 } 73 74 81 public void write(byte[] buffer, int off, int len) throws IOException { 82 int count = 0; 83 int attempts = 0; 84 for (;;) { 85 if (checkCancellation()) throw new OperationCanceledException(); 86 try { 87 out.write(buffer, off, len); 88 return; 89 } catch (InterruptedIOException e) { 90 int amount = e.bytesTransferred; 91 if (amount != 0) { len -= amount; 93 if (len <= 0) return; 94 off += amount; 95 count += amount; 96 attempts = 0; } 98 if (++attempts == numAttempts) { 99 e = new InterruptedIOException (Messages.PollingOutputStream_writeTimeout); 100 e.bytesTransferred = count; 101 throw e; 102 } 103 if (DEBUG) System.out.println("write retry=" + attempts); } 105 } 106 } 107 108 115 public void flush() throws IOException { 116 int count = 0; 117 int attempts = 0; 118 for (;;) { 119 if (checkCancellation()) throw new OperationCanceledException(); 120 try { 121 out.flush(); 122 return; 123 } catch (InterruptedIOException e) { 124 int amount = e.bytesTransferred; 125 if (amount != 0) { count += amount; 127 attempts = 0; } 129 if (++attempts == numAttempts) { 130 e = new InterruptedIOException (Messages.PollingOutputStream_writeTimeout); 131 e.bytesTransferred = count; 132 throw e; 133 } 134 if (DEBUG) System.out.println("write retry=" + attempts); } 136 } 137 } 138 139 146 public void close() throws IOException { 147 int attempts = numAttempts - 1; try { 149 out.flush(); 150 attempts = 0; 151 } finally { 152 boolean stop = false; 153 while (!stop) { 154 try { 155 out.close(); 156 stop = true; 157 } catch (InterruptedIOException e) { 158 if (checkCancellation()) throw new OperationCanceledException(); 159 if (++attempts == numAttempts) 160 throw new InterruptedIOException (Messages.PollingOutputStream_closeTimeout); 161 if (DEBUG) System.out.println("close retry=" + attempts); } 163 } 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 |