1 /*
   2  * Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /* Type-specific source code for unit test
  25  *
  26  * Regenerate the BasicX classes via genBasic.sh whenever this file changes.
  27  * We check in the generated source files so that the test tree can be used
  28  * independently of the rest of the source tree.
  29  */
  30 
  31 // -- This file was mechanically generated: Do not edit! -- //
  32 
  33 import java.nio.*;
  34 import java.lang.reflect.Method;
  35 
  36 
  37 public class BasicByte
  38     extends Basic
  39 {
  40 
  41     private static final byte[] VALUES = {
  42         Byte.MIN_VALUE,
  43         (byte) -1,
  44         (byte) 0,
  45         (byte) 1,
  46         Byte.MAX_VALUE,
  47 
  48 
  49 
  50 
  51 
  52 
  53 
  54 
  55 
  56 
  57 
  58 
  59     };
  60 
  61     private static void relGet(ByteBuffer b) {
  62         int n = b.capacity();
  63         byte v;
  64         for (int i = 0; i < n; i++)
  65             ck(b, (long)b.get(), (long)((byte)ic(i)));
  66         b.rewind();
  67     }
  68 
  69     private static void relGet(ByteBuffer b, int start) {
  70         int n = b.remaining();
  71         byte v;
  72         for (int i = start; i < n; i++)
  73             ck(b, (long)b.get(), (long)((byte)ic(i)));
  74         b.rewind();
  75     }
  76 
  77     private static void absGet(ByteBuffer b) {
  78         int n = b.capacity();
  79         byte v;
  80         for (int i = 0; i < n; i++)
  81             ck(b, (long)b.get(), (long)((byte)ic(i)));
  82         b.rewind();
  83     }
  84 
  85     private static void bulkGet(ByteBuffer b) {
  86         int n = b.capacity();
  87         byte[] a = new byte[n + 7];
  88         b.get(a, 7, n);
  89         for (int i = 0; i < n; i++)
  90             ck(b, (long)a[i + 7], (long)((byte)ic(i)));
  91     }
  92 
  93     private static void relPut(ByteBuffer b) {
  94         int n = b.capacity();
  95         b.clear();
  96         for (int i = 0; i < n; i++)
  97             b.put((byte)ic(i));
  98         b.flip();
  99     }
 100 
 101     private static void absPut(ByteBuffer b) {
 102         int n = b.capacity();
 103         b.clear();
 104         for (int i = 0; i < n; i++)
 105             b.put(i, (byte)ic(i));
 106         b.limit(n);
 107         b.position(0);
 108     }
 109 
 110     private static void bulkPutArray(ByteBuffer b) {
 111         int n = b.capacity();
 112         b.clear();
 113         byte[] a = new byte[n + 7];
 114         for (int i = 0; i < n; i++)
 115             a[i + 7] = (byte)ic(i);
 116         b.put(a, 7, n);
 117         b.flip();
 118     }
 119 
 120     private static void bulkPutBuffer(ByteBuffer b) {
 121         int n = b.capacity();
 122         b.clear();
 123         ByteBuffer c = ByteBuffer.allocate(n + 7);
 124         c.position(7);
 125         for (int i = 0; i < n; i++)
 126             c.put((byte)ic(i));
 127         c.flip();
 128         c.position(7);
 129         b.put(c);
 130         b.flip();
 131         try {
 132             b.put(b);
 133             fail("IllegalArgumentException expected for putting into same buffer");
 134         } catch (IllegalArgumentException e) {
 135             if (e.getMessage() == null) {
 136                 fail("Non-null IllegalArgumentException message expected from putting into same buffer");
 137             }
 138         }
 139     }
 140 
 141     //6231529
 142     private static void callReset(ByteBuffer b) {
 143         b.position(0);
 144         b.mark();
 145 
 146         b.duplicate().reset();
 147         b.asReadOnlyBuffer().reset();
 148     }
 149 
 150 
 151 
 152 
 153 
 154 
 155 
 156 
 157 
 158 
 159 
 160 
 161 
 162 
 163 
 164 
 165 
 166 
 167 
 168 
 169 
 170 
 171 
 172 
 173 
 174 
 175 
 176 
 177 
 178 
 179 
 180 
 181 
 182 
 183 
 184 
 185 
 186 
 187 
 188 
 189 
 190     private static void checkSlice(ByteBuffer b, ByteBuffer slice) {
 191         ck(slice, 0, slice.position());
 192         ck(slice, b.remaining(), slice.limit());
 193         ck(slice, b.remaining(), slice.capacity());
 194         if (b.isDirect() != slice.isDirect())
 195             fail("Lost direction", slice);
 196         if (b.isReadOnly() != slice.isReadOnly())
 197             fail("Lost read-only", slice);
 198     }
 199 
 200 
 201 
 202     private static void checkBytes(ByteBuffer b, byte[] bs) {
 203         int n = bs.length;
 204         int p = b.position();
 205         byte v;
 206         if (b.order() == ByteOrder.BIG_ENDIAN) {
 207             for (int i = 0; i < n; i++)
 208                 ck(b, b.get(), bs[i]);
 209         } else {
 210             for (int i = n - 1; i >= 0; i--)
 211                 ck(b, b.get(), bs[i]);
 212         }
 213         b.position(p);
 214     }
 215 
 216     private static void compact(Buffer b) {
 217         try {
 218             Class<?> cl = b.getClass();
 219             Method m = cl.getDeclaredMethod("compact");
 220             m.setAccessible(true);
 221             m.invoke(b);
 222         } catch (Exception e) {
 223             fail(e.getMessage(), b);
 224         }
 225     }
 226 
 227     private static void checkInvalidMarkException(final Buffer b) {
 228         tryCatch(b, InvalidMarkException.class, new Runnable() {
 229             public void run() {
 230                 b.mark();
 231                 compact(b);
 232                 b.reset();
 233             }});
 234     }
 235 
 236     private static void testViews(int level, ByteBuffer b, boolean direct) {
 237 
 238         ShortBuffer sb = b.asShortBuffer();
 239         BasicShort.test(level, sb, direct);
 240         checkBytes(b, new byte[] { 0, (byte)ic(0) });
 241         checkInvalidMarkException(sb);
 242 
 243         CharBuffer cb = b.asCharBuffer();
 244         BasicChar.test(level, cb, direct);
 245         checkBytes(b, new byte[] { 0, (byte)ic(0) });
 246         checkInvalidMarkException(cb);
 247 
 248         IntBuffer ib = b.asIntBuffer();
 249         BasicInt.test(level, ib, direct);
 250         checkBytes(b, new byte[] { 0, 0, 0, (byte)ic(0) });
 251         checkInvalidMarkException(ib);
 252 
 253         LongBuffer lb = b.asLongBuffer();
 254         BasicLong.test(level, lb, direct);
 255         checkBytes(b, new byte[] { 0, 0, 0, 0, 0, 0, 0, (byte)ic(0) });
 256         checkInvalidMarkException(lb);
 257 
 258         FloatBuffer fb = b.asFloatBuffer();
 259         BasicFloat.test(level, fb, direct);
 260         checkBytes(b, new byte[] { 0x42, (byte)0xc2, 0, 0 });
 261         checkInvalidMarkException(fb);
 262 
 263         DoubleBuffer db = b.asDoubleBuffer();
 264         BasicDouble.test(level, db, direct);
 265         checkBytes(b, new byte[] { 0x40, 0x58, 0x40, 0, 0, 0, 0, 0 });
 266         checkInvalidMarkException(db);
 267     }
 268 
 269     private static void testHet(int level, ByteBuffer b) {
 270 
 271         int p = b.position();
 272         b.limit(b.capacity());
 273         show(level, b);
 274         out.print("    put:");
 275 
 276         b.putChar((char)1);
 277         b.putChar((char)Character.MAX_VALUE);
 278         out.print(" char");
 279 
 280         b.putShort((short)1);
 281         b.putShort((short)Short.MAX_VALUE);
 282         out.print(" short");
 283 
 284         b.putInt(1);
 285         b.putInt(Integer.MAX_VALUE);
 286         out.print(" int");
 287 
 288         b.putLong((long)1);
 289         b.putLong((long)Long.MAX_VALUE);
 290         out.print(" long");
 291 
 292         b.putFloat((float)1);
 293         b.putFloat((float)Float.MIN_VALUE);
 294         b.putFloat((float)Float.MAX_VALUE);
 295         out.print(" float");
 296 
 297         b.putDouble((double)1);
 298         b.putDouble((double)Double.MIN_VALUE);
 299         b.putDouble((double)Double.MAX_VALUE);
 300         out.print(" double");
 301 
 302         out.println();
 303         b.limit(b.position());
 304         b.position(p);
 305         show(level, b);
 306         out.print("    get:");
 307 
 308         ck(b, b.getChar(), 1);
 309         ck(b, b.getChar(), Character.MAX_VALUE);
 310         out.print(" char");
 311 
 312         ck(b, b.getShort(), 1);
 313         ck(b, b.getShort(), Short.MAX_VALUE);
 314         out.print(" short");
 315 
 316         ck(b, b.getInt(), 1);
 317         ck(b, b.getInt(), Integer.MAX_VALUE);
 318         out.print(" int");
 319 
 320         ck(b, b.getLong(), 1);
 321         ck(b, b.getLong(), Long.MAX_VALUE);
 322         out.print(" long");
 323 
 324         ck(b, (long)b.getFloat(), 1);
 325         ck(b, (long)b.getFloat(), (long)Float.MIN_VALUE);
 326         ck(b, (long)b.getFloat(), (long)Float.MAX_VALUE);
 327         out.print(" float");
 328 
 329         ck(b, (long)b.getDouble(), 1);
 330         ck(b, (long)b.getDouble(), (long)Double.MIN_VALUE);
 331         ck(b, (long)b.getDouble(), (long)Double.MAX_VALUE);
 332         out.print(" double");
 333 
 334         out.println();
 335 
 336     }
 337 
 338 
 339 
 340     private static void fail(String problem,
 341                              ByteBuffer xb, ByteBuffer yb,
 342                              byte x, byte y) {
 343         fail(problem + String.format(": x=%s y=%s", x, y), xb, yb);
 344     }
 345 
 346     private static void tryCatch(Buffer b, Class<?> ex, Runnable thunk) {
 347         boolean caught = false;
 348         try {
 349             thunk.run();
 350         } catch (Throwable x) {
 351             if (ex.isAssignableFrom(x.getClass())) {
 352                 caught = true;
 353             } else {
 354                 fail(x.getMessage() + " not expected");
 355             }
 356         }
 357         if (!caught)
 358             fail(ex.getName() + " not thrown", b);
 359     }
 360 
 361     private static void tryCatch(byte [] t, Class<?> ex, Runnable thunk) {
 362         tryCatch(ByteBuffer.wrap(t), ex, thunk);
 363     }
 364 
 365     public static void test(int level, final ByteBuffer b, boolean direct) {
 366 
 367         show(level, b);
 368 
 369         if (direct != b.isDirect())
 370             fail("Wrong direction", b);
 371 
 372         // Gets and puts
 373 
 374         relPut(b);
 375         relGet(b);
 376         absGet(b);
 377         bulkGet(b);
 378 
 379         absPut(b);
 380         relGet(b);
 381         absGet(b);
 382         bulkGet(b);
 383 
 384         bulkPutArray(b);
 385         relGet(b);
 386 
 387         bulkPutBuffer(b);
 388         relGet(b);
 389 
 390 
 391 
 392 
 393 
 394 
 395 
 396 
 397 
 398 
 399 
 400 
 401 
 402 
 403 
 404 
 405 
 406 
 407 
 408 
 409 
 410 
 411 
 412 
 413 
 414 
 415 
 416 
 417 
 418 
 419 
 420 
 421 
 422 
 423 
 424 
 425 
 426 
 427 
 428         // Compact
 429 
 430         relPut(b);
 431         b.position(13);
 432         b.compact();
 433         b.flip();
 434         relGet(b, 13);
 435 
 436         // Exceptions
 437 
 438         relPut(b);
 439         b.limit(b.capacity() / 2);
 440         b.position(b.limit());
 441 
 442         tryCatch(b, BufferUnderflowException.class, new Runnable() {
 443                 public void run() {
 444                     b.get();
 445                 }});
 446 
 447         tryCatch(b, BufferOverflowException.class, new Runnable() {
 448                 public void run() {
 449                     b.put((byte)42);
 450                 }});
 451 
 452         // The index must be non-negative and lesss than the buffer's limit.
 453         tryCatch(b, IndexOutOfBoundsException.class, new Runnable() {
 454                 public void run() {
 455                     b.get(b.limit());
 456                 }});
 457         tryCatch(b, IndexOutOfBoundsException.class, new Runnable() {
 458                 public void run() {
 459                     b.get(-1);
 460                 }});
 461 
 462         tryCatch(b, IndexOutOfBoundsException.class, new Runnable() {
 463                 public void run() {
 464                     b.put(b.limit(), (byte)42);
 465                 }});
 466 
 467         tryCatch(b, InvalidMarkException.class, new Runnable() {
 468                 public void run() {
 469                     b.position(0);
 470                     b.mark();
 471                     b.compact();
 472                     b.reset();
 473                 }});
 474 
 475         try {
 476             b.position(b.limit() + 1);
 477             fail("IllegalArgumentException expected for setting position beyond limit");
 478         } catch (IllegalArgumentException e) {
 479             if (e.getMessage() == null) {
 480                 fail("Non-null IllegalArgumentException message expected for setting position beyond limit");
 481             }
 482         }
 483 
 484         try {
 485             b.position(-1);
 486             fail("IllegalArgumentException expected for setting negative position");
 487         } catch (IllegalArgumentException e) {
 488             if (e.getMessage() == null) {
 489                 fail("Non-null IllegalArgumentException message expected for setting negative position");
 490             }
 491         }
 492 
 493         try {
 494             b.limit(b.capacity() + 1);
 495             fail("IllegalArgumentException expected for setting limit beyond capacity");
 496         } catch (IllegalArgumentException e) {
 497             if (e.getMessage() == null) {
 498                 fail("Non-null IllegalArgumentException message expected for setting limit beyond capacity");
 499             }
 500         }
 501 
 502         try {
 503             b.limit(-1);
 504             fail("IllegalArgumentException expected for setting negative limit");
 505         } catch (IllegalArgumentException e) {
 506             if (e.getMessage() == null) {
 507                 fail("Non-null IllegalArgumentException message expected for setting negative limit");
 508             }
 509         }
 510 
 511         // Values
 512 
 513         b.clear();
 514         b.put((byte)0);
 515         b.put((byte)-1);
 516         b.put((byte)1);
 517         b.put(Byte.MAX_VALUE);
 518         b.put(Byte.MIN_VALUE);
 519 
 520 
 521 
 522 
 523 
 524 
 525 
 526 
 527 
 528 
 529 
 530 
 531 
 532 
 533 
 534 
 535 
 536         byte v;
 537         b.flip();
 538         ck(b, b.get(), 0);
 539         ck(b, b.get(), (byte)-1);
 540         ck(b, b.get(), 1);
 541         ck(b, b.get(), Byte.MAX_VALUE);
 542         ck(b, b.get(), Byte.MIN_VALUE);
 543 
 544 
 545 
 546 
 547 
 548 
 549 
 550 
 551 
 552 
 553 
 554 
 555 
 556 
 557 
 558 
 559 
 560 
 561 
 562 
 563 
 564 
 565         // Comparison
 566         b.rewind();
 567         ByteBuffer b2 = ByteBuffer.allocate(b.capacity());
 568         b2.put(b);
 569         b2.flip();
 570         b.position(2);
 571         b2.position(2);
 572         if (!b.equals(b2)) {
 573             for (int i = 2; i < b.limit(); i++) {
 574                 byte x = b.get(i);
 575                 byte y = b2.get(i);
 576                 if (x != y
 577 
 578 
 579 
 580 
 581 
 582 
 583                     )
 584                     out.println("[" + i + "] " + x + " != " + y);
 585             }
 586             fail("Identical buffers not equal", b, b2);
 587         }
 588         if (b.compareTo(b2) != 0)
 589             fail("Comparison to identical buffer != 0", b, b2);
 590 
 591         b.limit(b.limit() + 1);
 592         b.position(b.limit() - 1);
 593         b.put((byte)99);
 594         b.rewind();
 595         b2.rewind();
 596         if (b.equals(b2))
 597             fail("Non-identical buffers equal", b, b2);
 598         if (b.compareTo(b2) <= 0)
 599             fail("Comparison to shorter buffer <= 0", b, b2);
 600         b.limit(b.limit() - 1);
 601 
 602         b.put(2, (byte)42);
 603         if (b.equals(b2))
 604             fail("Non-identical buffers equal", b, b2);
 605         if (b.compareTo(b2) <= 0)
 606             fail("Comparison to lesser buffer <= 0", b, b2);
 607 
 608         // Check equals and compareTo with interesting values
 609         for (byte x : VALUES) {
 610             ByteBuffer xb = ByteBuffer.wrap(new byte[] { x });
 611             if (xb.compareTo(xb) != 0) {
 612                 fail("compareTo not reflexive", xb, xb, x, x);
 613             }
 614             if (! xb.equals(xb)) {
 615                 fail("equals not reflexive", xb, xb, x, x);
 616             }
 617             for (byte y : VALUES) {
 618                 ByteBuffer yb = ByteBuffer.wrap(new byte[] { y });
 619                 if (xb.compareTo(yb) != - yb.compareTo(xb)) {
 620                     fail("compareTo not anti-symmetric",
 621                          xb, yb, x, y);
 622                 }
 623                 if ((xb.compareTo(yb) == 0) != xb.equals(yb)) {
 624                     fail("compareTo inconsistent with equals",
 625                          xb, yb, x, y);
 626                 }
 627                 if (xb.compareTo(yb) != Byte.compare(x, y)) {
 628 
 629 
 630 
 631 
 632 
 633 
 634                     fail("Incorrect results for ByteBuffer.compareTo",
 635                          xb, yb, x, y);
 636                 }
 637                 if (xb.equals(yb) != ((x == y) || ((x != x) && (y != y)))) {
 638                     fail("Incorrect results for ByteBuffer.equals",
 639                          xb, yb, x, y);
 640                 }
 641             }
 642         }
 643 
 644         // Sub, dup
 645 
 646         relPut(b);
 647         relGet(b.duplicate());
 648         b.position(13);
 649         relGet(b.duplicate(), 13);
 650         relGet(b.duplicate().slice(), 13);
 651         relGet(b.slice(), 13);
 652         relGet(b.slice().duplicate(), 13);
 653 
 654         // Slice
 655 
 656         b.position(5);
 657         ByteBuffer sb = b.slice();
 658         checkSlice(b, sb);
 659         b.position(0);
 660         ByteBuffer sb2 = sb.slice();
 661         checkSlice(sb, sb2);
 662 
 663         if (!sb.equals(sb2))
 664             fail("Sliced slices do not match", sb, sb2);
 665         if ((sb.hasArray()) && (sb.arrayOffset() != sb2.arrayOffset()))
 666             fail("Array offsets do not match: "
 667                  + sb.arrayOffset() + " != " + sb2.arrayOffset(), sb, sb2);
 668 
 669 
 670 
 671         // Views
 672 
 673         b.clear();
 674         b.order(ByteOrder.BIG_ENDIAN);
 675         testViews(level + 1, b, direct);
 676 
 677         for (int i = 1; i <= 9; i++) {
 678             b.position(i);
 679             show(level + 1, b);
 680             testViews(level + 2, b, direct);
 681         }
 682 
 683         b.position(0);
 684         b.order(ByteOrder.LITTLE_ENDIAN);
 685         testViews(level + 1, b, direct);
 686 
 687         // Heterogeneous accessors
 688 
 689         b.order(ByteOrder.BIG_ENDIAN);
 690         for (int i = 0; i <= 9; i++) {
 691             b.position(i);
 692             testHet(level + 1, b);
 693         }
 694         b.order(ByteOrder.LITTLE_ENDIAN);
 695         b.position(3);
 696         testHet(level + 1, b);
 697 
 698 
 699 
 700         // Read-only views
 701 
 702         b.rewind();
 703         final ByteBuffer rb = b.asReadOnlyBuffer();
 704         if (!b.equals(rb))
 705             fail("Buffer not equal to read-only view", b, rb);
 706         show(level + 1, rb);
 707 
 708         tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
 709                 public void run() {
 710                     relPut(rb);
 711                 }});
 712 
 713         tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
 714                 public void run() {
 715                     absPut(rb);
 716                 }});
 717 
 718         tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
 719                 public void run() {
 720                     bulkPutArray(rb);
 721                 }});
 722 
 723         tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
 724                 public void run() {
 725                     bulkPutBuffer(rb);
 726                 }});
 727 
 728         // put(ByteBuffer) should not change source position
 729         final ByteBuffer src = ByteBuffer.allocate(1);
 730         tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
 731                 public void run() {
 732                     rb.put(src);
 733                  }});
 734         ck(src, src.position(), 0);
 735 
 736         tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
 737                 public void run() {
 738                     rb.compact();
 739                 }});
 740 
 741 
 742 
 743         tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
 744                 public void run() {
 745                     rb.putChar((char)1);
 746                 }});
 747         tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
 748                 public void run() {
 749                     rb.putChar(0, (char)1);
 750                 }});
 751 
 752         tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
 753                 public void run() {
 754                     rb.putShort((short)1);
 755                 }});
 756         tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
 757                 public void run() {
 758                     rb.putShort(0, (short)1);
 759                 }});
 760 
 761         tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
 762                 public void run() {
 763                     rb.putInt(1);
 764                 }});
 765         tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
 766                 public void run() {
 767                     rb.putInt(0, 1);
 768                 }});
 769 
 770         tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
 771                 public void run() {
 772                     rb.putLong((long)1);
 773                 }});
 774         tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
 775                 public void run() {
 776                     rb.putLong(0, (long)1);
 777                 }});
 778 
 779         tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
 780                 public void run() {
 781                     rb.putFloat((float)1);
 782                 }});
 783         tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
 784                 public void run() {
 785                     rb.putFloat(0, (float)1);
 786                 }});
 787 
 788         tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
 789                 public void run() {
 790                     rb.putDouble((double)1);
 791                 }});
 792         tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
 793                 public void run() {
 794                     rb.putDouble(0, (double)1);
 795                 }});
 796 
 797 
 798 
 799 
 800 
 801 
 802 
 803 
 804 
 805 
 806 
 807 
 808 
 809 
 810 
 811 
 812 
 813 
 814 
 815         if (rb.getClass().getName().startsWith("java.nio.Heap")) {
 816 
 817             tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
 818                     public void run() {
 819                         rb.array();
 820                     }});
 821 
 822             tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
 823                     public void run() {
 824                         rb.arrayOffset();
 825                     }});
 826 
 827             if (rb.hasArray())
 828                 fail("Read-only heap buffer's backing array is accessible",
 829                      rb);
 830 
 831         }
 832 
 833         // Bulk puts from read-only buffers
 834 
 835         b.clear();
 836         rb.rewind();
 837         b.put(rb);
 838 
 839 
 840         // For byte buffers, test both the direct and non-direct cases
 841         ByteBuffer ob
 842             = (b.isDirect()
 843                ? ByteBuffer.allocate(rb.capacity())
 844                : ByteBuffer.allocateDirect(rb.capacity()));
 845         rb.rewind();
 846         ob.put(rb);
 847 
 848 
 849         relPut(b);                       // Required by testViews
 850 
 851     }
 852 
 853 
 854 
 855 
 856 
 857 
 858 
 859 
 860 
 861 
 862 
 863 
 864 
 865 
 866 
 867 
 868 
 869 
 870 
 871 
 872 
 873 
 874 
 875 
 876 
 877 
 878 
 879 
 880 
 881 
 882 
 883 
 884 
 885 
 886 
 887 
 888 
 889 
 890 
 891 
 892 
 893 
 894 
 895 
 896 
 897 
 898 
 899 
 900 
 901 
 902 
 903 
 904 
 905 
 906 
 907 
 908 
 909 
 910 
 911 
 912 
 913 
 914 
 915 
 916 
 917 
 918 
 919 
 920 
 921 
 922 
 923 
 924 
 925 
 926 
 927 
 928 
 929 
 930 
 931 
 932 
 933 
 934 
 935 
 936 
 937     public static void test(final byte [] ba) {
 938         int offset = 47;
 939         int length = 900;
 940         final ByteBuffer b = ByteBuffer.wrap(ba, offset, length);
 941         show(0, b);
 942         ck(b, b.capacity(), ba.length);
 943         ck(b, b.position(), offset);
 944         ck(b, b.limit(), offset + length);
 945 
 946         // The offset must be non-negative and no larger than <array.length>.
 947         tryCatch(ba, IndexOutOfBoundsException.class, new Runnable() {
 948                 public void run() {
 949                     ByteBuffer.wrap(ba, -1, ba.length);
 950                 }});
 951         tryCatch(ba, IndexOutOfBoundsException.class, new Runnable() {
 952                 public void run() {
 953                     ByteBuffer.wrap(ba, ba.length + 1, ba.length);
 954                 }});
 955         tryCatch(ba, IndexOutOfBoundsException.class, new Runnable() {
 956                 public void run() {
 957                     ByteBuffer.wrap(ba, 0, -1);
 958                 }});
 959         tryCatch(ba, IndexOutOfBoundsException.class, new Runnable() {
 960                 public void run() {
 961                     ByteBuffer.wrap(ba, 0, ba.length + 1);
 962                 }});
 963 
 964         // A NullPointerException will be thrown if the array is null.
 965         tryCatch(ba, NullPointerException.class, new Runnable() {
 966                 public void run() {
 967                     ByteBuffer.wrap((byte []) null, 0, 5);
 968                 }});
 969         tryCatch(ba, NullPointerException.class, new Runnable() {
 970                 public void run() {
 971                     ByteBuffer.wrap((byte []) null);
 972                 }});
 973     }
 974 
 975     private static void testAllocate() {
 976         // An IllegalArgumentException will be thrown for negative capacities.
 977         tryCatch((Buffer) null, IllegalArgumentException.class, new Runnable() {
 978                 public void run() {
 979                     ByteBuffer.allocate(-1);
 980                 }});
 981         try {
 982             ByteBuffer.allocate(-1);
 983         } catch (IllegalArgumentException e) {
 984             if (e.getMessage() == null) {
 985                 fail("Non-null IllegalArgumentException message expected attempt to allocate negative capacity buffer");
 986             }
 987         }
 988 
 989         tryCatch((Buffer) null, IllegalArgumentException.class, new Runnable() {
 990                 public void run() {
 991                     ByteBuffer.allocateDirect(-1);
 992                 }});
 993         try {
 994             ByteBuffer.allocateDirect(-1);
 995         } catch (IllegalArgumentException e) {
 996             if (e.getMessage() == null) {
 997                 fail("Non-null IllegalArgumentException message expected attempt to allocate negative capacity direct buffer");
 998             }
 999         }
1000 
1001     }
1002 
1003     public static void test() {
1004         testAllocate();
1005         test(0, ByteBuffer.allocate(7 * 1024), false);
1006         test(0, ByteBuffer.wrap(new byte[7 * 1024], 0, 7 * 1024), false);
1007         test(new byte[1024]);
1008 
1009         ByteBuffer b = ByteBuffer.allocateDirect(7 * 1024);
1010         for (b.position(0); b.position() < b.limit(); )
1011             ck(b, b.get(), 0);
1012         test(0, b, true);
1013 
1014 
1015 
1016 
1017 
1018         callReset(ByteBuffer.allocate(10));
1019 
1020 
1021 
1022 
1023 
1024     }
1025 
1026 }