KickJava   Java API By Example, From Geeks To Geeks.

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


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.IOException JavaDoc;
27 import java.nio.ByteBuffer JavaDoc;
28 import java.nio.channels.SocketChannel JavaDoc;
29
30 import org.apache.coyote.Response;
31 import org.apache.coyote.http11.InternalOutputBuffer;
32
33
34
35 /**
36  * Output buffer.
37  * Buffer the bytes until the <code>ByteChunk</code> is full or the request
38  * is completed.
39  *
40  * @author Jean-Francois Arcand
41  * @author Scott Oaks
42  */

43 public class SocketChannelOutputBuffer extends InternalOutputBuffer{
44
45     // ----------------------------------------------------------- Constructors
46

47
48     /**
49      * Alternate constructor.
50      */

51     public SocketChannelOutputBuffer(Response response, int headerBufferSize) {
52
53         super(response,headerBufferSize);
54
55         socketBuffer.allocate(headerBufferSize, headerBufferSize);
56         useSocketBuffer = true;
57     }
58
59
60     // -------------------------------------------------------------- Variables
61

62     /**
63      * Underlying output socketChannel.
64      */

65     protected SocketChannel JavaDoc socketChannel;
66     
67     // ------------------------------------------------------------- Properties
68

69
70     /**
71      * Set the underlying socket output stream.
72      */

73     public void setChannel(SocketChannel JavaDoc socketChannel) {
74         this.socketChannel = socketChannel;
75     }
76
77
78     /**
79      * Get the underlying socket output stream.
80      */

81     public SocketChannel JavaDoc getChannel() {
82         return socketChannel;
83     }
84
85     
86     // --------------------------------------------------------- Public Methods
87

88     /**
89      * Send an acknoledgement without buffering.
90      */

91     public void sendAck() throws IOException JavaDoc {
92
93         if (!committed)
94             flushChannel(ByteBuffer.wrap(Constants.ACK_BYTES));
95
96     }
97
98     
99     /**
100      * Callback to write data from the buffer.
101      */

102     public void realWriteBytes(byte cbuf[], int off, int len)
103         throws IOException JavaDoc {
104         if (len > 0) {
105             flushChannel(ByteBuffer.wrap(cbuf,off,len));
106         }
107     }
108     
109     
110     /**
111      * Flush the buffer by looping until the <code>ByteBuffer</code> is empty
112      * @param bb the ByteBuffer to write.
113      */

114     public void flushChannel(ByteBuffer JavaDoc bb) throws IOException JavaDoc{
115         OutputWriter.flushChannel(socketChannel, bb);
116     }
117 }
118
Popular Tags