1 7 8 10 package java.nio; 11 12 13 23 24 class HeapByteBuffer 25 extends ByteBuffer 26 { 27 28 36 37 HeapByteBuffer(int cap, int lim) { 39 super(-1, 0, lim, cap, new byte[cap], 0); 40 44 45 46 47 48 } 49 50 HeapByteBuffer(byte[] buf, int off, int len) { 52 super(-1, off, off + len, buf.length, buf, 0); 53 57 58 59 60 61 } 62 63 protected HeapByteBuffer(byte[] buf, 64 int mark, int pos, int lim, int cap, 65 int off) 66 { 67 68 super(mark, pos, lim, cap, buf, off); 69 73 74 75 76 77 } 78 79 public ByteBuffer slice() { 80 return new HeapByteBuffer (hb, 81 -1, 82 0, 83 this.remaining(), 84 this.remaining(), 85 this.position() + offset); 86 } 87 88 public ByteBuffer duplicate() { 89 return new HeapByteBuffer (hb, 90 this.markValue(), 91 this.position(), 92 this.limit(), 93 this.capacity(), 94 offset); 95 } 96 97 public ByteBuffer asReadOnlyBuffer() { 98 99 return new HeapByteBufferR (hb, 100 this.markValue(), 101 this.position(), 102 this.limit(), 103 this.capacity(), 104 offset); 105 106 107 108 } 109 110 111 112 protected int ix(int i) { 113 return i + offset; 114 } 115 116 public byte get() { 117 return hb[ix(nextGetIndex())]; 118 } 119 120 public byte get(int i) { 121 return hb[ix(checkIndex(i))]; 122 } 123 124 public ByteBuffer get(byte[] dst, int offset, int length) { 125 checkBounds(offset, length, dst.length); 126 if (length > remaining()) 127 throw new BufferUnderflowException (); 128 System.arraycopy(hb, ix(position()), dst, offset, length); 129 position(position() + length); 130 return this; 131 } 132 133 public boolean isDirect() { 134 return false; 135 } 136 137 138 139 public boolean isReadOnly() { 140 return false; 141 } 142 143 public ByteBuffer put(byte x) { 144 145 hb[ix(nextPutIndex())] = x; 146 return this; 147 148 149 150 } 151 152 public ByteBuffer put(int i, byte x) { 153 154 hb[ix(checkIndex(i))] = x; 155 return this; 156 157 158 159 } 160 161 public ByteBuffer put(byte[] src, int offset, int length) { 162 163 checkBounds(offset, length, src.length); 164 if (length > remaining()) 165 throw new BufferOverflowException (); 166 System.arraycopy(src, offset, hb, ix(position()), length); 167 position(position() + length); 168 return this; 169 170 171 172 } 173 174 public ByteBuffer put(ByteBuffer src) { 175 176 if (src instanceof HeapByteBuffer ) { 177 if (src == this) 178 throw new IllegalArgumentException (); 179 HeapByteBuffer sb = (HeapByteBuffer )src; 180 int n = sb.remaining(); 181 if (n > remaining()) 182 throw new BufferOverflowException (); 183 System.arraycopy(sb.hb, sb.ix(sb.position()), 184 hb, ix(position()), n); 185 sb.position(sb.position() + n); 186 position(position() + n); 187 } else if (src.isDirect()) { 188 int n = src.remaining(); 189 if (n > remaining()) 190 throw new BufferOverflowException (); 191 src.get(hb, ix(position()), n); 192 position(position() + n); 193 } else { 194 super.put(src); 195 } 196 return this; 197 198 199 200 } 201 202 public ByteBuffer compact() { 203 204 System.arraycopy(hb, ix(position()), hb, ix(0), remaining()); 205 position(remaining()); 206 limit(capacity()); 207 return this; 208 209 210 211 } 212 213 214 215 216 217 byte _get(int i) { return hb[i]; 219 } 220 221 void _put(int i, byte b) { 223 hb[i] = b; 224 225 226 227 } 228 229 231 232 233 public char getChar() { 234 return Bits.getChar(this, ix(nextGetIndex(2)), bigEndian); 235 } 236 237 public char getChar(int i) { 238 return Bits.getChar(this, ix(checkIndex(i, 2)), bigEndian); 239 } 240 241 242 243 public ByteBuffer putChar(char x) { 244 245 Bits.putChar(this, ix(nextPutIndex(2)), x, bigEndian); 246 return this; 247 248 249 250 } 251 252 public ByteBuffer putChar(int i, char x) { 253 254 Bits.putChar(this, ix(checkIndex(i, 2)), x, bigEndian); 255 return this; 256 257 258 259 } 260 261 public CharBuffer asCharBuffer() { 262 int size = this.remaining() >> 1; 263 int off = offset + position(); 264 return (bigEndian 265 ? (CharBuffer )(new ByteBufferAsCharBufferB (this, 266 -1, 267 0, 268 size, 269 size, 270 off)) 271 : (CharBuffer )(new ByteBufferAsCharBufferL (this, 272 -1, 273 0, 274 size, 275 size, 276 off))); 277 } 278 279 280 282 283 284 public short getShort() { 285 return Bits.getShort(this, ix(nextGetIndex(2)), bigEndian); 286 } 287 288 public short getShort(int i) { 289 return Bits.getShort(this, ix(checkIndex(i, 2)), bigEndian); 290 } 291 292 293 294 public ByteBuffer putShort(short x) { 295 296 Bits.putShort(this, ix(nextPutIndex(2)), x, bigEndian); 297 return this; 298 299 300 301 } 302 303 public ByteBuffer putShort(int i, short x) { 304 305 Bits.putShort(this, ix(checkIndex(i, 2)), x, bigEndian); 306 return this; 307 308 309 310 } 311 312 public ShortBuffer asShortBuffer() { 313 int size = this.remaining() >> 1; 314 int off = offset + position(); 315 return (bigEndian 316 ? (ShortBuffer )(new ByteBufferAsShortBufferB (this, 317 -1, 318 0, 319 size, 320 size, 321 off)) 322 : (ShortBuffer )(new ByteBufferAsShortBufferL (this, 323 -1, 324 0, 325 size, 326 size, 327 off))); 328 } 329 330 331 333 334 335 public int getInt() { 336 return Bits.getInt(this, ix(nextGetIndex(4)), bigEndian); 337 } 338 339 public int getInt(int i) { 340 return Bits.getInt(this, ix(checkIndex(i, 4)), bigEndian); 341 } 342 343 344 345 public ByteBuffer putInt(int x) { 346 347 Bits.putInt(this, ix(nextPutIndex(4)), x, bigEndian); 348 return this; 349 350 351 352 } 353 354 public ByteBuffer putInt(int i, int x) { 355 356 Bits.putInt(this, ix(checkIndex(i, 4)), x, bigEndian); 357 return this; 358 359 360 361 } 362 363 public IntBuffer asIntBuffer() { 364 int size = this.remaining() >> 2; 365 int off = offset + position(); 366 return (bigEndian 367 ? (IntBuffer )(new ByteBufferAsIntBufferB (this, 368 -1, 369 0, 370 size, 371 size, 372 off)) 373 : (IntBuffer )(new ByteBufferAsIntBufferL (this, 374 -1, 375 0, 376 size, 377 size, 378 off))); 379 } 380 381 382 384 385 386 public long getLong() { 387 return Bits.getLong(this, ix(nextGetIndex(8)), bigEndian); 388 } 389 390 public long getLong(int i) { 391 return Bits.getLong(this, ix(checkIndex(i, 8)), bigEndian); 392 } 393 394 395 396 public ByteBuffer putLong(long x) { 397 398 Bits.putLong(this, ix(nextPutIndex(8)), x, bigEndian); 399 return this; 400 401 402 403 } 404 405 public ByteBuffer putLong(int i, long x) { 406 407 Bits.putLong(this, ix(checkIndex(i, 8)), x, bigEndian); 408 return this; 409 410 411 412 } 413 414 public LongBuffer asLongBuffer() { 415 int size = this.remaining() >> 3; 416 int off = offset + position(); 417 return (bigEndian 418 ? (LongBuffer )(new ByteBufferAsLongBufferB (this, 419 -1, 420 0, 421 size, 422 size, 423 off)) 424 : (LongBuffer )(new ByteBufferAsLongBufferL (this, 425 -1, 426 0, 427 size, 428 size, 429 off))); 430 } 431 432 433 435 436 437 public float getFloat() { 438 return Bits.getFloat(this, ix(nextGetIndex(4)), bigEndian); 439 } 440 441 public float getFloat(int i) { 442 return Bits.getFloat(this, ix(checkIndex(i, 4)), bigEndian); 443 } 444 445 446 447 public ByteBuffer putFloat(float x) { 448 449 Bits.putFloat(this, ix(nextPutIndex(4)), x, bigEndian); 450 return this; 451 452 453 454 } 455 456 public ByteBuffer putFloat(int i, float x) { 457 458 Bits.putFloat(this, ix(checkIndex(i, 4)), x, bigEndian); 459 return this; 460 461 462 463 } 464 465 public FloatBuffer asFloatBuffer() { 466 int size = this.remaining() >> 2; 467 int off = offset + position(); 468 return (bigEndian 469 ? (FloatBuffer )(new ByteBufferAsFloatBufferB (this, 470 -1, 471 0, 472 size, 473 size, 474 off)) 475 : (FloatBuffer )(new ByteBufferAsFloatBufferL (this, 476 -1, 477 0, 478 size, 479 size, 480 off))); 481 } 482 483 484 486 487 488 public double getDouble() { 489 return Bits.getDouble(this, ix(nextGetIndex(8)), bigEndian); 490 } 491 492 public double getDouble(int i) { 493 return Bits.getDouble(this, ix(checkIndex(i, 8)), bigEndian); 494 } 495 496 497 498 public ByteBuffer putDouble(double x) { 499 500 Bits.putDouble(this, ix(nextPutIndex(8)), x, bigEndian); 501 return this; 502 503 504 505 } 506 507 public ByteBuffer putDouble(int i, double x) { 508 509 Bits.putDouble(this, ix(checkIndex(i, 8)), x, bigEndian); 510 return this; 511 512 513 514 } 515 516 public DoubleBuffer asDoubleBuffer() { 517 int size = this.remaining() >> 3; 518 int off = offset + position(); 519 return (bigEndian 520 ? (DoubleBuffer )(new ByteBufferAsDoubleBufferB (this, 521 -1, 522 0, 523 size, 524 size, 525 off)) 526 : (DoubleBuffer )(new ByteBufferAsDoubleBufferL (this, 527 -1, 528 0, 529 size, 530 size, 531 off))); 532 } 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 } 574 | Popular Tags |