1 4 package com.tc.bytes; 5 6 import com.tc.exception.ImplementMe; 7 8 11 class TCByteBufferJDK13 extends AbstractTCByteBuffer { 12 private final int _offset; 13 private final int _capacity; 14 private int _limit; 15 private int _position; 16 private final byte _data[]; 17 private final boolean _readOnly; 18 19 TCByteBufferJDK13(final int capacity, final boolean direct) { 20 this(capacity); 22 } 23 24 private TCByteBufferJDK13(final int capacity) { 25 this(new byte[capacity]); 26 } 27 28 TCByteBufferJDK13(byte[] data) { 29 _data = data; 30 _capacity = data.length; 31 _limit = _capacity; 32 _position = 0; 33 _offset = 0; 34 _readOnly = false; 35 } 36 37 private TCByteBufferJDK13(final byte data[], final int limit, final int position, final int offset, final boolean readOnly) { 38 _offset = offset; 39 _capacity = data.length - _offset; 40 _data = data; 41 _limit = limit; 42 _position = position; 43 _readOnly = readOnly; 44 } 45 46 public int capacity() { 47 return _capacity; 48 } 49 50 public TCByteBuffer clear() { 51 _limit = _data.length; 52 _position = _offset; 53 return this; 54 } 55 56 public int position() { 57 return _position - _offset; 58 } 59 60 public TCByteBuffer flip() { 61 _limit = _position; 62 _position = _offset; 63 return this; 64 } 65 66 public boolean hasRemaining() { 67 return _position < _limit; 68 } 69 70 public int limit() { 71 return _limit - _offset; 72 } 73 74 public TCByteBuffer limit(final int newLimit) { 75 if ((newLimit > _capacity) || (newLimit < 0)) { throw new IllegalArgumentException (); } 76 77 _limit = newLimit + _offset; 78 if (_position > _limit) { 79 _position = _limit; 80 } 81 82 return this; 83 } 84 85 public TCByteBuffer position(final int newPosition) { 86 if ((newPosition > _limit) || (newPosition < 0)) throw new IllegalArgumentException (); 87 _position = newPosition + _offset; 88 89 return this; 90 } 91 92 public int remaining() { 93 return _limit - _position; 94 } 95 96 public TCByteBuffer rewind() { 97 _position = _offset; 98 return this; 99 } 100 101 public boolean isNioBuffer() { 102 return false; 103 } 104 105 public Object getNioBuffer() { 106 throw new UnsupportedOperationException ("This buffer does not provide a backing java.nio.ByteBuffer"); 107 } 108 109 public boolean isDirect() { 110 return false; 111 } 112 113 public byte[] array() { 114 return _data; 115 } 116 117 public byte get() { 118 if (_position < _limit) { 119 byte rv = _data[_position]; 120 _position++; 121 return rv; 122 } 123 throw new TCBufferUnderflowException(); 124 } 125 126 public TCByteBuffer get(final byte[] dst) { 127 return get(dst, 0, dst.length); 128 } 129 130 public TCByteBuffer get(final byte[] dst, final int offset, final int length) { 131 if (length > remaining()) { throw new TCBufferUnderflowException(); } 132 133 System.arraycopy(_data, _position, dst, offset, length); 134 _position += length; 135 return this; 136 } 137 138 public byte get(final int index) { 139 if ((index < 0) || (index > limit())) { throw new IndexOutOfBoundsException (); } 140 141 return _data[index]; 142 } 143 144 public int getInt() { 145 throw new ImplementMe(); 146 } 147 148 public long getLong() { 149 throw new ImplementMe(); 150 } 151 152 public TCByteBuffer put(final byte b) { 153 if (_readOnly) { throw new TCReadOnlyBufferException(); } 154 155 if (_position < _limit) { 156 _data[_position] = b; 157 _position++; 158 return this; 159 } 160 161 throw new TCBufferOverflowException(); 162 } 163 164 public TCByteBuffer put(final byte[] src) { 165 if (_readOnly) { throw new TCReadOnlyBufferException(); } 166 167 return put(src, 0, src.length); 168 } 169 170 public TCByteBuffer put(final byte[] src, final int offset, final int length) { 171 if (_readOnly) { throw new TCReadOnlyBufferException(); } 172 173 if (length > remaining()) { throw new TCBufferOverflowException(); } 174 175 System.arraycopy(src, offset, _data, _position, length); 176 _position += length; 177 178 return this; 179 } 180 181 public TCByteBuffer put(final int index, final byte b) { 182 if (_readOnly) { throw new TCReadOnlyBufferException(); } 183 184 if ((index < 0) || (index > limit())) { throw new IndexOutOfBoundsException (); } 185 186 _data[index] = b; 187 188 return this; 189 } 190 191 public TCByteBuffer duplicate() { 192 return new TCByteBufferJDK13(_data, _limit, _position, _offset, _readOnly); 193 } 194 195 public TCByteBuffer put(TCByteBuffer src) { 196 if (src.remaining() > remaining()) { throw new TCBufferOverflowException(); } 197 198 if (src == this) { throw new IllegalArgumentException ("Source buffer can not be this buffer"); } 199 200 if (_readOnly) { throw new TCReadOnlyBufferException(); } 201 202 int length = src.remaining(); 203 204 System.arraycopy(src.array(), src.position() + src.arrayOffset(), _data, _position, length); 205 src.position(src.position() + length); 206 207 _position += length; 208 209 return this; 210 } 211 212 public TCByteBuffer putInt(int i) { 213 214 throw new ImplementMe(); 215 } 216 217 public TCByteBuffer putLong(long l) { 218 219 throw new ImplementMe(); 220 } 221 222 public TCByteBuffer slice() { 223 final int newLimit = _position + remaining(); 224 225 return new TCByteBufferJDK13(_data, newLimit, _position, _position, _readOnly); 226 } 227 228 public int arrayOffset() { 229 return _offset; 230 } 231 232 public TCByteBuffer asReadOnlyBuffer() { 233 return new TCByteBufferJDK13(_data, _limit, _position, _offset, true); 234 } 235 236 public boolean isReadOnly() { 237 return _readOnly; 238 } 239 240 245 public TCByteBuffer putChar(char c) { 246 247 throw new ImplementMe(); 248 } 249 250 255 public TCByteBuffer putFloat(float f) { 256 257 throw new ImplementMe(); 258 } 259 260 265 public TCByteBuffer putDouble(double d) { 266 267 throw new ImplementMe(); 268 } 269 270 275 public TCByteBuffer putChar(int index, char c) { 276 277 throw new ImplementMe(); 278 } 279 280 285 public TCByteBuffer putInt(int index, int i) { 286 287 throw new ImplementMe(); 288 } 289 290 295 public TCByteBuffer putLong(int index, long l) { 296 297 throw new ImplementMe(); 298 } 299 300 305 public TCByteBuffer putFloat(int index, float f) { 306 307 throw new ImplementMe(); 308 } 309 310 315 public TCByteBuffer putDouble(int index, double d) { 316 317 throw new ImplementMe(); 318 } 319 320 325 public boolean getBoolean() { 326 327 throw new ImplementMe(); 328 } 329 330 335 public boolean getBoolean(int index) { 336 337 throw new ImplementMe(); 338 } 339 340 345 public char getChar() { 346 347 throw new ImplementMe(); 348 } 349 350 355 public char getChar(int index) { 356 357 throw new ImplementMe(); 358 } 359 360 365 public double getDouble() { 366 367 throw new ImplementMe(); 368 } 369 370 375 public double getDouble(int index) { 376 377 throw new ImplementMe(); 378 } 379 380 385 public float getFloat() { 386 387 throw new ImplementMe(); 388 } 389 390 395 public float getFloat(int index) { 396 397 throw new ImplementMe(); 398 } 399 400 405 public int getInt(int index) { 406 407 throw new ImplementMe(); 408 } 409 410 415 public long getLong(int index) { 416 417 throw new ImplementMe(); 418 } 419 420 425 public short getShort() { 426 427 throw new ImplementMe(); 428 } 429 430 435 public short getShort(int index) { 436 437 throw new ImplementMe(); 438 } 439 440 445 public TCByteBuffer putBoolean(boolean b) { 446 447 throw new ImplementMe(); 448 } 449 450 455 public TCByteBuffer putBoolean(int index, boolean b) { 456 457 throw new ImplementMe(); 458 } 459 460 465 public TCByteBuffer putShort(short s) { 466 467 throw new ImplementMe(); 468 } 469 470 475 public TCByteBuffer putShort(int index, short s) { 476 477 throw new ImplementMe(); 478 } 479 480 public boolean hasArray() { 481 return true; 482 } 483 484 public void recycle() { 485 } 487 488 } | Popular Tags |