|
| 1 | +package de.javasocketapi.core; |
| 2 | + |
| 3 | +import java.nio.ByteBuffer; |
| 4 | +import java.nio.ByteOrder; |
| 5 | + |
| 6 | +public class DynamicByteBuffer implements Comparable<ByteBuffer> { |
| 7 | + private ByteBuffer byteBuffer; |
| 8 | + private float expandFactor; |
| 9 | + private int writeIndex; |
| 10 | + private int readIndex; |
| 11 | + |
| 12 | + DynamicByteBuffer(int initialCapacity, float expandFactor) { |
| 13 | + if (expandFactor < 2) { |
| 14 | + throw new IllegalArgumentException( |
| 15 | + "The expand factor must be greater or equal to 2!"); |
| 16 | + } |
| 17 | + this.byteBuffer = ByteBuffer.allocate(initialCapacity); |
| 18 | + this.expandFactor = expandFactor; |
| 19 | + this.writeIndex = 0; |
| 20 | + this.readIndex = 0; |
| 21 | + } |
| 22 | + |
| 23 | + DynamicByteBuffer(int initialCapacity) { |
| 24 | + this(initialCapacity, 2); |
| 25 | + } |
| 26 | + |
| 27 | + DynamicByteBuffer() { |
| 28 | + this(16); |
| 29 | + } |
| 30 | + |
| 31 | + DynamicByteBuffer(int initialCapacity, float expandFactor, byte... bytes) { |
| 32 | + this(initialCapacity, expandFactor); |
| 33 | + put(bytes); |
| 34 | + } |
| 35 | + |
| 36 | + DynamicByteBuffer(int initialCapacity, byte... bytes) { |
| 37 | + this(initialCapacity, 2, bytes); |
| 38 | + } |
| 39 | + |
| 40 | + DynamicByteBuffer(byte... bytes) { |
| 41 | + this(16, 2, bytes); |
| 42 | + } |
| 43 | + |
| 44 | + public byte[] array() { |
| 45 | + return byteBuffer.array(); |
| 46 | + } |
| 47 | + |
| 48 | + public int compareTo(ByteBuffer that) { |
| 49 | + return byteBuffer.compareTo(that); |
| 50 | + } |
| 51 | + |
| 52 | + public int remaining() { |
| 53 | + return byteBuffer.remaining(); |
| 54 | + } |
| 55 | + |
| 56 | + public boolean equals(Object ob) { |
| 57 | + return byteBuffer.equals(ob); |
| 58 | + } |
| 59 | + |
| 60 | + public byte get() { |
| 61 | + int index = this.readIndex; |
| 62 | + this.readIndex += 1; |
| 63 | + return byteBuffer.get(index); |
| 64 | + } |
| 65 | + |
| 66 | + public char getChar() { |
| 67 | + int index = this.readIndex; |
| 68 | + this.readIndex += 2; |
| 69 | + return byteBuffer.getChar(index); |
| 70 | + } |
| 71 | + |
| 72 | + public double getDouble() { |
| 73 | + int index = this.readIndex; |
| 74 | + this.readIndex += 8; |
| 75 | + return byteBuffer.getDouble(index); |
| 76 | + } |
| 77 | + |
| 78 | + public float getFloat() { |
| 79 | + int index = this.readIndex; |
| 80 | + this.readIndex += 4; |
| 81 | + return byteBuffer.getFloat(index); |
| 82 | + } |
| 83 | + |
| 84 | + public int getInt() { |
| 85 | + int index = this.readIndex; |
| 86 | + this.readIndex += 4; |
| 87 | + return byteBuffer.getInt(index); |
| 88 | + } |
| 89 | + |
| 90 | + public long getLong() { |
| 91 | + int index = this.readIndex; |
| 92 | + this.readIndex += 8; |
| 93 | + return byteBuffer.getLong(index); |
| 94 | + } |
| 95 | + |
| 96 | + public short getShort() { |
| 97 | + int index = this.readIndex; |
| 98 | + this.readIndex += 2; |
| 99 | + return byteBuffer.getShort(index); |
| 100 | + } |
| 101 | + |
| 102 | + public ByteOrder order() { |
| 103 | + return byteBuffer.order(); |
| 104 | + } |
| 105 | + |
| 106 | + public ByteBuffer order(ByteOrder bo) { |
| 107 | + return byteBuffer.order(bo); |
| 108 | + } |
| 109 | + |
| 110 | + public ByteBuffer put(byte b) { |
| 111 | + ensureSpace(1 + 1); |
| 112 | + int index = this.writeIndex; |
| 113 | + this.writeIndex += 1; |
| 114 | + return byteBuffer.put(index, b); |
| 115 | + } |
| 116 | + |
| 117 | + public ByteBuffer put(byte[] src) { |
| 118 | + ensureSpace(src.length); |
| 119 | + return byteBuffer.put(src); |
| 120 | + } |
| 121 | + |
| 122 | + public ByteBuffer put(ByteBuffer src) { |
| 123 | + ensureSpace(src.remaining()); |
| 124 | + return byteBuffer.put(src); |
| 125 | + } |
| 126 | + |
| 127 | + public ByteBuffer putChar(char value) { |
| 128 | + ensureSpace(2 + 1); |
| 129 | + int index = this.writeIndex; |
| 130 | + this.writeIndex += 2; |
| 131 | + return byteBuffer.putChar(index, value); |
| 132 | + } |
| 133 | + |
| 134 | + public ByteBuffer putDouble(double value) { |
| 135 | + ensureSpace(8 + 1); |
| 136 | + int index = this.writeIndex; |
| 137 | + this.writeIndex += 8; |
| 138 | + return byteBuffer.putDouble(index, value); |
| 139 | + } |
| 140 | + |
| 141 | + public ByteBuffer putFloat(float value) { |
| 142 | + ensureSpace(4 + 1); |
| 143 | + int index = this.writeIndex; |
| 144 | + this.writeIndex += 4; |
| 145 | + return byteBuffer.putFloat(index, value); |
| 146 | + } |
| 147 | + |
| 148 | + public ByteBuffer putInt(int value) { |
| 149 | + ensureSpace(4 + 1); |
| 150 | + int index = this.writeIndex; |
| 151 | + this.writeIndex += 4; |
| 152 | + return byteBuffer.putInt(index, value); |
| 153 | + } |
| 154 | + |
| 155 | + public ByteBuffer putLong(long value) { |
| 156 | + ensureSpace(8 + 1); |
| 157 | + int index = this.writeIndex; |
| 158 | + this.writeIndex += 8; |
| 159 | + return byteBuffer.putLong(index, value); |
| 160 | + } |
| 161 | + |
| 162 | + public ByteBuffer putShort(short value) { |
| 163 | + ensureSpace(2 + 1); |
| 164 | + int index = this.writeIndex; |
| 165 | + this.writeIndex += 2; |
| 166 | + return byteBuffer.putShort(index, value); |
| 167 | + } |
| 168 | + |
| 169 | + @Override |
| 170 | + public int hashCode() { |
| 171 | + return byteBuffer.hashCode(); |
| 172 | + } |
| 173 | + |
| 174 | + @Override |
| 175 | + public String toString() { |
| 176 | + return byteBuffer.toString(); |
| 177 | + } |
| 178 | + |
| 179 | + public ByteBuffer getByteBuffer() { |
| 180 | + return byteBuffer; |
| 181 | + } |
| 182 | + |
| 183 | + public float getExpandFactor() { |
| 184 | + return expandFactor; |
| 185 | + } |
| 186 | + |
| 187 | + public int getWriteIndex() { |
| 188 | + return writeIndex; |
| 189 | + } |
| 190 | + |
| 191 | + public int getReadIndex() { |
| 192 | + return readIndex; |
| 193 | + } |
| 194 | + |
| 195 | + private void ensureSpace(int needed) { |
| 196 | + if (remaining() >= needed) { |
| 197 | + return; |
| 198 | + } |
| 199 | + int newCapacity = (int) (byteBuffer.capacity() * expandFactor); |
| 200 | + while (newCapacity < (byteBuffer.capacity() + needed)) { |
| 201 | + newCapacity *= expandFactor; |
| 202 | + } |
| 203 | + ByteBuffer expanded = ByteBuffer.allocate(newCapacity); |
| 204 | + expanded.order(byteBuffer.order()); |
| 205 | + expanded.put(byteBuffer.array()); |
| 206 | + byteBuffer = expanded; |
| 207 | + } |
| 208 | +} |
0 commit comments