1 4 package com.tc.bytes; 5 6 import java.nio.ByteBuffer ; 7 8 11 12 class TCByteBufferJDK14 extends AbstractTCByteBuffer { 15 16 private final ByteBuffer buffer; 17 private final TCByteBufferJDK14 root; 18 19 TCByteBufferJDK14(int capacity, boolean direct) { 20 if (direct) { 21 buffer = ByteBuffer.allocateDirect(capacity); 22 } else { 23 buffer = ByteBuffer.allocate(capacity); 24 } 25 root = this; 26 } 27 28 private TCByteBufferJDK14(ByteBuffer buf) { 29 buffer = buf; 30 this.root = null; 31 } 32 33 private TCByteBufferJDK14(ByteBuffer buf, TCByteBufferJDK14 root) { 34 buffer = buf; 35 this.root = root; 36 } 37 38 static TCByteBufferJDK14 wrap(byte[] data) { 39 return new TCByteBufferJDK14(ByteBuffer.wrap(data)); 40 } 41 42 protected ByteBuffer getBuffer() { 43 return buffer; 44 } 45 46 public TCByteBuffer clear() { 47 buffer.clear(); 48 return this; 49 } 50 51 public int capacity() { 52 return buffer.capacity(); 53 } 54 55 public int position() { 56 return buffer.position(); 57 } 58 59 public TCByteBuffer flip() { 60 buffer.flip(); 61 return this; 62 } 63 64 public boolean hasRemaining() { 65 return buffer.hasRemaining(); 66 } 67 68 public int limit() { 69 return buffer.limit(); 70 } 71 72 public TCByteBuffer limit(int newLimit) { 73 buffer.limit(newLimit); 74 return this; 75 } 76 77 public TCByteBuffer position(int newPosition) { 78 buffer.position(newPosition); 79 return this; 80 } 81 82 public int remaining() { 83 return buffer.remaining(); 84 } 85 86 public com.tc.bytes.TCByteBuffer rewind() { 87 buffer.rewind(); 88 return this; 89 } 90 91 public boolean isNioBuffer() { 92 return true; 93 } 94 95 public Object getNioBuffer() { 96 return buffer; 97 } 98 99 public boolean isDirect() { 100 return buffer.isDirect(); 101 } 102 103 public byte[] array() { 104 return buffer.array(); 105 } 106 107 public byte get() { 108 return buffer.get(); 109 } 110 111 public boolean getBoolean() { 112 return buffer.get() > 0; 114 } 115 116 public boolean getBoolean(int index) { 117 return buffer.get(index) > 0; 118 } 119 120 public char getChar() { 121 return buffer.getChar(); 122 } 123 124 public char getChar(int index) { 125 return buffer.getChar(index); 126 } 127 128 public double getDouble() { 129 return buffer.getDouble(); 130 } 131 132 public double getDouble(int index) { 133 return buffer.getDouble(index); 134 } 135 136 public float getFloat() { 137 return buffer.getFloat(); 138 } 139 140 public float getFloat(int index) { 141 return buffer.getFloat(index); 142 } 143 144 public int getInt() { 145 return buffer.getInt(); 146 } 147 148 public int getInt(int index) { 149 return buffer.getInt(index); 150 } 151 152 public long getLong() { 153 return buffer.getLong(); 154 } 155 156 public long getLong(int index) { 157 return buffer.getLong(index); 158 } 159 160 public short getShort() { 161 return buffer.getShort(); 162 } 163 164 public short getShort(int index) { 165 return buffer.getShort(index); 166 } 167 168 public TCByteBuffer get(byte[] dst) { 169 buffer.get(dst); 170 return this; 171 } 172 173 public TCByteBuffer get(byte[] dst, int offset, int length) { 174 buffer.get(dst, offset, length); 175 return this; 176 } 177 178 public byte get(int index) { 179 return buffer.get(index); 180 } 181 182 public TCByteBuffer put(byte b) { 183 buffer.put(b); 184 return this; 185 } 186 187 public TCByteBuffer put(byte[] src) { 188 buffer.put(src); 189 return this; 190 } 191 192 public TCByteBuffer put(byte[] src, int offset, int length) { 193 buffer.put(src, offset, length); 194 return this; 195 } 196 197 public TCByteBuffer put(int index, byte b) { 198 buffer.put(index, b); 199 return this; 200 } 201 202 public TCByteBuffer putBoolean(boolean b) { 203 buffer.put((b) ? (byte) 1 : (byte) 0); 205 return this; 206 } 207 208 public TCByteBuffer putBoolean(int index, boolean b) { 209 buffer.put(index, (b) ? (byte) 1 : (byte) 0); 210 return this; 211 } 212 213 public TCByteBuffer putChar(char c) { 214 buffer.putChar(c); 215 return this; 216 } 217 218 public TCByteBuffer putChar(int index, char c) { 219 buffer.putChar(index, c); 220 return this; 221 } 222 223 public TCByteBuffer putDouble(double d) { 224 buffer.putDouble(d); 225 return this; 226 } 227 228 public TCByteBuffer putDouble(int index, double d) { 229 buffer.putDouble(index, d); 230 return this; 231 } 232 233 public TCByteBuffer putFloat(float f) { 234 buffer.putFloat(f); 235 return this; 236 } 237 238 public TCByteBuffer putFloat(int index, float f) { 239 buffer.putFloat(index, f); 240 return this; 241 } 242 243 public TCByteBuffer putInt(int i) { 244 buffer.putInt(i); 245 return this; 246 } 247 248 public TCByteBuffer putInt(int index, int i) { 249 buffer.putInt(index, i); 250 return this; 251 } 252 253 public TCByteBuffer putLong(long l) { 254 buffer.putLong(l); 255 return this; 256 } 257 258 public TCByteBuffer putLong(int index, long l) { 259 buffer.putLong(index, l); 260 return this; 261 } 262 263 public TCByteBuffer putShort(short s) { 264 buffer.putShort(s); 265 return this; 266 } 267 268 public TCByteBuffer putShort(int index, short s) { 269 buffer.putShort(index, s); 270 return this; 271 } 272 273 public TCByteBuffer duplicate() { 274 return new TCByteBufferJDK14(buffer.duplicate(), root); 275 } 276 277 public TCByteBuffer put(TCByteBuffer src) { 278 if (!src.isNioBuffer()) { throw new IllegalArgumentException ("src buffer is not backed by a java.nio.ByteBuffer"); } 279 280 buffer.put((ByteBuffer ) src.getNioBuffer()); 281 return this; 282 } 283 284 public TCByteBuffer slice() { 285 return new TCByteBufferJDK14(buffer.slice(), root); 286 } 287 288 public int arrayOffset() { 289 return buffer.arrayOffset(); 290 } 291 292 public TCByteBuffer asReadOnlyBuffer() { 293 return new TCByteBufferJDK14(buffer.asReadOnlyBuffer(), root); 294 } 295 296 public boolean isReadOnly() { 297 return buffer.isReadOnly(); 298 } 299 300 public String toString() { 301 return (buffer == null) ? "TCByteBufferJDK14(null buffer)" : "TCByteBufferJDK14@" + System.identityHashCode(this) 302 + "(" + buffer.toString() + ")"; 303 } 304 305 public boolean hasArray() { 306 return buffer.hasArray(); 307 } 308 309 public void recycle() { 311 if (root != null) { 312 TCByteBufferFactory.returnBuffer(root.reInit()); 313 } 314 } 315 316 private TCByteBufferJDK14 reInit() { 317 clear(); 318 return this; 319 } 320 321 322 323 } | Popular Tags |