##### Coverage Report - com.sun.grizzly.util.ByteBufferFactory

Classes in this File | Line Coverage | Branch Coverage | Complexity
ByteBufferFactory |
52 % | 13/25
32 % | 7/22
| 0.0;0
ByteBufferFactory$ByteBufferType |
100 % | 1/1
N/A | N/A
| 0.0;0

1 |  |
     /*
2 |  |
      *
3 |  |
      * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4 |  |
      *
5 |  |
      * Copyright 2007-2008 Sun Microsystems, Inc. All rights reserved.
6 |  |
      *
7 |  |
      * The contents of this file are subject to the terms of either the GNU
8 |  |
      * General Public License Version 2 only ("GPL") or the Common Development
9 |  |
      * and Distribution License("CDDL") (collectively, the "License").  You
10 |  |
      * may not use this file except in compliance with the License. You can obtain
11 |  |
      * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
12 |  |
      * or glassfish/bootstrap/legal/LICENSE.txt.  See the License for the specific
13 |  |
      * language governing permissions and limitations under the License.
14 |  |
      *
15 |  |
      * When distributing the software, include this License Header Notice in each
16 |  |
      * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
17 |  |
      * Sun designates this particular file as subject to the "Classpath" exception
18 |  |
      * as provided by Sun in the GPL Version 2 section of the License file that
19 |  |
      * accompanied this code.  If applicable, add the following below the License
20 |  |
      * Header, with the fields enclosed by brackets [] replaced by your own
21 |  |
      * identifying information: "Portions Copyrighted [year]
22 |  |
      * [name of copyright owner]"
23 |  |
      *
24 |  |
      * Contributor(s):
25 |  |
      *
26 |  |
      * If you wish your version of this file to be governed by only the CDDL or
27 |  |
      * only the GPL Version 2, indicate your decision by adding "[Contributor]
28 |  |
      * elects to include this software in this distribution under the [CDDL or GPL
29 |  |
      * Version 2] license."  If you don't indicate a single choice of license, a
30 |  |
      * recipient has the option to distribute your version of this file under
31 |  |
      * either the CDDL, the GPL Version 2 or to extend the choice of license to
32 |  |
      * its licensees as provided above.  However, if you add GPL Version 2 code
33 |  |
      * and therefore, elected the GPL Version 2 license, then the option applies
34 |  |
      * only if the new code is made subject to such option by the copyright
35 |  |
      * holder.
36 |  |
      *
37 |  |
      */
38 |  |
39 |  |
     package com.sun.grizzly.util;
40 |  |
41 |  |
     import java.nio.ByteBuffer;
42 |  |
43 |  |
44 |  |
     /**
45 |  |
      * Factory class used to create {@link ByteBuffer}.
46 |  |
      *
47 |  |
      * The ByteBuffer can by direct (ByteBufferType.DIRECT) or heap (ByteBufferType.HEAP)
48 |  |
      * a view (ByteBufferType.DIRECT_VIEW) or ByteBufferType.HEAP_VIEW)
49 |  |
      * or backed by an array (ByteBufferType.HEAP_ARRAY).
50 |  |
      *
51 |  |
      * @author Jean-Francois Arcand
52 |  |
      */
53 |  |
     public class ByteBufferFactory{
54 |  |
55 |  |
         /**
56 |  |
          * An enumeration of all type of ByteBuffer this object can create.
57 |  |
          */
58 | 6 |
         public enum ByteBufferType { DIRECT, HEAP, DIRECT_VIEW, HEAP_VIEW, HEAP_ARRAY }
59 |  |
60 |  |
61 |  |
         /**
62 |  |
          * The default capacity of the default view of a {@link ByteBuffer}
63 |  |
          */
64 | 1 |
         public static int defaultCapacity = 8192;
65 |  |
66 |  |
67 |  |
         /**
68 |  |
          * The default capacity of the {@link ByteBuffer} from which views
69 |  |
          * will be created.
70 |  |
          */
71 | 1 |
         public static int capacity = 4000000;
72 |  |
73 |  |
74 |  |
         /**
75 |  |
          * The {@link ByteBuffer} used to create direct byteBuffer view.
76 |  |
          */
77 |  |
         private static ByteBuffer directByteBuffer;
78 |  |
79 |  |
80 |  |
         /**
81 |  |
          * The {@link ByteBuffer} used to create direct byteBuffer view.
82 |  |
          */
83 |  |
         private static ByteBuffer heapByteBuffer;
84 |  |
85 |  |
86 |  |
         /**
87 |  |
          * Private constructor.
88 |  |
          */
89 | 0 |
         private ByteBufferFactory(){
90 | 0 |
         }
91 |  |
92 |  |
93 |  |
         /**
94 |  |
          * Return a direct {@link ByteBuffer} view
95 |  |
          * @param size the Size of the {@link ByteBuffer}
96 |  |
          * @param direct - direct or non-direct buffer?
97 |  |
          * @return {@link ByteBuffer}
98 |  |
          */
99 |  |
         public synchronized static ByteBuffer allocateView(int size, boolean direct){
100 | 638 |
             if (direct && (directByteBuffer == null ||
101 |  |
                    (directByteBuffer.capacity() - directByteBuffer.limit() < size))){
102 | 0 |
                 directByteBuffer = ByteBuffer.allocateDirect(capacity);
103 | 638 |
             } else if (heapByteBuffer == null ||
104 |  |
                    (heapByteBuffer.capacity() - heapByteBuffer.limit() < size)){
105 | 1 |
                 heapByteBuffer = ByteBuffer.allocate(capacity);
106 |  |
             }
107 | 638 |
             ByteBuffer byteBuffer = (direct ? directByteBuffer : heapByteBuffer);
108 |  |
109 | 638 |
             byteBuffer.limit(byteBuffer.position() + size);
110 | 638 |
             ByteBuffer view = byteBuffer.slice();
111 | 638 |
             byteBuffer.position(byteBuffer.limit());
112 |  |
113 | 638 |
             return view;
114 |  |
         }
115 |  |
116 |  |
117 |  |
         /**
118 |  |
          * Return a direct {@link ByteBuffer} view using the default size.
119 |  |
          * @param direct - direct or non-direct buffer
120 |  |
          * @return {@link ByteBuffer}
121 |  |
          */
122 |  |
         public synchronized static ByteBuffer allocateView(boolean direct){
123 | 0 |
             return allocateView(defaultCapacity, direct);
124 |  |
         }
125 |  |
126 |  |
127 |  |
         /**
128 |  |
          * Return a new ByteBuffer based on the requested <code>ByteBufferType</code>
129 |  |
          * @param type the requested <code>ByteBufferType</code>
130 |  |
          * @param size the {@link ByteBuffer} size.
131 |  |
          * @return a new ByteBuffer based on the requested <code>ByteBufferType</code>
132 |  |
          */
133 |  |
         public static ByteBuffer allocate(ByteBufferType type,int size){
134 | 363 |
             if (type == ByteBufferType.HEAP){
135 | 0 |
                 return ByteBuffer.allocate(size);
136 | 363 |
             } else if (type == ByteBufferType.HEAP_VIEW) {
137 | 363 |
                 return allocateView(size,false);
138 | 0 |
             } else if (type == ByteBufferType.HEAP_ARRAY) {
139 | 0 |
                 return ByteBuffer.wrap(new byte[size]);
140 | 0 |
             } else if (type == ByteBufferType.DIRECT){
141 | 0 |
                return ByteBuffer.allocateDirect(size);
142 | 0 |
             } else if (type == ByteBufferType.DIRECT_VIEW){
143 | 0 |
                 return allocateView(size,true);
144 |  |
             } else {
145 | 0 |
                 throw new IllegalStateException("Invalid ByteBuffer Type");
146 |  |
             }
147 |  |
         }
148 |  |
     }