1 5 6 9 10 package java.nio.channels.spi; 11 12 import java.io.IOException ; 13 import java.lang.reflect.Method ; 14 import java.lang.reflect.InvocationTargetException ; 15 import java.nio.channels.*; 16 import java.security.AccessController ; 17 import java.security.PrivilegedAction ; 18 import sun.nio.ch.Interruptible; 19 20 21 68 69 public abstract class AbstractInterruptibleChannel 70 implements Channel, InterruptibleChannel 71 { 72 73 private Object closeLock = new Object (); 74 private volatile boolean open = true; 75 76 79 protected AbstractInterruptibleChannel() { } 80 81 92 public final void close() throws IOException { 93 synchronized (closeLock) { 94 if (!open) 95 return; 96 open = false; 97 implCloseChannel(); 98 } 99 } 100 101 117 protected abstract void implCloseChannel() throws IOException ; 118 119 public final boolean isOpen() { 120 return open; 121 } 122 123 124 126 private Interruptible interruptor; 127 private volatile boolean interrupted = false; 128 129 137 protected final void begin() { 138 if (interruptor == null) { 139 interruptor = new Interruptible() { 140 public void interrupt() { 141 synchronized (closeLock) { 142 if (!open) 143 return; 144 interrupted = true; 145 open = false; 146 try { 147 AbstractInterruptibleChannel.this.implCloseChannel(); 148 } catch (IOException x) { } 149 } 150 }}; 151 } 152 blockedOn(interruptor); 153 if (Thread.currentThread().isInterrupted()) 154 interruptor.interrupt(); 155 } 156 157 176 protected final void end(boolean completed) 177 throws AsynchronousCloseException 178 { 179 blockedOn(null); 180 if (completed) { 181 interrupted = false; 182 return; 183 } 184 if (interrupted) throw new ClosedByInterruptException(); 185 if (!open) throw new AsynchronousCloseException(); 186 } 187 188 189 static void blockedOn(Interruptible intr) { sun.misc.SharedSecrets.getJavaLangAccess().blockedOn(Thread.currentThread(), 192 intr); 193 } 194 } 195 | Popular Tags |