1/* 2 * Copyright (C) 2010 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package libcore.java.nio; 18 19import junit.framework.TestCase; 20import java.io.File; 21import java.io.RandomAccessFile; 22import java.lang.reflect.Constructor; 23import java.lang.reflect.Field; 24import java.nio.Buffer; 25import java.nio.BufferOverflowException; 26import java.nio.BufferUnderflowException; 27import java.nio.ByteBuffer; 28import java.nio.ByteOrder; 29import java.nio.CharBuffer; 30import java.nio.DoubleBuffer; 31import java.nio.FloatBuffer; 32import java.nio.IntBuffer; 33import java.nio.LongBuffer; 34import java.nio.MappedByteBuffer; 35import java.nio.NioUtils; 36import java.nio.ReadOnlyBufferException; 37import java.nio.ShortBuffer; 38import java.nio.channels.FileChannel; 39import java.util.Arrays; 40import libcore.io.SizeOf; 41import libcore.io.Memory; 42 43public class BufferTest extends TestCase { 44 45 static { 46 System.loadLibrary("javacoretests"); 47 } 48 49 private static ByteBuffer allocateMapped(int size) throws Exception { 50 File f = File.createTempFile("mapped", "tmp"); 51 f.deleteOnExit(); 52 RandomAccessFile raf = new RandomAccessFile(f, "rw"); 53 raf.setLength(size); 54 FileChannel ch = raf.getChannel(); 55 MappedByteBuffer result = ch.map(FileChannel.MapMode.READ_WRITE, 0, size); 56 ch.close(); 57 return result; 58 } 59 60 /** 61 * Try to create a {@link MappedByteBuffer} from /dev/zero, to see if 62 * we support mapping UNIX character devices. 63 */ 64 public void testDevZeroMap() throws Exception { 65 RandomAccessFile raf = new RandomAccessFile("/dev/zero", "r"); 66 try { 67 MappedByteBuffer mbb = raf.getChannel().map(FileChannel.MapMode.READ_ONLY, 0, 65536); 68 69 // Create an array initialized to all "(byte) 1" 70 byte[] buf1 = new byte[65536]; 71 Arrays.fill(buf1, (byte) 1); 72 73 // Read from mapped /dev/zero, and overwrite this array. 74 mbb.get(buf1); 75 76 // Verify that everything is zero 77 for (int i = 0; i < 65536; i++) { 78 assertEquals((byte) 0, buf1[i]); 79 } 80 } finally { 81 raf.close(); 82 } 83 } 84 85 /** 86 * Same as {@link libcore.java.nio.BufferTest#testDevZeroMap()}, but try to see 87 * if we can write to the UNIX character device. 88 */ 89 public void testDevZeroMapRW() throws Exception { 90 RandomAccessFile raf = new RandomAccessFile("/dev/zero", "rw"); 91 try { 92 MappedByteBuffer mbb = raf.getChannel() 93 .map(FileChannel.MapMode.READ_WRITE, 65536, 131072); 94 95 // Create an array initialized to all "(byte) 1" 96 byte[] buf1 = new byte[65536]; 97 Arrays.fill(buf1, (byte) 1); 98 99 // Put all "(byte) 1"s into the /dev/zero MappedByteBuffer. 100 mbb.put(buf1); 101 102 mbb.position(0); 103 104 byte[] buf2 = new byte[65536]; 105 mbb.get(buf2); 106 107 // Verify that everything is one 108 for (int i = 0; i < 65536; i++) { 109 assertEquals((byte) 1, buf2[i]); 110 } 111 } finally { 112 raf.close(); 113 } 114 } 115 116 public void testByteSwappedBulkGetDirect() throws Exception { 117 testByteSwappedBulkGet(ByteBuffer.allocateDirect(10)); 118 } 119 120 public void testByteSwappedBulkGetHeap() throws Exception { 121 testByteSwappedBulkGet(ByteBuffer.allocate(10)); 122 } 123 124 public void testByteSwappedBulkGetMapped() throws Exception { 125 testByteSwappedBulkGet(allocateMapped(10)); 126 } 127 128 private void testByteSwappedBulkGet(ByteBuffer b) throws Exception { 129 for (int i = 0; i < b.limit(); ++i) { 130 b.put(i, (byte) i); 131 } 132 b.position(1); 133 134 char[] chars = new char[6]; 135 b.order(ByteOrder.BIG_ENDIAN).asCharBuffer().get(chars, 1, 4); 136 assertEquals("[\u0000, \u0102, \u0304, \u0506, \u0708, \u0000]", Arrays.toString(chars)); 137 b.order(ByteOrder.LITTLE_ENDIAN).asCharBuffer().get(chars, 1, 4); 138 assertEquals("[\u0000, \u0201, \u0403, \u0605, \u0807, \u0000]", Arrays.toString(chars)); 139 140 double[] doubles = new double[3]; 141 b.order(ByteOrder.BIG_ENDIAN).asDoubleBuffer().get(doubles, 1, 1); 142 assertEquals(0, Double.doubleToRawLongBits(doubles[0])); 143 assertEquals(0x0102030405060708L, Double.doubleToRawLongBits(doubles[1])); 144 assertEquals(0, Double.doubleToRawLongBits(doubles[2])); 145 b.order(ByteOrder.LITTLE_ENDIAN).asDoubleBuffer().get(doubles, 1, 1); 146 assertEquals(0, Double.doubleToRawLongBits(doubles[0])); 147 assertEquals(0x0807060504030201L, Double.doubleToRawLongBits(doubles[1])); 148 assertEquals(0, Double.doubleToRawLongBits(doubles[2])); 149 150 float[] floats = new float[4]; 151 b.order(ByteOrder.BIG_ENDIAN).asFloatBuffer().get(floats, 1, 2); 152 assertEquals(0, Float.floatToRawIntBits(floats[0])); 153 assertEquals(0x01020304, Float.floatToRawIntBits(floats[1])); 154 assertEquals(0x05060708, Float.floatToRawIntBits(floats[2])); 155 assertEquals(0, Float.floatToRawIntBits(floats[3])); 156 b.order(ByteOrder.LITTLE_ENDIAN).asFloatBuffer().get(floats, 1, 2); 157 assertEquals(0, Float.floatToRawIntBits(floats[0])); 158 assertEquals(0x04030201, Float.floatToRawIntBits(floats[1])); 159 assertEquals(0x08070605, Float.floatToRawIntBits(floats[2])); 160 assertEquals(0, Float.floatToRawIntBits(floats[3])); 161 162 int[] ints = new int[4]; 163 b.order(ByteOrder.BIG_ENDIAN).asIntBuffer().get(ints, 1, 2); 164 assertEquals(0, ints[0]); 165 assertEquals(0x01020304, ints[1]); 166 assertEquals(0x05060708, ints[2]); 167 assertEquals(0, ints[3]); 168 b.order(ByteOrder.LITTLE_ENDIAN).asIntBuffer().get(ints, 1, 2); 169 assertEquals(0, ints[0]); 170 assertEquals(0x04030201, ints[1]); 171 assertEquals(0x08070605, ints[2]); 172 assertEquals(0, ints[3]); 173 174 long[] longs = new long[3]; 175 b.order(ByteOrder.BIG_ENDIAN).asLongBuffer().get(longs, 1, 1); 176 assertEquals(0, longs[0]); 177 assertEquals(0x0102030405060708L, longs[1]); 178 assertEquals(0, longs[2]); 179 b.order(ByteOrder.LITTLE_ENDIAN).asLongBuffer().get(longs, 1, 1); 180 assertEquals(0, longs[0]); 181 assertEquals(0x0807060504030201L, longs[1]); 182 assertEquals(0, longs[2]); 183 184 short[] shorts = new short[6]; 185 b.order(ByteOrder.BIG_ENDIAN).asShortBuffer().get(shorts, 1, 4); 186 assertEquals(0, shorts[0]); 187 assertEquals(0x0102, shorts[1]); 188 assertEquals(0x0304, shorts[2]); 189 assertEquals(0x0506, shorts[3]); 190 assertEquals(0x0708, shorts[4]); 191 assertEquals(0, shorts[5]); 192 b.order(ByteOrder.LITTLE_ENDIAN).asShortBuffer().get(shorts, 1, 4); 193 assertEquals(0, shorts[0]); 194 assertEquals(0x0201, shorts[1]); 195 assertEquals(0x0403, shorts[2]); 196 assertEquals(0x0605, shorts[3]); 197 assertEquals(0x0807, shorts[4]); 198 assertEquals(0, shorts[5]); 199 } 200 201 private static String toString(ByteBuffer b) { 202 StringBuilder result = new StringBuilder(); 203 for (int i = 0; i < b.limit(); ++i) { 204 result.append(String.format("%02x", (int) b.get(i))); 205 } 206 return result.toString(); 207 } 208 209 public void testByteSwappedBulkPutDirect() throws Exception { 210 testByteSwappedBulkPut(ByteBuffer.allocateDirect(10)); 211 } 212 213 public void testByteSwappedBulkPutHeap() throws Exception { 214 testByteSwappedBulkPut(ByteBuffer.allocate(10)); 215 } 216 217 public void testByteSwappedBulkPutMapped() throws Exception { 218 testByteSwappedBulkPut(allocateMapped(10)); 219 } 220 221 private void testByteSwappedBulkPut(ByteBuffer b) throws Exception { 222 b.position(1); 223 224 char[] chars = new char[] { '\u2222', '\u0102', '\u0304', '\u0506', '\u0708', '\u2222' }; 225 b.order(ByteOrder.BIG_ENDIAN).asCharBuffer().put(chars, 1, 4); 226 assertEquals("00010203040506070800", toString(b)); 227 b.order(ByteOrder.LITTLE_ENDIAN).asCharBuffer().put(chars, 1, 4); 228 assertEquals("00020104030605080700", toString(b)); 229 230 double[] doubles = new double[] { 0, Double.longBitsToDouble(0x0102030405060708L), 0 }; 231 b.order(ByteOrder.BIG_ENDIAN).asDoubleBuffer().put(doubles, 1, 1); 232 assertEquals("00010203040506070800", toString(b)); 233 b.order(ByteOrder.LITTLE_ENDIAN).asDoubleBuffer().put(doubles, 1, 1); 234 assertEquals("00080706050403020100", toString(b)); 235 236 float[] floats = new float[] { 0, Float.intBitsToFloat(0x01020304), 237 Float.intBitsToFloat(0x05060708), 0 }; 238 b.order(ByteOrder.BIG_ENDIAN).asFloatBuffer().put(floats, 1, 2); 239 assertEquals("00010203040506070800", toString(b)); 240 b.order(ByteOrder.LITTLE_ENDIAN).asFloatBuffer().put(floats, 1, 2); 241 assertEquals("00040302010807060500", toString(b)); 242 243 int[] ints = new int[] { 0, 0x01020304, 0x05060708, 0 }; 244 b.order(ByteOrder.BIG_ENDIAN).asIntBuffer().put(ints, 1, 2); 245 assertEquals("00010203040506070800", toString(b)); 246 b.order(ByteOrder.LITTLE_ENDIAN).asIntBuffer().put(ints, 1, 2); 247 assertEquals("00040302010807060500", toString(b)); 248 249 long[] longs = new long[] { 0, 0x0102030405060708L, 0 }; 250 b.order(ByteOrder.BIG_ENDIAN).asLongBuffer().put(longs, 1, 1); 251 assertEquals("00010203040506070800", toString(b)); 252 b.order(ByteOrder.LITTLE_ENDIAN).asLongBuffer().put(longs, 1, 1); 253 assertEquals("00080706050403020100", toString(b)); 254 255 short[] shorts = new short[] { 0, 0x0102, 0x0304, 0x0506, 0x0708, 0 }; 256 b.order(ByteOrder.BIG_ENDIAN).asShortBuffer().put(shorts, 1, 4); 257 assertEquals("00010203040506070800", toString(b)); 258 b.order(ByteOrder.LITTLE_ENDIAN).asShortBuffer().put(shorts, 1, 4); 259 assertEquals("00020104030605080700", toString(b)); 260 } 261 262 public void testByteBufferByteOrderDirectRW() throws Exception { 263 testByteBufferByteOrder(ByteBuffer.allocateDirect(10), false); 264 } 265 266 public void testByteBufferByteOrderHeapRW() throws Exception { 267 testByteBufferByteOrder(ByteBuffer.allocate(10), false); 268 } 269 270 public void testByteBufferByteOrderMappedRW() throws Exception { 271 testByteBufferByteOrder(allocateMapped(10), false); 272 } 273 274 public void testByteBufferByteOrderDirectRO() throws Exception { 275 testByteBufferByteOrder(ByteBuffer.allocateDirect(10), true); 276 } 277 278 public void testByteBufferByteOrderHeapRO() throws Exception { 279 testByteBufferByteOrder(ByteBuffer.allocate(10), true); 280 } 281 282 public void testByteBufferByteOrderMappedRO() throws Exception { 283 testByteBufferByteOrder(allocateMapped(10), true); 284 } 285 286 private void testByteBufferByteOrder(ByteBuffer b, boolean readOnly) throws Exception { 287 if (readOnly) { 288 b = b.asReadOnlyBuffer(); 289 } 290 // allocate/allocateDirect/map always returns a big-endian buffer. 291 assertEquals(ByteOrder.BIG_ENDIAN, b.order()); 292 293 // wrap always returns a big-endian buffer. 294 assertEquals(ByteOrder.BIG_ENDIAN, b.wrap(new byte[10]).order()); 295 296 // duplicate always returns a big-endian buffer. 297 b.order(ByteOrder.BIG_ENDIAN); 298 assertEquals(ByteOrder.BIG_ENDIAN, b.duplicate().order()); 299 b.order(ByteOrder.LITTLE_ENDIAN); 300 assertEquals(ByteOrder.BIG_ENDIAN, b.duplicate().order()); 301 302 // slice always returns a big-endian buffer. 303 b.order(ByteOrder.BIG_ENDIAN); 304 assertEquals(ByteOrder.BIG_ENDIAN, b.slice().order()); 305 b.order(ByteOrder.LITTLE_ENDIAN); 306 assertEquals(ByteOrder.BIG_ENDIAN, b.slice().order()); 307 308 // asXBuffer always returns a current-endian buffer. 309 b.order(ByteOrder.BIG_ENDIAN); 310 assertEquals(ByteOrder.BIG_ENDIAN, b.asCharBuffer().order()); 311 assertEquals(ByteOrder.BIG_ENDIAN, b.asDoubleBuffer().order()); 312 assertEquals(ByteOrder.BIG_ENDIAN, b.asFloatBuffer().order()); 313 assertEquals(ByteOrder.BIG_ENDIAN, b.asIntBuffer().order()); 314 assertEquals(ByteOrder.BIG_ENDIAN, b.asLongBuffer().order()); 315 assertEquals(ByteOrder.BIG_ENDIAN, b.asShortBuffer().order()); 316 assertEquals(ByteOrder.BIG_ENDIAN, b.asReadOnlyBuffer().order()); 317 b.order(ByteOrder.LITTLE_ENDIAN); 318 assertEquals(ByteOrder.LITTLE_ENDIAN, b.asCharBuffer().order()); 319 assertEquals(ByteOrder.LITTLE_ENDIAN, b.asDoubleBuffer().order()); 320 assertEquals(ByteOrder.LITTLE_ENDIAN, b.asFloatBuffer().order()); 321 assertEquals(ByteOrder.LITTLE_ENDIAN, b.asIntBuffer().order()); 322 assertEquals(ByteOrder.LITTLE_ENDIAN, b.asLongBuffer().order()); 323 assertEquals(ByteOrder.LITTLE_ENDIAN, b.asShortBuffer().order()); 324 // ...except for asReadOnlyBuffer, which always returns a big-endian buffer. 325 assertEquals(ByteOrder.BIG_ENDIAN, b.asReadOnlyBuffer().order()); 326 } 327 328 public void testCharBufferByteOrderWrapped() throws Exception { 329 assertEquals(ByteOrder.nativeOrder(), CharBuffer.wrap(new char[10]).order()); 330 assertEquals(ByteOrder.nativeOrder(), CharBuffer.wrap(new char[10]).asReadOnlyBuffer().order()); 331 } 332 333 private void testCharBufferByteOrder(CharBuffer b, ByteOrder bo) throws Exception { 334 assertEquals(bo, b.order()); 335 assertEquals(bo, b.duplicate().order()); 336 assertEquals(bo, b.slice().order()); 337 b = b.asReadOnlyBuffer(); 338 assertEquals(bo, b.order()); 339 assertEquals(bo, b.duplicate().order()); 340 assertEquals(bo, b.slice().order()); 341 } 342 343 private CharBuffer allocateCharBuffer(ByteOrder order) { 344 return ByteBuffer.allocate(10).order(order).asCharBuffer(); 345 } 346 347 public void testCharBufferByteOrderArray() throws Exception { 348 testCharBufferByteOrder(CharBuffer.allocate(10), ByteOrder.nativeOrder()); 349 } 350 351 public void testCharBufferByteOrderBE() throws Exception { 352 testCharBufferByteOrder(allocateCharBuffer(ByteOrder.BIG_ENDIAN), ByteOrder.BIG_ENDIAN); 353 } 354 355 public void testCharBufferByteOrderLE() throws Exception { 356 testCharBufferByteOrder(allocateCharBuffer(ByteOrder.LITTLE_ENDIAN), ByteOrder.LITTLE_ENDIAN); 357 } 358 359 public void testDoubleBufferByteOrderWrapped() throws Exception { 360 assertEquals(ByteOrder.nativeOrder(), DoubleBuffer.wrap(new double[10]).order()); 361 assertEquals(ByteOrder.nativeOrder(), DoubleBuffer.wrap(new double[10]).asReadOnlyBuffer().order()); 362 } 363 364 private void testDoubleBufferByteOrder(DoubleBuffer b, ByteOrder bo) throws Exception { 365 assertEquals(bo, b.order()); 366 assertEquals(bo, b.duplicate().order()); 367 assertEquals(bo, b.slice().order()); 368 b = b.asReadOnlyBuffer(); 369 assertEquals(bo, b.order()); 370 assertEquals(bo, b.duplicate().order()); 371 assertEquals(bo, b.slice().order()); 372 } 373 374 private DoubleBuffer allocateDoubleBuffer(ByteOrder order) { 375 return ByteBuffer.allocate(10*8).order(order).asDoubleBuffer(); 376 } 377 378 public void testDoubleBufferByteOrderArray() throws Exception { 379 testDoubleBufferByteOrder(DoubleBuffer.allocate(10), ByteOrder.nativeOrder()); 380 } 381 382 public void testDoubleBufferByteOrderBE() throws Exception { 383 testDoubleBufferByteOrder(allocateDoubleBuffer(ByteOrder.BIG_ENDIAN), ByteOrder.BIG_ENDIAN); 384 } 385 386 public void testDoubleBufferByteOrderLE() throws Exception { 387 testDoubleBufferByteOrder(allocateDoubleBuffer(ByteOrder.LITTLE_ENDIAN), ByteOrder.LITTLE_ENDIAN); 388 } 389 390 public void testFloatBufferByteOrderWrapped() throws Exception { 391 assertEquals(ByteOrder.nativeOrder(), FloatBuffer.wrap(new float[10]).order()); 392 assertEquals(ByteOrder.nativeOrder(), FloatBuffer.wrap(new float[10]).asReadOnlyBuffer().order()); 393 } 394 395 private void testFloatBufferByteOrder(FloatBuffer b, ByteOrder bo) throws Exception { 396 assertEquals(bo, b.order()); 397 assertEquals(bo, b.duplicate().order()); 398 assertEquals(bo, b.slice().order()); 399 b = b.asReadOnlyBuffer(); 400 assertEquals(bo, b.order()); 401 assertEquals(bo, b.duplicate().order()); 402 assertEquals(bo, b.slice().order()); 403 } 404 405 private FloatBuffer allocateFloatBuffer(ByteOrder order) { 406 return ByteBuffer.allocate(10*8).order(order).asFloatBuffer(); 407 } 408 409 public void testFloatBufferByteOrderArray() throws Exception { 410 testFloatBufferByteOrder(FloatBuffer.allocate(10), ByteOrder.nativeOrder()); 411 } 412 413 public void testFloatBufferByteOrderBE() throws Exception { 414 testFloatBufferByteOrder(allocateFloatBuffer(ByteOrder.BIG_ENDIAN), ByteOrder.BIG_ENDIAN); 415 } 416 417 public void testFloatBufferByteOrderLE() throws Exception { 418 testFloatBufferByteOrder(allocateFloatBuffer(ByteOrder.LITTLE_ENDIAN), ByteOrder.LITTLE_ENDIAN); 419 } 420 421 public void testIntBufferByteOrderWrapped() throws Exception { 422 assertEquals(ByteOrder.nativeOrder(), IntBuffer.wrap(new int[10]).order()); 423 assertEquals(ByteOrder.nativeOrder(), IntBuffer.wrap(new int[10]).asReadOnlyBuffer().order()); 424 } 425 426 private void testIntBufferByteOrder(IntBuffer b, ByteOrder bo) throws Exception { 427 assertEquals(bo, b.order()); 428 assertEquals(bo, b.duplicate().order()); 429 assertEquals(bo, b.slice().order()); 430 b = b.asReadOnlyBuffer(); 431 assertEquals(bo, b.order()); 432 assertEquals(bo, b.duplicate().order()); 433 assertEquals(bo, b.slice().order()); 434 } 435 436 private IntBuffer allocateIntBuffer(ByteOrder order) { 437 return ByteBuffer.allocate(10*8).order(order).asIntBuffer(); 438 } 439 440 public void testIntBufferByteOrderArray() throws Exception { 441 testIntBufferByteOrder(IntBuffer.allocate(10), ByteOrder.nativeOrder()); 442 } 443 444 public void testIntBufferByteOrderBE() throws Exception { 445 testIntBufferByteOrder(allocateIntBuffer(ByteOrder.BIG_ENDIAN), ByteOrder.BIG_ENDIAN); 446 } 447 448 public void testIntBufferByteOrderLE() throws Exception { 449 testIntBufferByteOrder(allocateIntBuffer(ByteOrder.LITTLE_ENDIAN), ByteOrder.LITTLE_ENDIAN); 450 } 451 452 public void testLongBufferByteOrderWrapped() throws Exception { 453 assertEquals(ByteOrder.nativeOrder(), LongBuffer.wrap(new long[10]).order()); 454 assertEquals(ByteOrder.nativeOrder(), LongBuffer.wrap(new long[10]).asReadOnlyBuffer().order()); 455 } 456 457 private void testLongBufferByteOrder(LongBuffer b, ByteOrder bo) throws Exception { 458 assertEquals(bo, b.order()); 459 assertEquals(bo, b.duplicate().order()); 460 assertEquals(bo, b.slice().order()); 461 b = b.asReadOnlyBuffer(); 462 assertEquals(bo, b.order()); 463 assertEquals(bo, b.duplicate().order()); 464 assertEquals(bo, b.slice().order()); 465 } 466 467 private LongBuffer allocateLongBuffer(ByteOrder order) { 468 return ByteBuffer.allocate(10*8).order(order).asLongBuffer(); 469 } 470 471 public void testLongBufferByteOrderArray() throws Exception { 472 testLongBufferByteOrder(LongBuffer.allocate(10), ByteOrder.nativeOrder()); 473 } 474 475 public void testLongBufferByteOrderBE() throws Exception { 476 testLongBufferByteOrder(allocateLongBuffer(ByteOrder.BIG_ENDIAN), ByteOrder.BIG_ENDIAN); 477 } 478 479 public void testLongBufferByteOrderLE() throws Exception { 480 testLongBufferByteOrder(allocateLongBuffer(ByteOrder.LITTLE_ENDIAN), ByteOrder.LITTLE_ENDIAN); 481 } 482 483 public void testShortBufferByteOrderWrapped() throws Exception { 484 assertEquals(ByteOrder.nativeOrder(), ShortBuffer.wrap(new short[10]).order()); 485 assertEquals(ByteOrder.nativeOrder(), ShortBuffer.wrap(new short[10]).asReadOnlyBuffer().order()); 486 } 487 488 private void testShortBufferByteOrder(ShortBuffer b, ByteOrder bo) throws Exception { 489 assertEquals(bo, b.order()); 490 assertEquals(bo, b.duplicate().order()); 491 assertEquals(bo, b.slice().order()); 492 b = b.asReadOnlyBuffer(); 493 assertEquals(bo, b.order()); 494 assertEquals(bo, b.duplicate().order()); 495 assertEquals(bo, b.slice().order()); 496 } 497 498 private ShortBuffer allocateShortBuffer(ByteOrder order) { 499 return ByteBuffer.allocate(10*8).order(order).asShortBuffer(); 500 } 501 502 public void testShortBufferByteOrderArray() throws Exception { 503 testShortBufferByteOrder(ShortBuffer.allocate(10), ByteOrder.nativeOrder()); 504 } 505 506 public void testShortBufferByteOrderBE() throws Exception { 507 testShortBufferByteOrder(allocateShortBuffer(ByteOrder.BIG_ENDIAN), ByteOrder.BIG_ENDIAN); 508 } 509 510 public void testShortBufferByteOrderLE() throws Exception { 511 testShortBufferByteOrder(allocateShortBuffer(ByteOrder.LITTLE_ENDIAN), ByteOrder.LITTLE_ENDIAN); 512 } 513 514 public void testRelativePositionsHeap() throws Exception { 515 testRelativePositions(ByteBuffer.allocate(10)); 516 } 517 518 public void testRelativePositionsDirect() throws Exception { 519 testRelativePositions(ByteBuffer.allocateDirect(10)); 520 } 521 522 public void testRelativePositionsMapped() throws Exception { 523 testRelativePositions(allocateMapped(10)); 524 } 525 526 // http://b/3291927 - ensure that the relative get and put methods advance 'position'. 527 private void testRelativePositions(ByteBuffer b) throws Exception { 528 // gets 529 b.position(0); 530 b.get(); 531 assertEquals(1, b.position()); 532 533 byte[] buf = new byte[5]; 534 b.position(0); 535 b.get(buf); 536 assertEquals(5, b.position()); 537 538 b.position(0); 539 b.get(buf, 1, 3); 540 assertEquals(3, b.position()); 541 542 b.position(0); 543 b.getChar(); 544 assertEquals(2, b.position()); 545 546 b.position(0); 547 b.getDouble(); 548 assertEquals(8, b.position()); 549 550 b.position(0); 551 b.getFloat(); 552 assertEquals(4, b.position()); 553 554 b.position(0); 555 b.getInt(); 556 assertEquals(4, b.position()); 557 558 b.position(0); 559 b.getLong(); 560 assertEquals(8, b.position()); 561 562 b.position(0); 563 b.getShort(); 564 assertEquals(2, b.position()); 565 566 // puts 567 b.position(0); 568 b.put((byte) 0); 569 assertEquals(1, b.position()); 570 571 b.position(0); 572 b.put(buf); 573 assertEquals(5, b.position()); 574 575 b.position(0); 576 b.put(buf, 1, 3); 577 assertEquals(3, b.position()); 578 579 b.position(0); 580 b.putChar('x'); 581 assertEquals(2, b.position()); 582 583 b.position(0); 584 b.putDouble(0); 585 assertEquals(8, b.position()); 586 587 b.position(0); 588 b.putFloat(0); 589 assertEquals(4, b.position()); 590 591 b.position(0); 592 b.putInt(0); 593 assertEquals(4, b.position()); 594 595 b.position(0); 596 b.putLong(0); 597 assertEquals(8, b.position()); 598 599 b.position(0); 600 b.putShort((short) 0); 601 assertEquals(2, b.position()); 602 } 603 604 // This test will fail on the RI. Our direct buffers are cooler than theirs. 605 // http://b/3384431 606 public void testDirectByteBufferHasArray() throws Exception { 607 ByteBuffer b = ByteBuffer.allocateDirect(10); 608 assertTrue(b.isDirect()); 609 // Check the buffer has an array of the right size. 610 assertTrue(b.hasArray()); 611 byte[] array = b.array(); 612 assertTrue(array.length >= b.capacity()); 613 assertEquals(10, b.capacity()); 614 // Check that writes to the array show up in the buffer. 615 assertEquals(0, b.get(0)); 616 array[b.arrayOffset()] = 1; 617 assertEquals(1, b.get(0)); 618 // Check that writes to the buffer show up in the array. 619 assertEquals(1, array[b.arrayOffset()]); 620 b.put(0, (byte) 0); 621 assertEquals(0, array[b.arrayOffset()]); 622 } 623 624 // Test that direct byte buffers are 8 byte aligned. 625 // http://b/16449607 626 public void testDirectByteBufferAlignment() throws Exception { 627 ByteBuffer b = ByteBuffer.allocateDirect(10); 628 Field addressField = Buffer.class.getDeclaredField("address"); 629 assertTrue(addressField != null); 630 addressField.setAccessible(true); 631 long address = addressField.getLong(b); 632 // Check that the address field is aligned by 8. 633 // Normally reading this field happens in native code by calling 634 // GetDirectBufferAddress. 635 assertEquals(0, address % 8); 636 } 637 638 public static native long jniGetDirectBufferAddress(Buffer buf); 639 public static native long jniGetDirectBufferCapacity(Buffer buf); 640 641 // Test that JNI GetDirectBufferAddress and GetDirectBufferCapacity work as expected for 642 // direct ByteBuffers. 643 // http://b/26233076 644 public void testDirectByteBufferJniGetDirectBufferAddressAndCapacity() throws Exception { 645 // Create a direct ByteBuffer with the following contents. 646 byte[] contents = "Testing, 1, 2, 3...".getBytes("US-ASCII"); 647 ByteBuffer original = ByteBuffer.allocateDirect(contents.length); 648 original.put(contents); 649 650 // Check the content of the original buffer by reading the memory it references. 651 long originalAddress = jniGetDirectBufferAddress(original); 652 if (originalAddress == 0) { 653 fail("Obtaining address of direct ByteBuffer not supported"); 654 } 655 long originalCapacity = jniGetDirectBufferCapacity(original); 656 assertEquals(contents.length, originalCapacity); 657 byte[] originalData = new byte[original.capacity()]; 658 Memory.peekByteArray(originalAddress, originalData, 0, originalData.length); 659 assertTrue(new String(originalData, "US-ASCII"), Arrays.equals(contents, originalData)); 660 661 // Slice the buffer and check the content of the result. 662 // The slice starts at offset 3 of the original and is 5 bytes shorter than the original. 663 original.position(3); 664 original.limit(original.capacity() - 2); 665 ByteBuffer slice = original.slice(); 666 System.out.println("original: " + original + ", slice: " + slice); 667 668 long sliceAddress = jniGetDirectBufferAddress(slice); 669 System.out.println("originalAddress: 0x" + Long.toHexString(originalAddress) 670 + ", sliceAddress: 0x" + Long.toHexString(sliceAddress)); 671 assertEquals(3 + originalAddress, sliceAddress); 672 long sliceCapacity = jniGetDirectBufferCapacity(slice); 673 assertEquals(originalCapacity - 5, sliceCapacity); 674 byte[] actualSliceData = new byte[slice.capacity()]; 675 Memory.peekByteArray(sliceAddress, actualSliceData, 0, actualSliceData.length); 676 byte[] expectedSliceData = new byte[slice.capacity()]; 677 System.arraycopy(originalData, 3, expectedSliceData, 0, expectedSliceData.length); 678 assertTrue( 679 new String(actualSliceData, "US-ASCII"), 680 Arrays.equals(expectedSliceData, actualSliceData)); 681 } 682 683 public void testSliceOffset() throws Exception { 684 // Slicing changes the array offset. 685 ByteBuffer buffer = ByteBuffer.allocate(10); 686 buffer.get(); 687 ByteBuffer slice = buffer.slice(); 688 assertEquals(buffer.arrayOffset() + 1, slice.arrayOffset()); 689 690 ByteBuffer directBuffer = ByteBuffer.allocateDirect(10); 691 directBuffer.get(); 692 ByteBuffer directSlice = directBuffer.slice(); 693 assertEquals(directBuffer.arrayOffset() + 1, directSlice.arrayOffset()); 694 } 695 696 // http://code.google.com/p/android/issues/detail?id=16184 697 public void testPutByteBuffer() throws Exception { 698 ByteBuffer dst = ByteBuffer.allocate(10).asReadOnlyBuffer(); 699 700 // Can't put into a read-only buffer. 701 try { 702 dst.put(ByteBuffer.allocate(5)); 703 fail(); 704 } catch (ReadOnlyBufferException expected) { 705 } 706 707 // Can't put a buffer into itself. 708 dst = ByteBuffer.allocate(10); 709 try { 710 dst.put(dst); 711 fail(); 712 } catch (IllegalArgumentException expected) { 713 } 714 715 // Can't put the null ByteBuffer. 716 try { 717 dst.put((ByteBuffer) null); 718 fail(); 719 } catch (NullPointerException expected) { 720 } 721 722 // Can't put a larger source into a smaller destination. 723 try { 724 dst.put(ByteBuffer.allocate(dst.capacity() + 1)); 725 fail(); 726 } catch (BufferOverflowException expected) { 727 } 728 729 assertPutByteBuffer(ByteBuffer.allocate(10), ByteBuffer.allocate(8), false); 730 assertPutByteBuffer(ByteBuffer.allocate(10), ByteBuffer.allocateDirect(8), false); 731 assertPutByteBuffer(ByteBuffer.allocate(10), allocateMapped(8), false); 732 assertPutByteBuffer(ByteBuffer.allocate(10), ByteBuffer.allocate(8), true); 733 assertPutByteBuffer(ByteBuffer.allocate(10), ByteBuffer.allocateDirect(8), true); 734 assertPutByteBuffer(ByteBuffer.allocate(10), allocateMapped(8), true); 735 736 assertPutByteBuffer(ByteBuffer.allocateDirect(10), ByteBuffer.allocate(8), false); 737 assertPutByteBuffer(ByteBuffer.allocateDirect(10), ByteBuffer.allocateDirect(8), false); 738 assertPutByteBuffer(ByteBuffer.allocateDirect(10), allocateMapped(8), false); 739 assertPutByteBuffer(ByteBuffer.allocateDirect(10), ByteBuffer.allocate(8), true); 740 assertPutByteBuffer(ByteBuffer.allocateDirect(10), ByteBuffer.allocateDirect(8), true); 741 assertPutByteBuffer(ByteBuffer.allocateDirect(10), allocateMapped(8), true); 742 } 743 744 private void assertPutByteBuffer(ByteBuffer dst, ByteBuffer src, boolean readOnly) { 745 // Start 'dst' off as the index-identity pattern. 746 for (int i = 0; i < dst.capacity(); ++i) { 747 dst.put(i, (byte) i); 748 } 749 // Deliberately offset the position we'll write to by 1. 750 dst.position(1); 751 752 // Make the source more interesting. 753 for (int i = 0; i < src.capacity(); ++i) { 754 src.put(i, (byte) (16 + i)); 755 } 756 if (readOnly) { 757 src = src.asReadOnlyBuffer(); 758 } 759 760 ByteBuffer dst2 = dst.put(src); 761 assertSame(dst, dst2); 762 assertEquals(0, src.remaining()); 763 assertEquals(src.position(), src.capacity()); 764 assertEquals(dst.position(), src.capacity() + 1); 765 for (int i = 0; i < src.capacity(); ++i) { 766 assertEquals(src.get(i), dst.get(i + 1)); 767 } 768 769 // No room for another. 770 src.position(0); 771 try { 772 dst.put(src); 773 fail(); 774 } catch (BufferOverflowException expected) { 775 } 776 } 777 778 public void testCharBufferSubSequence() throws Exception { 779 ByteBuffer b = ByteBuffer.allocateDirect(10).order(ByteOrder.nativeOrder()); 780 b.putChar('H'); 781 b.putChar('e'); 782 b.putChar('l'); 783 b.putChar('l'); 784 b.putChar('o'); 785 b.flip(); 786 787 assertEquals("Hello", b.asCharBuffer().toString()); 788 789 CharBuffer cb = b.asCharBuffer(); 790 CharSequence cs = cb.subSequence(0, cb.length()); 791 assertEquals("Hello", cs.toString()); 792 } 793 794 public void testHasArrayOnJniDirectByteBuffer() throws Exception { 795 // Simulate a call to JNI's NewDirectByteBuffer. 796 Class<?> c = Class.forName("java.nio.DirectByteBuffer"); 797 Constructor<?> ctor = c.getDeclaredConstructor(long.class, int.class); 798 ctor.setAccessible(true); 799 ByteBuffer bb = (ByteBuffer) ctor.newInstance(0, 0); 800 801 try { 802 bb.array(); 803 fail(); 804 } catch (UnsupportedOperationException expected) { 805 } 806 try { 807 bb.arrayOffset(); 808 fail(); 809 } catch (UnsupportedOperationException expected) { 810 } 811 assertFalse(bb.hasArray()); 812 } 813 814 public void testBug6085292() { 815 ByteBuffer b = ByteBuffer.allocateDirect(1); 816 817 try { 818 b.asCharBuffer().get(); 819 fail(); 820 } catch (BufferUnderflowException expected) { 821 } 822 try { 823 b.asCharBuffer().get(0); 824 fail(); 825 } catch (IndexOutOfBoundsException expected) { 826 assertTrue(expected.getMessage().contains("limit=0")); 827 } 828 829 try { 830 b.asDoubleBuffer().get(); 831 fail(); 832 } catch (BufferUnderflowException expected) { 833 } 834 try { 835 b.asDoubleBuffer().get(0); 836 fail(); 837 } catch (IndexOutOfBoundsException expected) { 838 assertTrue(expected.getMessage().contains("limit=0")); 839 } 840 841 try { 842 b.asFloatBuffer().get(); 843 fail(); 844 } catch (BufferUnderflowException expected) { 845 } 846 try { 847 b.asFloatBuffer().get(0); 848 fail(); 849 } catch (IndexOutOfBoundsException expected) { 850 assertTrue(expected.getMessage().contains("limit=0")); 851 } 852 853 try { 854 b.asIntBuffer().get(); 855 fail(); 856 } catch (BufferUnderflowException expected) { 857 } 858 try { 859 b.asIntBuffer().get(0); 860 fail(); 861 } catch (IndexOutOfBoundsException expected) { 862 assertTrue(expected.getMessage().contains("limit=0")); 863 } 864 865 try { 866 b.asLongBuffer().get(); 867 fail(); 868 } catch (BufferUnderflowException expected) { 869 } 870 try { 871 b.asLongBuffer().get(0); 872 fail(); 873 } catch (IndexOutOfBoundsException expected) { 874 assertTrue(expected.getMessage().contains("limit=0")); 875 } 876 877 try { 878 b.asShortBuffer().get(); 879 fail(); 880 } catch (BufferUnderflowException expected) { 881 } 882 try { 883 b.asShortBuffer().get(0); 884 fail(); 885 } catch (IndexOutOfBoundsException expected) { 886 assertTrue(expected.getMessage().contains("limit=0")); 887 } 888 } 889 890 public void testUsingDirectBufferAsMappedBuffer() throws Exception { 891 MappedByteBuffer notMapped = (MappedByteBuffer) ByteBuffer.allocateDirect(1); 892 try { 893 notMapped.force(); 894 fail(); 895 } catch (UnsupportedOperationException expected) { 896 } 897 try { 898 notMapped.isLoaded(); 899 fail(); 900 } catch (UnsupportedOperationException expected) { 901 } 902 try { 903 notMapped.load(); 904 fail(); 905 } catch (UnsupportedOperationException expected) { 906 } 907 908 MappedByteBuffer mapped = (MappedByteBuffer) allocateMapped(1); 909 mapped.force(); 910 mapped.isLoaded(); 911 mapped.load(); 912 } 913 914 // https://code.google.com/p/android/issues/detail?id=53637 915 public void testBug53637() throws Exception { 916 MappedByteBuffer mapped = (MappedByteBuffer) allocateMapped(1); 917 mapped.get(); 918 mapped.rewind(); 919 mapped.get(); 920 921 mapped.rewind(); 922 mapped.mark(); 923 mapped.get(); 924 mapped.reset(); 925 mapped.get(); 926 927 mapped.rewind(); 928 mapped.get(); 929 mapped.clear(); 930 mapped.get(); 931 932 mapped.rewind(); 933 mapped.get(); 934 mapped.flip(); 935 mapped.get(); 936 } 937 938 public void testElementSizeShifts() { 939 // Element size shifts are the log base 2 of the element size 940 // of this buffer. 941 assertEquals(1, 1 << ByteBuffer.allocate(0).getElementSizeShift()); 942 943 assertEquals(SizeOf.CHAR, 1 << CharBuffer.allocate(0).getElementSizeShift()); 944 assertEquals(SizeOf.SHORT, 1 << ShortBuffer.allocate(0).getElementSizeShift()); 945 946 assertEquals(SizeOf.INT, 1 << IntBuffer.allocate(0).getElementSizeShift()); 947 assertEquals(SizeOf.FLOAT, 1 << FloatBuffer.allocate(0).getElementSizeShift()); 948 949 assertEquals(SizeOf.LONG, 1 << LongBuffer.allocate(0).getElementSizeShift()); 950 assertEquals(SizeOf.DOUBLE, 1 << DoubleBuffer.allocate(0).getElementSizeShift()); 951 } 952 953 public void testFreed() { 954 ByteBuffer b1 = ByteBuffer.allocateDirect(1); 955 ByteBuffer b2 = b1.duplicate(); 956 NioUtils.freeDirectBuffer(b1); 957 for (ByteBuffer b: new ByteBuffer[] { b1, b2 }) { 958 //assertFalse(b.isAccessible()); 959 try { 960 b.compact(); 961 fail(); 962 } catch (IllegalStateException expected) { 963 } 964 try { 965 b.duplicate(); 966 fail(); 967 } catch (IllegalStateException expected) { 968 } 969 testFailForPutMethods(b); 970 testFailForAsMethods(b); 971 testFailForGetMethods(b); 972 NioUtils.freeDirectBuffer(b); // should be able to free twice 973 } 974 } 975 976 /* setAccessible is not available in OpenJdk's buffers. 977 public void testAccess() { 978 ByteBuffer b1 = ByteBuffer.allocate(1); 979 ByteBuffer b2 = b1.duplicate(); 980 for (ByteBuffer b: new ByteBuffer[] { b1, b2 }) { 981 try { 982 b.setAccessible(true); 983 fail(); 984 } catch (UnsupportedOperationException expected) { 985 } 986 try { 987 b.setAccessible(false); 988 fail(); 989 } catch (UnsupportedOperationException expected) { 990 } 991 } 992 b1 = ByteBuffer.allocateDirect(8); 993 b2 = b1.duplicate(); 994 b1.setAccessible(false); 995 ByteBuffer b3 = b1.asReadOnlyBuffer(); 996 for (ByteBuffer b: new ByteBuffer[] { b1, b2, b3 }) { 997 b.duplicate(); 998 assertFalse(b.isAccessible()); 999 // even read-only buffers should fail with IllegalStateException 1000 testFailForPutMethods(b); 1001 testAsMethods(b); 1002 testFailForGetMethods(b); 1003 b.position(0); 1004 b.limit(8); 1005 try { 1006 b.asCharBuffer().get(0); 1007 fail(); 1008 } catch (IllegalStateException expected) { 1009 } 1010 try { 1011 b.asShortBuffer().get(0); 1012 fail(); 1013 } catch (IllegalStateException expected) { 1014 } 1015 try { 1016 b.asIntBuffer().get(0); 1017 fail(); 1018 } catch (IllegalStateException expected) { 1019 } 1020 try { 1021 b.asLongBuffer().get(0); 1022 fail(); 1023 } catch (IllegalStateException expected) { 1024 } 1025 try { 1026 b.asFloatBuffer().get(0); 1027 fail(); 1028 } catch (IllegalStateException expected) { 1029 } 1030 try { 1031 b.asDoubleBuffer().get(0); 1032 fail(); 1033 } catch (IllegalStateException expected) { 1034 } 1035 } 1036 b2.setAccessible(true); 1037 for (ByteBuffer b: new ByteBuffer[] { b1, b2, b3 }) { 1038 assertTrue(b.isAccessible()); 1039 b.position(0); 1040 b.limit(8); 1041 b.asCharBuffer().get(0); 1042 b.asShortBuffer().get(0); 1043 b.asIntBuffer().get(0); 1044 b.asLongBuffer().get(0); 1045 b.asFloatBuffer().get(0); 1046 b.asDoubleBuffer().get(0); 1047 if (!b.isReadOnly()) { 1048 testPutMethods(b); 1049 b.compact(); 1050 } else { 1051 try { 1052 b.put(0, (byte) 0); 1053 fail(); 1054 } catch (ReadOnlyBufferException expected) { 1055 } 1056 } 1057 testAsMethods(b); 1058 testGetMethods(b); 1059 } 1060 }*/ 1061 1062 private void testPutMethods(ByteBuffer b) { 1063 b.position(0); 1064 b.put((byte) 0); 1065 b.put(0, (byte) 0); 1066 b.put(new byte[1]); 1067 b.put(new byte[1], 0, 1); 1068 b.put(ByteBuffer.allocate(1)); 1069 b.putChar('a'); 1070 b.putChar(0, 'a'); 1071 b.position(0); 1072 b.putDouble(0); 1073 b.putDouble(0, 0); 1074 b.position(0); 1075 b.putFloat(0); 1076 b.putFloat(0, 0); 1077 b.putInt(0); 1078 b.putInt(0, 0); 1079 b.position(0); 1080 b.putLong(0); 1081 b.putLong(0, 0); 1082 b.position(0); 1083 b.putShort((short) 0); 1084 b.putShort(0, (short) 0); 1085 } 1086 1087 private void testFailForPutMethods(ByteBuffer b) { 1088 try { 1089 b.put((byte) 0); 1090 fail(); 1091 } catch (IllegalStateException expected) { 1092 } 1093 try { 1094 b.put(0, (byte) 0); 1095 fail(); 1096 } catch (IllegalStateException expected) { 1097 } 1098 try { 1099 b.put(new byte[1]); 1100 fail(); 1101 } catch (IllegalStateException expected) { 1102 } 1103 try { 1104 b.put(new byte[1], 0, 1); 1105 fail(); 1106 } catch (IllegalStateException expected) { 1107 } 1108 try { 1109 b.put(ByteBuffer.allocate(1)); 1110 fail(); 1111 } catch (IllegalStateException expected) { 1112 } 1113 try { 1114 b.putChar('a'); 1115 fail(); 1116 } catch (IllegalStateException expected) { 1117 } 1118 try { 1119 b.putChar(0, 'a'); 1120 fail(); 1121 } catch (IllegalStateException expected) { 1122 } 1123 try { 1124 b.putDouble(0); 1125 fail(); 1126 } catch (IllegalStateException expected) { 1127 } 1128 try { 1129 b.putDouble(0, 0); 1130 fail(); 1131 } catch (IllegalStateException expected) { 1132 } 1133 try { 1134 b.putFloat(0); 1135 fail(); 1136 } catch (IllegalStateException expected) { 1137 } 1138 try { 1139 b.putFloat(0, 0); 1140 fail(); 1141 } catch (IllegalStateException expected) { 1142 } 1143 try { 1144 b.putInt(0); 1145 fail(); 1146 } catch (IllegalStateException expected) { 1147 } 1148 try { 1149 b.putInt(0, 0); 1150 fail(); 1151 } catch (IllegalStateException expected) { 1152 } 1153 try { 1154 b.putLong(0); 1155 fail(); 1156 } catch (IllegalStateException expected) { 1157 } 1158 try { 1159 b.putLong(0, 0); 1160 fail(); 1161 } catch (IllegalStateException expected) { 1162 } 1163 try { 1164 b.putShort((short) 0); 1165 fail(); 1166 } catch (IllegalStateException expected) { 1167 } 1168 try { 1169 b.putShort(0, (short) 0); 1170 fail(); 1171 } catch (IllegalStateException expected) { 1172 } 1173 } 1174 1175 private void testGetMethods(ByteBuffer b) { 1176 b.position(0); 1177 b.get(); 1178 b.get(0); 1179 b.get(new byte[1]); 1180 b.get(new byte[1], 0, 1); 1181 b.getChar(); 1182 b.getChar(0); 1183 b.position(0); 1184 b.getDouble(); 1185 b.getDouble(0); 1186 b.position(0); 1187 b.getFloat(); 1188 b.getFloat(0); 1189 b.getInt(); 1190 b.getInt(0); 1191 b.position(0); 1192 b.getLong(); 1193 b.getLong(0); 1194 b.position(0); 1195 b.getShort(); 1196 b.getShort(0); 1197 } 1198 1199 private void testFailForGetMethods(ByteBuffer b) { 1200 try { 1201 b.get(); 1202 fail(); 1203 } catch (IllegalStateException expected) { 1204 } 1205 try { 1206 b.get(0); 1207 fail(); 1208 } catch (IllegalStateException expected) { 1209 } 1210 try { 1211 b.get(new byte[1]); 1212 fail(); 1213 } catch (IllegalStateException expected) { 1214 } 1215 try { 1216 b.get(new byte[1], 0, 1); 1217 fail(); 1218 } catch (IllegalStateException expected) { 1219 } 1220 try { 1221 b.getChar(); 1222 fail(); 1223 } catch (IllegalStateException expected) { 1224 } 1225 try { 1226 b.getChar(0); 1227 fail(); 1228 } catch (IllegalStateException expected) { 1229 } 1230 try { 1231 b.getDouble(); 1232 fail(); 1233 } catch (IllegalStateException expected) { 1234 } 1235 try { 1236 b.getDouble(0); 1237 fail(); 1238 } catch (IllegalStateException expected) { 1239 } 1240 try { 1241 b.getFloat(); 1242 fail(); 1243 } catch (IllegalStateException expected) { 1244 } 1245 try { 1246 b.getFloat(0); 1247 fail(); 1248 } catch (IllegalStateException expected) { 1249 } 1250 try { 1251 b.getInt(); 1252 fail(); 1253 } catch (IllegalStateException expected) { 1254 } 1255 try { 1256 b.getInt(0); 1257 fail(); 1258 } catch (IllegalStateException expected) { 1259 } 1260 try { 1261 b.getLong(); 1262 fail(); 1263 } catch (IllegalStateException expected) { 1264 } 1265 try { 1266 b.getLong(0); 1267 fail(); 1268 } catch (IllegalStateException expected) { 1269 } 1270 try { 1271 b.getShort(); 1272 fail(); 1273 } catch (IllegalStateException expected) { 1274 } 1275 try { 1276 b.getShort(0); 1277 fail(); 1278 } catch (IllegalStateException expected) { 1279 } 1280 } 1281 1282 private void testAsMethods(ByteBuffer b) { 1283 b.asCharBuffer(); 1284 b.asDoubleBuffer(); 1285 b.asFloatBuffer(); 1286 b.asIntBuffer(); 1287 b.asLongBuffer(); 1288 b.asReadOnlyBuffer(); 1289 b.asShortBuffer(); 1290 } 1291 1292 private void testFailForAsMethods(ByteBuffer b) { 1293 try { 1294 b.asCharBuffer(); 1295 fail(); 1296 } catch (IllegalStateException expected) { 1297 } 1298 try { 1299 b.asDoubleBuffer(); 1300 fail(); 1301 } catch (IllegalStateException expected) { 1302 } 1303 try { 1304 b.asFloatBuffer(); 1305 fail(); 1306 } catch (IllegalStateException expected) { 1307 } 1308 try { 1309 b.asIntBuffer(); 1310 fail(); 1311 } catch (IllegalStateException expected) { 1312 } 1313 try { 1314 b.asLongBuffer(); 1315 fail(); 1316 } catch (IllegalStateException expected) { 1317 } 1318 try { 1319 b.asReadOnlyBuffer(); 1320 fail(); 1321 } catch (IllegalStateException expected) { 1322 } 1323 try { 1324 b.asShortBuffer(); 1325 fail(); 1326 } catch (IllegalStateException expected) { 1327 } 1328 } 1329 1330 // b/26019694 1331 public void testIndependentLimit() { 1332 // Check if the limit parameter of original ByteBuffer 1333 // is not affecting buffer created by its as*Buffer 1334 testBuffersIndependentLimit(ByteBuffer.allocateDirect(16)); 1335 testBuffersIndependentLimit(ByteBuffer.allocate(16)); 1336 } 1337 1338 private void testBuffersIndependentLimit(ByteBuffer b) { 1339 CharBuffer c = b.asCharBuffer(); 1340 b.limit(b.capacity()); c.put(1, (char)1); 1341 b.limit(0); c.put(1, (char)1); 1342 1343 b.limit(b.capacity()); 1344 ShortBuffer s = b.asShortBuffer(); 1345 b.limit(b.capacity()); s.put(1, (short)1); 1346 b.limit(0); s.put(1, (short)1); 1347 1348 b.limit(b.capacity()); 1349 IntBuffer i = b.asIntBuffer(); 1350 i.put(1, (int)1); 1351 b.limit(0); i.put(1, (int)1); 1352 1353 b.limit(b.capacity()); 1354 LongBuffer l = b.asLongBuffer(); 1355 l.put(1, (long)1); 1356 b.limit(0); l.put(1, (long)1); 1357 1358 b.limit(b.capacity()); 1359 FloatBuffer f = b.asFloatBuffer(); 1360 f.put(1, (float)1); 1361 b.limit(0); f.put(1, (float)1); 1362 1363 b.limit(b.capacity()); 1364 DoubleBuffer d = b.asDoubleBuffer(); 1365 d.put(1, (double)1); 1366 b.limit(0); d.put(1, (double)1); 1367 } 1368} 1369