KickJava   Java API By Example, From Geeks To Geeks.

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


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

24 package com.sun.enterprise.web.connector.grizzly;
25
26 import java.nio.ByteBuffer JavaDoc;
27
28
29 /**
30  * Factory class used to create views of a <code>ByteBuffer</code>.
31  * The ByteBuffer can by direct or not.
32  *
33  * @author Jean-Francois Arcand
34  */

35 public class ByteBufferFactory{
36
37     
38     /**
39      * The default capacity of the default view of a <code>ByteBuffer</code>
40      */

41     public static int defaultCapacity = 9000;
42     
43     
44     /**
45      * The default capacity of the <code>ByteBuffer</code> from which views
46      * will be created.
47      */

48     public static int capacity = 4000000;
49     
50     
51     /**
52      * The <code>ByteBuffer</code> used to create views.
53      */

54     private static ByteBuffer JavaDoc byteBuffer;
55             
56     
57     /**
58      * Private constructor.
59      */

60     private ByteBufferFactory(){
61     }
62     
63     
64     /**
65      * Return a direct <code>ByteBuffer</code> view
66      * @param size the Size of the <code>ByteBuffer</code>
67      */

68     public synchronized static ByteBuffer JavaDoc allocateView(int size, boolean direct){
69         if (byteBuffer == null ||
70                (byteBuffer.capacity() - byteBuffer.limit() < size)){
71             if ( direct )
72                 byteBuffer = ByteBuffer.allocateDirect(capacity);
73             else
74                 byteBuffer = ByteBuffer.allocate(capacity);
75         }
76
77         byteBuffer.limit(byteBuffer.position() + size);
78         ByteBuffer JavaDoc view = byteBuffer.slice();
79         byteBuffer.position(byteBuffer.limit());
80         
81         return view;
82     }
83
84     
85     /**
86      * Return a direct <code>ByteBuffer</code> view using the default size.
87      */

88     public synchronized static ByteBuffer JavaDoc allocateView(boolean direct){
89         return allocateView(defaultCapacity, direct);
90     }
91      
92 }
93
Popular Tags