KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > web > connector > grizzly > OutputWriter


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.enterprise.web.connector.grizzly;
25
26 import java.io.EOFException JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.nio.ByteBuffer JavaDoc;
29 import java.nio.channels.ClosedChannelException JavaDoc;
30 import java.nio.channels.SelectionKey JavaDoc;
31 import java.nio.channels.Selector JavaDoc;
32 import java.nio.channels.SocketChannel JavaDoc;
33
34 /**
35  * NIO utility to flush <code>ByteBuffer</code>
36  *
37  * @author Scott Oaks
38  */

39 public final class OutputWriter {
40     
41     /**
42      * Flush the buffer by looping until the <code>ByteBuffer</code> is empty
43      * @param bb the ByteBuffer to write.
44      */

45     public static void flushChannel(SocketChannel JavaDoc socketChannel, ByteBuffer JavaDoc bb)
46             throws IOException JavaDoc{
47         SelectionKey JavaDoc key = null;
48         Selector JavaDoc 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 JavaDoc();
56                 }
57             
58                 if (len == 0) {
59                     if ( writeSelector == null ){
60                         writeSelector = SelectorFactory.getSelector();
61                         if ( writeSelector == null){
62                             // Continue using the main one.
63
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 JavaDoc("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                 // Cancel the key.
87
writeSelector.selectNow();
88                 SelectorFactory.returnSelector(writeSelector);
89             }
90         }
91     }
92 }
93
Popular Tags