1 23 24 package com.sun.enterprise.web.connector.grizzly; 25 26 import java.io.EOFException ; 27 import java.io.IOException ; 28 import java.nio.ByteBuffer ; 29 import java.nio.channels.ClosedChannelException ; 30 import java.nio.channels.SelectionKey ; 31 import java.nio.channels.Selector ; 32 import java.nio.channels.SocketChannel ; 33 34 39 public final class OutputWriter { 40 41 45 public static void flushChannel(SocketChannel socketChannel, ByteBuffer bb) 46 throws IOException { 47 SelectionKey key = null; 48 Selector writeSelector = null; 49 int attempts = 0; 50 try { 51 while ( bb.hasRemaining() ) { 52 int len = socketChannel.write(bb); 53 attempts++; 54 if (len < 0){ 55 throw new EOFException (); 56 } 57 58 if (len == 0) { 59 if ( writeSelector == null ){ 60 writeSelector = SelectorFactory.getSelector(); 61 if ( writeSelector == null){ 62 continue; 64 } 65 } 66 67 key = socketChannel.register(writeSelector, key.OP_WRITE); 68 69 if (writeSelector.select(30 * 1000) == 0) { 70 if (attempts > 2) 71 throw new IOException ("Client disconnected"); 72 } else { 73 attempts--; 74 } 75 } else { 76 attempts = 0; 77 } 78 } 79 } finally { 80 if (key != null) { 81 key.cancel(); 82 key = null; 83 } 84 85 if ( writeSelector != null ) { 86 writeSelector.selectNow(); 88 SelectorFactory.returnSelector(writeSelector); 89 } 90 } 91 } 92 } 93 | Popular Tags |