Skip to content

Package: AbstractBufferArray

AbstractBufferArray

nameinstructionbranchcomplexitylinemethod
AbstractBufferArray(Class)
M: 16 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
add(Object)
M: 10 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
add(Object, int, int)
M: 50 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 12 C: 0
0%
M: 1 C: 0
0%
ensureCapacity(int)
M: 40 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 7 C: 0
0%
M: 1 C: 0
0%
getArray()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getInitialBufferSize(int)
M: 8 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getInitialLimit(int)
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getInitialPosition(int)
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
recycle()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
reset()
M: 11 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
restore()
M: 24 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
size()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*
2: * Copyright (c) 2010, 2020 Oracle and/or its affiliates. All rights reserved.
3: *
4: * This program and the accompanying materials are made available under the
5: * terms of the Eclipse Public License v. 2.0, which is available at
6: * http://www.eclipse.org/legal/epl-2.0.
7: *
8: * This Source Code may also be made available under the following Secondary
9: * Licenses when the conditions for such availability set forth in the
10: * Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
11: * version 2 with the GNU Classpath Exception, which is available at
12: * https://www.gnu.org/software/classpath/license.html.
13: *
14: * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
15: */
16:
17: package org.glassfish.grizzly.memory;
18:
19: import java.lang.reflect.Array;
20: import java.util.Arrays;
21:
22: /**
23: *
24: * @author oleksiys
25: */
26: public abstract class AbstractBufferArray<E> {
27: protected final Class<E> clazz;
28: private E[] byteBufferArray;
29: private PosLim[] initStateArray;
30:
31: private int size;
32:
33: protected abstract void setPositionLimit(E buffer, int position, int limit);
34:
35: protected abstract int getPosition(E buffer);
36:
37: protected abstract int getLimit(E buffer);
38:
39: @SuppressWarnings("unchecked")
40: protected AbstractBufferArray(Class<E> clazz) {
41: this.clazz = clazz;
42: byteBufferArray = (E[]) Array.newInstance(clazz, 4);
43: initStateArray = new PosLim[4];
44: }
45:
46: public void add(final E byteBuffer) {
47: add(byteBuffer, getPosition(byteBuffer), getLimit(byteBuffer));
48: }
49:
50: public void add(final E byteBuffer, final int restorePosition, final int restoreLimit) {
51:
52: ensureCapacity(1);
53: byteBufferArray[size] = byteBuffer;
54: PosLim poslim = initStateArray[size];
55:• if (poslim == null) {
56: poslim = new PosLim();
57: initStateArray[size] = poslim;
58: }
59:
60: poslim.initialPosition = getPosition(byteBuffer);
61: poslim.initialLimit = getLimit(byteBuffer);
62: poslim.restorePosition = restorePosition;
63: poslim.restoreLimit = restoreLimit;
64:
65: size++;
66: }
67:
68: public E[] getArray() {
69: return byteBufferArray;
70: }
71:
72: public void restore() {
73:• for (int i = 0; i < size; i++) {
74: final PosLim poslim = initStateArray[i];
75: setPositionLimit(byteBufferArray[i], poslim.restorePosition, poslim.restoreLimit);
76: }
77: }
78:
79: public final int getInitialPosition(final int idx) {
80: return initStateArray[idx].initialPosition;
81: }
82:
83: public int getInitialLimit(final int idx) {
84: return initStateArray[idx].initialLimit;
85: }
86:
87: public final int getInitialBufferSize(final int idx) {
88: return getInitialLimit(idx) - getInitialPosition(idx);
89: }
90:
91: public int size() {
92: return size;
93: }
94:
95: private void ensureCapacity(final int grow) {
96: final int diff = byteBufferArray.length - size;
97:• if (diff >= grow) {
98: return;
99: }
100:
101: final int newSize = Math.max(diff + size, byteBufferArray.length * 3 / 2 + 1);
102: byteBufferArray = Arrays.copyOf(byteBufferArray, newSize);
103: initStateArray = Arrays.copyOf(initStateArray, newSize);
104: }
105:
106: public void reset() {
107: Arrays.fill(byteBufferArray, 0, size, null);
108: size = 0;
109: }
110:
111: public void recycle() {
112: reset();
113: }
114:
115: private final static class PosLim {
116: int initialPosition;
117: int initialLimit;
118:
119: int restorePosition;
120: int restoreLimit;
121: }
122: }