Skip to content

Package: HeapBuffer

HeapBuffer

nameinstructionbranchcomplexitylinemethod
HeapBuffer()
M: 15 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%
HeapBuffer(byte[], int, int)
M: 28 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 10 C: 0
0%
M: 1 C: 0
0%
allowBufferDispose()
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%
allowBufferDispose(boolean)
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
array()
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%
arrayOffset()
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%
asReadOnlyBuffer()
M: 24 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%
capacity()
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
checkDispose()
M: 11 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
clear()
M: 12 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
compact()
M: 25 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
compareTo(Buffer)
M: 46 C: 0
0%
M: 6 C: 0
0%
M: 4 C: 0
0%
M: 10 C: 0
0%
M: 1 C: 0
0%
createHeapBuffer(int, int)
M: 13 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
dispose()
M: 27 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 10 C: 0
0%
M: 1 C: 0
0%
dumpHex(Appendable)
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
duplicate()
M: 20 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
equals(Object)
M: 49 C: 0
0%
M: 8 C: 0
0%
M: 5 C: 0
0%
M: 12 C: 0
0%
M: 1 C: 0
0%
flip()
M: 12 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
get()
M: 21 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
get(ByteBuffer)
M: 22 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
get(ByteBuffer, int, int)
M: 35 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 7 C: 0
0%
M: 1 C: 0
0%
get(byte[])
M: 7 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
get(byte[], int, int)
M: 27 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
get(int)
M: 18 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
getChar()
M: 27 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
getChar(int)
M: 22 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
getDouble()
M: 27 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
getDouble(int)
M: 22 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
getFloat()
M: 27 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
getFloat(int)
M: 22 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
getInt()
M: 27 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
getInt(int)
M: 22 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
getLong()
M: 27 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
getLong(int)
M: 22 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
getShort()
M: 27 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
getShort(int)
M: 22 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
hasArray()
M: 2 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
hasRemaining()
M: 9 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
hashCode()
M: 105 C: 0
0%
M: 12 C: 0
0%
M: 7 C: 0
0%
M: 12 C: 0
0%
M: 1 C: 0
0%
isComposite()
M: 2 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
isDirect()
M: 2 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
isExternal()
M: 2 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
isReadOnly()
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%
limit()
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
limit(int)
M: 15 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
mark()
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
onShareHeap()
M: 1 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
order()
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%
order(ByteOrder)
M: 14 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
position()
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
position(int)
M: 15 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
prepareDispose()
M: 7 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
prepend(Buffer)
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
put(Buffer)
M: 15 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
put(Buffer, int, int)
M: 44 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 10 C: 0
0%
M: 1 C: 0
0%
put(ByteBuffer)
M: 22 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
put(ByteBuffer, int, int)
M: 35 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 7 C: 0
0%
M: 1 C: 0
0%
put(byte)
M: 23 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
put(byte[])
M: 7 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
put(byte[], int, int)
M: 27 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
put(int, byte)
M: 20 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
put8BitString(String)
M: 30 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%
putChar(char)
M: 27 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
putChar(int, char)
M: 24 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
putDouble(double)
M: 27 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
putDouble(int, double)
M: 24 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
putFloat(float)
M: 27 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
putFloat(int, float)
M: 24 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
putInt(int)
M: 27 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
putInt(int, int)
M: 24 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
putLong(int, long)
M: 24 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
putLong(long)
M: 27 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
putShort(int, short)
M: 24 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
putShort(short)
M: 27 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
release()
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%
remaining()
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%
reset()
M: 14 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
rewind()
M: 8 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
shrink()
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%
slice()
M: 7 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
slice(int, int)
M: 9 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
split(int)
M: 77 C: 0
0%
M: 10 C: 0
0%
M: 6 C: 0
0%
M: 19 C: 0
0%
M: 1 C: 0
0%
static {...}
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%
toBufferArray()
M: 7 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
toBufferArray(BufferArray)
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
toBufferArray(BufferArray, int, int)
M: 19 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%
toBufferArray(int, 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%
toByteBuffer()
M: 7 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
toByteBuffer(int, 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%
toByteBuffer0(int, int, boolean)
M: 28 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
toByteBufferArray()
M: 8 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
toByteBufferArray(ByteBufferArray)
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
toByteBufferArray(ByteBufferArray, int, int)
M: 8 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
toByteBufferArray(int, 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%
toString()
M: 41 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 9 C: 0
0%
M: 1 C: 0
0%
toStringContent()
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%
toStringContent(Charset)
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%
toStringContent(Charset, int, int)
M: 47 C: 0
0%
M: 8 C: 0
0%
M: 5 C: 0
0%
M: 13 C: 0
0%
M: 1 C: 0
0%
trim()
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
tryDispose()
M: 9 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
underlying()
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
wrap(byte[])
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%
wrap(byte[], int, int)
M: 7 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.nio.BufferOverflowException;
20: import java.nio.BufferUnderflowException;
21: import java.nio.ByteBuffer;
22: import java.nio.ByteOrder;
23: import java.nio.InvalidMarkException;
24: import java.nio.charset.Charset;
25: import java.util.Arrays;
26:
27: import org.glassfish.grizzly.Buffer;
28:
29: /**
30: * {@link Buffer} implementation, which uses the {@link ByteBuffer} underneath.
31: *
32: * @see Buffer
33: * @see MemoryManager
34: * @see ByteBuffer
35: *
36: * @author Ken Cavanaugh
37: * @author John Vieten
38: * @author Alexey Stashok
39: * @author Ryan Lubke
40: *
41: * @since 2.0
42: */
43: public class HeapBuffer implements Buffer {
44: public static volatile boolean DEBUG_MODE = false;
45:
46: // Dispose underlying Buffer flag
47: protected boolean allowBufferDispose = false;
48:
49: protected Exception disposeStackTrace;
50:
51: protected byte[] heap;
52:
53: protected int offset;
54:
55: protected int pos;
56:
57: protected int cap;
58:
59: protected int lim;
60:
61: protected int mark = -1;
62:
63: protected boolean readOnly;
64:
65: protected ByteOrder order = ByteOrder.BIG_ENDIAN;
66:
67: protected boolean bigEndian = true;
68:
69: protected ByteBuffer byteBuffer;
70:
71: // ------------------------------------------------------------ Constructors
72:
73: protected HeapBuffer() {
74: }
75:
76: protected HeapBuffer(final byte[] heap, final int offset, final int cap) {
77: this.heap = heap;
78: this.offset = offset;
79: this.cap = cap;
80: this.lim = this.cap;
81: }
82:
83: @Override
84: public final boolean isComposite() {
85: return false;
86: }
87:
88: @Override
89: public HeapBuffer prepend(final Buffer header) {
90: checkDispose();
91: return this;
92: }
93:
94: @Override
95: public void trim() {
96: checkDispose();
97: flip();
98: }
99:
100: @Override
101: public void shrink() {
102: checkDispose();
103: }
104:
105: @Override
106: public final boolean allowBufferDispose() {
107: return allowBufferDispose;
108: }
109:
110: @Override
111: public final void allowBufferDispose(boolean allowBufferDispose) {
112: this.allowBufferDispose = allowBufferDispose;
113: }
114:
115: @Override
116: public final boolean tryDispose() {
117:• if (allowBufferDispose) {
118: dispose();
119: return true;
120: }
121:
122: return false;
123: }
124:
125: @Override
126: public void dispose() {
127: prepareDispose();
128:
129: byteBuffer = null;
130: heap = null;
131: pos = 0;
132: offset = 0;
133: lim = 0;
134: cap = 0;
135: order = ByteOrder.BIG_ENDIAN;
136: bigEndian = true;
137: }
138:
139: protected final void prepareDispose() {
140: checkDispose();
141:• if (DEBUG_MODE) { // if debug is on - clear the buffer content
142: // Use static logic class to help JIT optimize the code
143: DebugLogic.doDebug(this);
144: }
145: }
146:
147: @Override
148: public ByteBuffer underlying() {
149: checkDispose();
150: return toByteBuffer();
151: }
152:
153: @Override
154: public final int capacity() {
155: checkDispose();
156: return cap;
157: }
158:
159: @Override
160: public final int position() {
161: checkDispose();
162: return pos;
163: }
164:
165: @Override
166: public final HeapBuffer position(final int newPosition) {
167: checkDispose();
168: pos = newPosition;
169:• if (mark > pos) {
170: mark = -1;
171: }
172: return this;
173: }
174:
175: @Override
176: public final int limit() {
177: checkDispose();
178: return lim;
179: }
180:
181: @Override
182: public final HeapBuffer limit(final int newLimit) {
183: checkDispose();
184: lim = newLimit;
185:• if (mark > lim) {
186: mark = -1;
187: }
188: return this;
189: }
190:
191: @Override
192: public final HeapBuffer mark() {
193: mark = pos;
194: return this;
195: }
196:
197: @Override
198: public final HeapBuffer reset() {
199: int m = mark;
200:• if (m < 0) {
201: throw new InvalidMarkException();
202: }
203: pos = m;
204: return this;
205: }
206:
207: @Override
208: public final HeapBuffer clear() {
209: pos = 0;
210: lim = cap;
211: mark = -1;
212: return this;
213: }
214:
215: @Override
216: public final HeapBuffer flip() {
217: lim = pos;
218: pos = 0;
219: mark = -1;
220: return this;
221: }
222:
223: @Override
224: public final HeapBuffer rewind() {
225: pos = 0;
226: mark = -1;
227: return this;
228: }
229:
230: @Override
231: public final int remaining() {
232: return lim - pos;
233: }
234:
235: @Override
236: public final boolean hasRemaining() {
237:• return pos < lim;
238: }
239:
240: @Override
241: public boolean isReadOnly() {
242: return readOnly;
243: }
244:
245: @Override
246: public final boolean isDirect() {
247: return false;
248: }
249:
250: /**
251: * {@inheritDoc}
252: */
253: @Override
254: public Buffer split(final int splitPosition) {
255: checkDispose();
256:
257:• if (splitPosition < 0 || splitPosition > cap) {
258: throw new IllegalArgumentException("Invalid splitPosition value, should be 0 <= splitPosition <= capacity");
259: }
260:
261:• if (mark >= splitPosition) {
262: mark = -1;
263: }
264:
265: final int oldPosition = pos;
266: final int oldLimit = lim;
267:
268: final HeapBuffer ret = createHeapBuffer(splitPosition, cap - splitPosition);
269:
270: cap = splitPosition;
271:
272:• if (oldPosition < splitPosition) {
273: pos = oldPosition;
274: } else {
275: pos = cap;
276: ret.position(oldPosition - splitPosition);
277: }
278:
279:• if (oldLimit < splitPosition) {
280: lim = oldLimit;
281: ret.limit(0);
282: } else {
283: lim = cap;
284: ret.limit(oldLimit - splitPosition);
285: }
286:
287: return ret;
288: }
289:
290: @Override
291: public HeapBuffer slice() {
292: return slice(pos, lim);
293: }
294:
295: @Override
296: public HeapBuffer slice(final int position, final int limit) {
297: checkDispose();
298: return createHeapBuffer(position, limit - position);
299: }
300:
301: @Override
302: public HeapBuffer duplicate() {
303: checkDispose();
304:
305: final HeapBuffer duplicate = createHeapBuffer(0, cap);
306: duplicate.position(pos);
307: duplicate.limit(lim);
308: return duplicate;
309: }
310:
311: @Override
312: public HeapBuffer asReadOnlyBuffer() {
313: checkDispose();
314:
315: onShareHeap();
316: final HeapBuffer b = new ReadOnlyHeapBuffer(heap, offset, cap);
317: b.pos = pos;
318: b.lim = lim;
319: return b;
320: }
321:
322: @Override
323: public byte get() {
324:• if (!hasRemaining()) {
325: throw new BufferUnderflowException();
326: }
327: return heap[offset + pos++];
328: }
329:
330: @Override
331: public byte get(int index) {
332:• if (index < 0 || index >= lim) {
333: throw new IndexOutOfBoundsException();
334: }
335: return heap[offset + index];
336: }
337:
338: @Override
339: public HeapBuffer put(byte b) {
340:• if (!hasRemaining()) {
341: throw new BufferOverflowException();
342: }
343: heap[offset + pos++] = b;
344: return this;
345: }
346:
347: @Override
348: public HeapBuffer put(int index, byte b) {
349:• if (index < 0 || index >= lim) {
350: throw new IndexOutOfBoundsException();
351: }
352: heap[offset + index] = b;
353: return this;
354: }
355:
356: @Override
357: public HeapBuffer get(final byte[] dst) {
358: return get(dst, 0, dst.length);
359: }
360:
361: @Override
362: public HeapBuffer get(final byte[] dst, final int offset, final int length) {
363:• if (remaining() < length) {
364: throw new BufferUnderflowException();
365: }
366: System.arraycopy(heap, this.offset + pos, dst, offset, length);
367: pos += length;
368: return this;
369: }
370:
371: @Override
372: public HeapBuffer put(final Buffer src) {
373: put(src, src.position(), src.remaining());
374: src.position(src.limit());
375: return this;
376: }
377:
378: @Override
379: public HeapBuffer put(final Buffer src, final int position, final int length) {
380:• if (remaining() < length) {
381: throw new BufferOverflowException();
382: }
383:
384: final int oldPos = src.position();
385: final int oldLim = src.limit();
386:
387: final int thisPos = pos; // Save the current pos for case, if src == this
388: Buffers.setPositionLimit(src, position, position + length);
389:
390: src.get(heap, offset + thisPos, length);
391: Buffers.setPositionLimit(src, oldPos, oldLim);
392: pos = thisPos + length;
393:
394: return this;
395: }
396:
397: @Override
398: public Buffer get(final ByteBuffer dst) {
399: final int length = dst.remaining();
400:
401: dst.put(heap, offset + pos, length);
402: pos += length;
403:
404: return this;
405: }
406:
407: @Override
408: public Buffer get(final ByteBuffer dst, final int position, final int length) {
409: final int oldPos = dst.position();
410: final int oldLim = dst.limit();
411:
412: try {
413: Buffers.setPositionLimit(dst, position, position + length);
414: dst.put(heap, offset + pos, length);
415: pos += length;
416: } finally {
417: Buffers.setPositionLimit(dst, oldPos, oldLim);
418: }
419:
420: return this;
421: }
422:
423: @Override
424: public Buffer put(final ByteBuffer src) {
425: final int length = src.remaining();
426:
427: src.get(heap, offset + pos, length);
428: pos += length;
429:
430: return this;
431: }
432:
433: @Override
434: public Buffer put(final ByteBuffer src, final int position, final int length) {
435: final int oldPos = src.position();
436: final int oldLim = src.limit();
437:
438: try {
439: Buffers.setPositionLimit(src, position, position + length);
440: src.get(heap, offset + pos, length);
441: pos += length;
442: } finally {
443: Buffers.setPositionLimit(src, oldPos, oldLim);
444: }
445:
446: return this;
447: }
448:
449: public static HeapBuffer wrap(byte[] heap) {
450: return wrap(heap, 0, heap.length);
451: }
452:
453: public static HeapBuffer wrap(byte[] heap, int off, int len) {
454: return new HeapBuffer(heap, off, len);
455: }
456:
457: @Override
458: public HeapBuffer put(byte[] src) {
459: return put(src, 0, src.length);
460: }
461:
462: @Override
463: public HeapBuffer put(byte[] src, int offset, int length) {
464:• if (remaining() < length) {
465: throw new BufferOverflowException();
466: }
467:
468: System.arraycopy(src, offset, heap, this.offset + pos, length);
469: pos += length;
470:
471: return this;
472: }
473:
474: @SuppressWarnings("deprecation")
475: @Override
476: public HeapBuffer put8BitString(final String s) {
477: final int len = s.length();
478:• if (remaining() < len) {
479: throw new BufferOverflowException();
480: }
481:
482: s.getBytes(0, len, heap, offset + pos);
483: pos += len;
484:
485: return this;
486: }
487:
488: @Override
489: public HeapBuffer compact() {
490: final int length = remaining();
491: System.arraycopy(heap, offset + pos, heap, offset, length);
492: pos = length;
493: lim = cap;
494: return this;
495: }
496:
497: @Override
498: public ByteOrder order() {
499: return order;
500: }
501:
502: @Override
503: public HeapBuffer order(ByteOrder bo) {
504: order = bo;
505:• bigEndian = order == ByteOrder.BIG_ENDIAN;
506: return this;
507: }
508:
509: @Override
510: public char getChar() {
511:• if (remaining() < 2) {
512: throw new BufferUnderflowException();
513: }
514: final char c = Bits.getChar(heap, offset + pos, bigEndian);
515: pos += 2;
516: return c;
517: }
518:
519: @Override
520: public char getChar(int index) {
521:• if (index < 0 || index >= lim - 1) {
522: throw new IndexOutOfBoundsException();
523: }
524: return Bits.getChar(heap, offset + index, bigEndian);
525: }
526:
527: @Override
528: public HeapBuffer putChar(char value) {
529:• if (remaining() < 2) {
530: throw new BufferUnderflowException();
531: }
532:
533: Bits.putChar(heap, offset + pos, value, bigEndian);
534: pos += 2;
535: return this;
536: }
537:
538: @Override
539: public HeapBuffer putChar(int index, char value) {
540:• if (index < 0 || index >= lim - 1) {
541: throw new IndexOutOfBoundsException();
542: }
543: Bits.putChar(heap, offset + index, value, bigEndian);
544: return this;
545: }
546:
547: @Override
548: public short getShort() {
549:• if (remaining() < 2) {
550: throw new BufferUnderflowException();
551: }
552: final short s = Bits.getShort(heap, offset + pos, bigEndian);
553: pos += 2;
554: return s;
555: }
556:
557: @Override
558: public short getShort(int index) {
559:• if (index < 0 || index >= lim - 1) {
560: throw new IndexOutOfBoundsException();
561: }
562: return Bits.getShort(heap, offset + index, bigEndian);
563: }
564:
565: @Override
566: public HeapBuffer putShort(short value) {
567:• if (remaining() < 2) {
568: throw new BufferUnderflowException();
569: }
570: Bits.putShort(heap, offset + pos, value, bigEndian);
571: pos += 2;
572: return this;
573: }
574:
575: @Override
576: public HeapBuffer putShort(int index, short value) {
577:• if (index < 0 || index >= lim - 1) {
578: throw new IndexOutOfBoundsException();
579: }
580: Bits.putShort(heap, offset + index, value, bigEndian);
581: return this;
582: }
583:
584: @Override
585: public int getInt() {
586:• if (remaining() < 4) {
587: throw new BufferUnderflowException();
588: }
589: final int i = Bits.getInt(heap, offset + pos, bigEndian);
590: pos += 4;
591: return i;
592: }
593:
594: @Override
595: public int getInt(int index) {
596:• if (index < 0 || index >= lim - 3) {
597: throw new IndexOutOfBoundsException();
598: }
599: return Bits.getInt(heap, offset + index, bigEndian);
600: }
601:
602: @Override
603: public HeapBuffer putInt(int value) {
604:• if (remaining() < 4) {
605: throw new BufferUnderflowException();
606: }
607: Bits.putInt(heap, offset + pos, value, bigEndian);
608: pos += 4;
609: return this;
610: }
611:
612: @Override
613: public HeapBuffer putInt(int index, int value) {
614:• if (index < 0 || index >= lim - 3) {
615: throw new IndexOutOfBoundsException();
616: }
617: Bits.putInt(heap, offset + index, value, bigEndian);
618: return this;
619: }
620:
621: @Override
622: public long getLong() {
623:• if (remaining() < 8) {
624: throw new BufferUnderflowException();
625: }
626: final long l = Bits.getLong(heap, offset + pos, bigEndian);
627: pos += 8;
628: return l;
629: }
630:
631: @Override
632: public long getLong(int index) {
633:• if (index < 0 || index >= lim - 7) {
634: throw new IndexOutOfBoundsException();
635: }
636: return Bits.getLong(heap, offset + index, bigEndian);
637: }
638:
639: @Override
640: public HeapBuffer putLong(long value) {
641:• if (remaining() < 8) {
642: throw new BufferUnderflowException();
643: }
644: Bits.putLong(heap, offset + pos, value, bigEndian);
645: pos += 8;
646: return this;
647: }
648:
649: @Override
650: public HeapBuffer putLong(int index, long value) {
651:• if (index < 0 || index >= lim - 7) {
652: throw new IndexOutOfBoundsException();
653: }
654: Bits.putLong(heap, offset + index, value, bigEndian);
655: return this;
656: }
657:
658: @Override
659: public float getFloat() {
660:• if (remaining() < 4) {
661: throw new BufferUnderflowException();
662: }
663: final float f = Bits.getFloat(heap, offset + pos, bigEndian);
664: pos += 4;
665: return f;
666: }
667:
668: @Override
669: public float getFloat(int index) {
670:• if (index < 0 || index >= lim - 3) {
671: throw new IndexOutOfBoundsException();
672: }
673: return Bits.getFloat(heap, offset + index, bigEndian);
674: }
675:
676: @Override
677: public HeapBuffer putFloat(float value) {
678:• if (remaining() < 4) {
679: throw new BufferUnderflowException();
680: }
681: Bits.putFloat(heap, offset + pos, value, bigEndian);
682: pos += 4;
683: return this;
684: }
685:
686: @Override
687: public HeapBuffer putFloat(int index, float value) {
688:• if (index < 0 || index >= lim - 3) {
689: throw new IndexOutOfBoundsException();
690: }
691: Bits.putFloat(heap, offset + index, value, bigEndian);
692: return this;
693: }
694:
695: @Override
696: public double getDouble() {
697:• if (remaining() < 8) {
698: throw new BufferUnderflowException();
699: }
700: final double d = Bits.getDouble(heap, offset + pos, bigEndian);
701: pos += 8;
702: return d;
703: }
704:
705: @Override
706: public double getDouble(int index) {
707:• if (index < 0 || index >= lim - 7) {
708: throw new IndexOutOfBoundsException();
709: }
710: return Bits.getDouble(heap, offset + index, bigEndian);
711: }
712:
713: @Override
714: public HeapBuffer putDouble(double value) {
715:• if (remaining() < 8) {
716: throw new BufferUnderflowException();
717: }
718: Bits.putDouble(heap, offset + pos, value, bigEndian);
719: pos += 8;
720: return this;
721: }
722:
723: @Override
724: public HeapBuffer putDouble(int index, double value) {
725:• if (index < 0 || index >= lim - 7) {
726: throw new IndexOutOfBoundsException();
727: }
728: Bits.putDouble(heap, offset + index, value, bigEndian);
729: return this;
730: }
731:
732: @Override
733: public int hashCode() {
734:• int result = allowBufferDispose ? 1 : 0;
735:• result = 31 * result + (disposeStackTrace != null ? disposeStackTrace.hashCode() : 0);
736:• result = 31 * result + (heap != null ? Arrays.hashCode(heap) : 0);
737: result = 31 * result + offset;
738: result = 31 * result + pos;
739: result = 31 * result + cap;
740: result = 31 * result + lim;
741: result = 31 * result + mark;
742:• result = 31 * result + (readOnly ? 1 : 0);
743:• result = 31 * result + (order != null ? order.hashCode() : 0);
744:• result = 31 * result + (bigEndian ? 1 : 0);
745: return result;
746: }
747:
748: @Override
749: public boolean equals(Object obj) {
750:• if (obj instanceof Buffer) {
751: Buffer that = (Buffer) obj;
752:• if (this.remaining() != that.remaining()) {
753: return false;
754: }
755: int p = this.position();
756:• for (int i = this.limit() - 1, j = that.limit() - 1; i >= p; i--, j--) {
757: byte v1 = this.get(i);
758: byte v2 = that.get(j);
759:• if (v1 != v2) {
760: return false;
761: }
762: }
763: return true;
764: }
765:
766: return false;
767: }
768:
769: @Override
770: public int compareTo(Buffer o) {
771: // taken from ByteBuffer#compareTo(...)
772: int n = position() + Math.min(remaining(), o.remaining());
773:• for (int i = this.position(), j = o.position(); i < n; i++, j++) {
774: byte v1 = this.get(i);
775: byte v2 = o.get(j);
776:• if (v1 == v2) {
777: continue;
778: }
779:• if (v1 < v2) {
780: return -1;
781: }
782: return +1;
783: }
784:
785: return remaining() - o.remaining();
786: }
787:
788: protected void checkDispose() {
789:• if (heap == null) {
790: throw new IllegalStateException("HeapBuffer has already been disposed", disposeStackTrace);
791: }
792: }
793:
794: @Override
795: public String toString() {
796: final StringBuilder sb = new StringBuilder("HeapBuffer (" + System.identityHashCode(this) + ") ");
797: sb.append("[pos=");
798: sb.append(pos);
799: sb.append(" lim=");
800: sb.append(lim);
801: sb.append(" cap=");
802: sb.append(cap);
803: sb.append(']');
804: return sb.toString();
805: }
806:
807: @Override
808: public String toStringContent() {
809: return toStringContent(Charset.defaultCharset(), pos, lim);
810: }
811:
812: @Override
813: public String toStringContent(final Charset charset) {
814: return toStringContent(charset, pos, lim);
815: }
816:
817: @Override
818: public String toStringContent(Charset charset, final int position, final int limit) {
819: checkDispose();
820:• if (charset == null) {
821: charset = Charset.defaultCharset();
822: }
823:
824:• final boolean isRestoreByteBuffer = byteBuffer != null;
825: int oldPosition = 0;
826: int oldLimit = 0;
827:
828:• if (isRestoreByteBuffer) {
829: // ByteBuffer can be used by outer code - so save its state
830: oldPosition = byteBuffer.position();
831: oldLimit = byteBuffer.limit();
832: }
833:
834: final ByteBuffer bb = toByteBuffer0(position, limit, false);
835:
836: try {
837: return charset.decode(bb).toString();
838: } finally {
839:• if (isRestoreByteBuffer) {
840: Buffers.setPositionLimit(byteBuffer, oldPosition, oldLimit);
841: }
842: }
843: }
844:
845: /**
846: * {@inheritDoc}
847: */
848: @Override
849: public void dumpHex(java.lang.Appendable appendable) {
850: Buffers.dumpBuffer(appendable, this);
851: }
852:
853: /**
854: * {@inheritDoc}
855: */
856: @Override
857: public ByteBuffer toByteBuffer() {
858: return toByteBuffer(pos, lim);
859: }
860:
861: /**
862: * {@inheritDoc}
863: */
864: @Override
865: public ByteBuffer toByteBuffer(final int position, final int limit) {
866: return toByteBuffer0(position, limit, false);
867: }
868:
869: /**
870: * {@inheritDoc}
871: */
872: @Override
873: public final ByteBufferArray toByteBufferArray() {
874: final ByteBufferArray array = ByteBufferArray.create();
875: array.add(toByteBuffer());
876:
877: return array;
878: }
879:
880: /**
881: * {@inheritDoc}
882: */
883: @Override
884: public final ByteBufferArray toByteBufferArray(final int position, final int limit) {
885: return toByteBufferArray(ByteBufferArray.create(), position, limit);
886: }
887:
888: /**
889: * {@inheritDoc}
890: */
891: @Override
892: public final ByteBufferArray toByteBufferArray(final ByteBufferArray array) {
893: array.add(toByteBuffer());
894: return array;
895: }
896:
897: /**
898: * {@inheritDoc}
899: */
900: @Override
901: public final ByteBufferArray toByteBufferArray(final ByteBufferArray array, final int position, final int limit) {
902:
903: array.add(toByteBuffer(position, limit));
904:
905: return array;
906: }
907:
908: /**
909: * {@inheritDoc}
910: */
911: @Override
912: public final BufferArray toBufferArray() {
913: final BufferArray array = BufferArray.create();
914: array.add(this);
915:
916: return array;
917: }
918:
919: /**
920: * {@inheritDoc}
921: */
922: @Override
923: public final BufferArray toBufferArray(final int position, final int limit) {
924: return toBufferArray(BufferArray.create(), position, limit);
925: }
926:
927: /**
928: * {@inheritDoc}
929: */
930: @Override
931: public final BufferArray toBufferArray(final BufferArray array) {
932: array.add(this);
933: return array;
934: }
935:
936: /**
937: * {@inheritDoc}
938: */
939: @Override
940: public final BufferArray toBufferArray(final BufferArray array, final int position, final int limit) {
941:
942: final int oldPos = pos;
943: final int oldLim = lim;
944:
945: pos = position;
946: lim = limit;
947:
948: array.add(this, oldPos, oldLim);
949: return array;
950: }
951:
952: @Override
953: public boolean release() {
954: return tryDispose();
955: }
956:
957: /**
958: * {@inheritDoc}
959: */
960: @Override
961: public boolean isExternal() {
962: return false;
963: }
964:
965: @Override
966: public boolean hasArray() {
967: return true;
968: }
969:
970: @Override
971: public int arrayOffset() {
972: return offset;
973: }
974:
975: @Override
976: public byte[] array() {
977: return heap;
978: }
979:
980: // ------------------------------------------------------- Protected Methods
981:
982: protected void onShareHeap() {
983: }
984:
985: /**
986: * Create a new {@link HeapBuffer} based on the current heap.
987: *
988: * @param offs relative offset, the absolute value will calculated as (this.offset + offs)
989: * @param capacity
990: * @return
991: */
992: protected HeapBuffer createHeapBuffer(final int offs, final int capacity) {
993: onShareHeap();
994:
995: return new HeapBuffer(heap, offs + offset, capacity);
996: }
997:
998: protected ByteBuffer toByteBuffer0(final int pos, final int lim, final boolean slice) {
999:• if (byteBuffer == null) {
1000: byteBuffer = ByteBuffer.wrap(heap);
1001: }
1002:
1003: Buffers.setPositionLimit(byteBuffer, offset + pos, offset + lim);
1004:
1005:• return slice ? byteBuffer.slice() : byteBuffer;
1006:
1007: }
1008:
1009: // ---------------------------------------------------------- Nested Classes
1010:
1011: private static class DebugLogic {
1012: static void doDebug(HeapBuffer heapBuffer) {
1013: heapBuffer.clear();
1014: while (heapBuffer.hasRemaining()) {
1015: heapBuffer.put((byte) 0xFF);
1016: }
1017: heapBuffer.flip();
1018: heapBuffer.disposeStackTrace = new Exception("HeapBuffer was disposed from: ");
1019: }
1020: }
1021:
1022: }