< prev index next >

test/jdk/java/nio/Buffer/BasicByte.java

Print this page


   1 /*
   2  * Copyright (c) 2000, 2016, 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  */


  70             ck(b, (long)b.get(), (long)((byte)ic(i)));
  71         b.rewind();
  72     }
  73 
  74     private static void absGet(ByteBuffer b) {
  75         int n = b.capacity();
  76         for (int i = 0; i < n; i++)
  77             ck(b, (long)b.get(), (long)((byte)ic(i)));
  78         b.rewind();
  79     }
  80 
  81     private static void bulkGet(ByteBuffer b) {
  82         int n = b.capacity();
  83         byte[] a = new byte[n + 7];
  84         b.get(a, 7, n);
  85         for (int i = 0; i < n; i++) {
  86             ck(b, (long)a[i + 7], (long)((byte)ic(i)));
  87         }
  88     }
  89 












  90     private static void relPut(ByteBuffer b) {
  91         int n = b.capacity();
  92         b.clear();
  93         for (int i = 0; i < n; i++)
  94             b.put((byte)ic(i));
  95         b.flip();
  96     }
  97 
  98     private static void absPut(ByteBuffer b) {
  99         int n = b.capacity();
 100         b.clear();
 101         for (int i = 0; i < n; i++)
 102             b.put(i, (byte)ic(i));
 103         b.limit(n);
 104         b.position(0);
 105     }
 106 
 107     private static void bulkPutArray(ByteBuffer b) {
 108         int n = b.capacity();
 109         b.clear();


 119         b.clear();
 120         ByteBuffer c = ByteBuffer.allocate(n + 7);
 121         c.position(7);
 122         for (int i = 0; i < n; i++)
 123             c.put((byte)ic(i));
 124         c.flip();
 125         c.position(7);
 126         b.put(c);
 127         b.flip();
 128         try {
 129             b.put(b);
 130             fail("IllegalArgumentException expected for put into same buffer");
 131         } catch (IllegalArgumentException e) {
 132             if (e.getMessage() == null) {
 133                 fail("Non-null IllegalArgumentException message expected from"
 134                      + " put into same buffer");
 135             }
 136         }
 137     }
 138 









































 139     //6231529
 140     private static void callReset(ByteBuffer b) {
 141         b.position(0);
 142         b.mark();
 143 
 144         b.duplicate().reset();
 145         b.asReadOnlyBuffer().reset();
 146     }
 147 
 148 
 149 
 150 
 151 
 152 
 153 
 154 
 155 
 156 
 157 
 158 


 435                 // Round up position
 436                 p = (p_mod > 0) ? p + (us - p_mod) : p;
 437                 // Round down limit
 438                 l = l - l_mod;
 439 
 440                 int ec = l - p;
 441                 if (as.limit() != ec) {
 442                     fail("Buffer capacity incorrect, expected: " + ec, as);
 443                 }
 444             }
 445         }
 446     }
 447 
 448 
 449     private static void fail(String problem,
 450                              ByteBuffer xb, ByteBuffer yb,
 451                              byte x, byte y) {
 452         fail(problem + String.format(": x=%s y=%s", x, y), xb, yb);
 453     }
 454 




 455     private static void catchIllegalArgument(Buffer b, Runnable thunk) {
 456         tryCatch(b, IllegalArgumentException.class, thunk);
 457     }
 458 
 459     private static void catchReadOnlyBuffer(Buffer b, Runnable thunk) {
 460         tryCatch(b, ReadOnlyBufferException.class, thunk);
 461     }
 462 
 463     private static void catchIndexOutOfBounds(Buffer b, Runnable thunk) {
 464         tryCatch(b, IndexOutOfBoundsException.class, thunk);
 465     }
 466 
 467     private static void catchIndexOutOfBounds(byte[] t, Runnable thunk) {
 468         tryCatch(t, IndexOutOfBoundsException.class, thunk);
 469     }
 470 
 471     private static void tryCatch(Buffer b, Class<?> ex, Runnable thunk) {
 472         boolean caught = false;
 473         try {
 474             thunk.run();
 475         } catch (Throwable x) {
 476             if (ex.isAssignableFrom(x.getClass())) {
 477                 caught = true;
 478             } else {
 479                 fail(x.getMessage() + " not expected");



 480             }
 481         }
 482         if (!caught) {
 483             fail(ex.getName() + " not thrown", b);
 484         }
 485     }
 486 
 487     private static void tryCatch(byte[] t, Class<?> ex, Runnable thunk) {
 488         tryCatch(ByteBuffer.wrap(t), ex, thunk);
 489     }
 490 
 491     public static void test(int level, final ByteBuffer b, boolean direct) {
 492 
 493         show(level, b);
 494 
 495         if (direct != b.isDirect())
 496             fail("Wrong direction", b);
 497 
 498         // Gets and puts
 499 
 500         relPut(b);
 501         relGet(b);
 502         absGet(b);
 503         bulkGet(b);
 504 
 505         absPut(b);
 506         relGet(b);
 507         absGet(b);
 508         bulkGet(b);
 509 
 510         bulkPutArray(b);
 511         relGet(b);
 512 
 513         bulkPutBuffer(b);
 514         relGet(b);
 515 









 516 
 517 
 518 
 519 
 520 
 521 
 522 
 523 
 524 
 525 
 526 
 527 
 528 
 529 
 530 
 531 
 532 
 533 
 534 
 535 


 595         try {
 596             b.limit(b.capacity() + 1);
 597             fail("IllegalArgumentException expected for limit beyond capacity");
 598         } catch (IllegalArgumentException e) {
 599             if (e.getMessage() == null) {
 600                 fail("Non-null IllegalArgumentException message expected for"
 601                      + " limit beyond capacity");
 602             }
 603         }
 604 
 605         try {
 606             b.limit(-1);
 607             fail("IllegalArgumentException expected for negative limit");
 608         } catch (IllegalArgumentException e) {
 609             if (e.getMessage() == null) {
 610                 fail("Non-null IllegalArgumentException message expected for"
 611                      + " negative limit");
 612             }
 613         }
 614 































 615         // Values
 616 
 617         b.clear();
 618         b.put((byte)0);
 619         b.put((byte)-1);
 620         b.put((byte)1);
 621         b.put(Byte.MAX_VALUE);
 622         b.put(Byte.MIN_VALUE);
 623 
 624 
 625 
 626 
 627 
 628 
 629 
 630 
 631 
 632 
 633 
 634 


 802             testHet(level + 1, b);
 803         }
 804         b.order(ByteOrder.LITTLE_ENDIAN);
 805         b.position(3);
 806         testHet(level + 1, b);
 807 
 808 
 809 
 810         // Read-only views
 811 
 812         b.rewind();
 813         final ByteBuffer rb = b.asReadOnlyBuffer();
 814         if (!b.equals(rb))
 815             fail("Buffer not equal to read-only view", b, rb);
 816         show(level + 1, rb);
 817 
 818         catchReadOnlyBuffer(b, () -> relPut(rb));
 819         catchReadOnlyBuffer(b, () -> absPut(rb));
 820         catchReadOnlyBuffer(b, () -> bulkPutArray(rb));
 821         catchReadOnlyBuffer(b, () -> bulkPutBuffer(rb));


 822 
 823         // put(ByteBuffer) should not change source position
 824         final ByteBuffer src = ByteBuffer.allocate(1);
 825         catchReadOnlyBuffer(b, () -> rb.put(src));
 826         ck(src, src.position(), 0);
 827 
 828         catchReadOnlyBuffer(b, () -> rb.compact());
 829 
 830 
 831 
 832         catchReadOnlyBuffer(b, () -> rb.putChar((char)1));
 833         catchReadOnlyBuffer(b, () -> rb.putChar(0, (char)1));
 834         catchReadOnlyBuffer(b, () -> rb.putShort((short)1));
 835         catchReadOnlyBuffer(b, () -> rb.putShort(0, (short)1));
 836         catchReadOnlyBuffer(b, () -> rb.putInt(1));
 837         catchReadOnlyBuffer(b, () -> rb.putInt(0, 1));
 838         catchReadOnlyBuffer(b, () -> rb.putLong((long)1));
 839         catchReadOnlyBuffer(b, () -> rb.putLong(0, (long)1));
 840         catchReadOnlyBuffer(b, () -> rb.putFloat((float)1));
 841         catchReadOnlyBuffer(b, () -> rb.putFloat(0, (float)1));


   1 /*
   2  * Copyright (c) 2000, 2019, 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  */


  70             ck(b, (long)b.get(), (long)((byte)ic(i)));
  71         b.rewind();
  72     }
  73 
  74     private static void absGet(ByteBuffer b) {
  75         int n = b.capacity();
  76         for (int i = 0; i < n; i++)
  77             ck(b, (long)b.get(), (long)((byte)ic(i)));
  78         b.rewind();
  79     }
  80 
  81     private static void bulkGet(ByteBuffer b) {
  82         int n = b.capacity();
  83         byte[] a = new byte[n + 7];
  84         b.get(a, 7, n);
  85         for (int i = 0; i < n; i++) {
  86             ck(b, (long)a[i + 7], (long)((byte)ic(i)));
  87         }
  88     }
  89 
  90     private static void absBulkGet(ByteBuffer b) {
  91         int n = b.capacity();
  92         int len = n - 7*2;
  93         byte[] a = new byte[n + 7];
  94         b.position(42);
  95         b.get(7, a, 7, len);
  96         ck(b, b.position() == 42);
  97         for (int i = 0; i < len; i++) {
  98             ck(b, (long)a[i + 7], (long)((byte)ic(i)));
  99         }
 100     }
 101 
 102     private static void relPut(ByteBuffer b) {
 103         int n = b.capacity();
 104         b.clear();
 105         for (int i = 0; i < n; i++)
 106             b.put((byte)ic(i));
 107         b.flip();
 108     }
 109 
 110     private static void absPut(ByteBuffer b) {
 111         int n = b.capacity();
 112         b.clear();
 113         for (int i = 0; i < n; i++)
 114             b.put(i, (byte)ic(i));
 115         b.limit(n);
 116         b.position(0);
 117     }
 118 
 119     private static void bulkPutArray(ByteBuffer b) {
 120         int n = b.capacity();
 121         b.clear();


 131         b.clear();
 132         ByteBuffer c = ByteBuffer.allocate(n + 7);
 133         c.position(7);
 134         for (int i = 0; i < n; i++)
 135             c.put((byte)ic(i));
 136         c.flip();
 137         c.position(7);
 138         b.put(c);
 139         b.flip();
 140         try {
 141             b.put(b);
 142             fail("IllegalArgumentException expected for put into same buffer");
 143         } catch (IllegalArgumentException e) {
 144             if (e.getMessage() == null) {
 145                 fail("Non-null IllegalArgumentException message expected from"
 146                      + " put into same buffer");
 147             }
 148         }
 149     }
 150 
 151     private static void absBulkPutArray(ByteBuffer b) {
 152         int n = b.capacity();
 153         b.clear();
 154         int lim = n - 7;
 155         int len = lim - 7;
 156         b.limit(lim);
 157         byte[] a = new byte[len + 7];
 158         for (int i = 0; i < len; i++)
 159             a[i + 7] = (byte)ic(i);
 160         b.position(42);
 161         b.put(7, a, 7, len);
 162         ck(b, b.position() == 42);
 163     }
 164 
 165     private static void absBulkPutBuffer(ByteBuffer b, boolean direct) {
 166         int n = b.capacity();
 167         b.clear();
 168         int lim = n - 7;
 169         int len = lim - 7;
 170         b.limit(lim);
 171         ByteBuffer c = direct ?
 172 
 173             ByteBuffer.allocateDirect(len + 7)
 174 
 175 
 176 
 177 
 178             : ByteBuffer.allocate(len + 7);
 179 
 180 
 181 
 182 
 183         for (int i = 0; i < len; i++)
 184             c.put(i + 7, (byte)ic(i));
 185         b.position(42);
 186         c.position(42);
 187         b.put(7, c, 7, len);
 188         ck(b, b.position() == 42);
 189         ck(c, c.position() == 42);
 190     }
 191 
 192     //6231529
 193     private static void callReset(ByteBuffer b) {
 194         b.position(0);
 195         b.mark();
 196 
 197         b.duplicate().reset();
 198         b.asReadOnlyBuffer().reset();
 199     }
 200 
 201 
 202 
 203 
 204 
 205 
 206 
 207 
 208 
 209 
 210 
 211 


 488                 // Round up position
 489                 p = (p_mod > 0) ? p + (us - p_mod) : p;
 490                 // Round down limit
 491                 l = l - l_mod;
 492 
 493                 int ec = l - p;
 494                 if (as.limit() != ec) {
 495                     fail("Buffer capacity incorrect, expected: " + ec, as);
 496                 }
 497             }
 498         }
 499     }
 500 
 501 
 502     private static void fail(String problem,
 503                              ByteBuffer xb, ByteBuffer yb,
 504                              byte x, byte y) {
 505         fail(problem + String.format(": x=%s y=%s", x, y), xb, yb);
 506     }
 507 
 508     private static void catchNullArgument(Buffer b, Runnable thunk) {
 509         tryCatch(b, NullPointerException.class, thunk);
 510     }
 511 
 512     private static void catchIllegalArgument(Buffer b, Runnable thunk) {
 513         tryCatch(b, IllegalArgumentException.class, thunk);
 514     }
 515 
 516     private static void catchReadOnlyBuffer(Buffer b, Runnable thunk) {
 517         tryCatch(b, ReadOnlyBufferException.class, thunk);
 518     }
 519 
 520     private static void catchIndexOutOfBounds(Buffer b, Runnable thunk) {
 521         tryCatch(b, IndexOutOfBoundsException.class, thunk);
 522     }
 523 
 524     private static void catchIndexOutOfBounds(byte[] t, Runnable thunk) {
 525         tryCatch(t, IndexOutOfBoundsException.class, thunk);
 526     }
 527 
 528     private static void tryCatch(Buffer b, Class<?> ex, Runnable thunk) {
 529         boolean caught = false;
 530         try {
 531             thunk.run();
 532         } catch (Throwable x) {
 533             if (ex.isAssignableFrom(x.getClass())) {
 534                 caught = true;
 535             } else {
 536                 String s = x.getMessage();
 537                 if (s == null)
 538                     s = x.getClass().getName();
 539                 fail(s + " not expected");
 540             }
 541         }
 542         if (!caught) {
 543             fail(ex.getName() + " not thrown", b);
 544         }
 545     }
 546 
 547     private static void tryCatch(byte[] t, Class<?> ex, Runnable thunk) {
 548         tryCatch(ByteBuffer.wrap(t), ex, thunk);
 549     }
 550 
 551     public static void test(int level, final ByteBuffer b, boolean direct) {
 552 
 553         show(level, b);
 554 
 555         if (direct != b.isDirect())
 556             fail("Wrong direction", b);
 557 
 558         // Gets and puts
 559 
 560         relPut(b);
 561         relGet(b);
 562         absGet(b);
 563         bulkGet(b);
 564 
 565         absPut(b);
 566         relGet(b);
 567         absGet(b);
 568         bulkGet(b);
 569 
 570         bulkPutArray(b);
 571         relGet(b);
 572 
 573         bulkPutBuffer(b);
 574         relGet(b);
 575 
 576         absBulkPutArray(b);
 577         absBulkGet(b);
 578 
 579         absBulkPutBuffer(b, direct);
 580         absBulkGet(b);
 581 
 582         absBulkPutBuffer(b, !direct);
 583         absBulkGet(b);
 584 
 585 
 586 
 587 
 588 
 589 
 590 
 591 
 592 
 593 
 594 
 595 
 596 
 597 
 598 
 599 
 600 
 601 
 602 
 603 
 604 


 664         try {
 665             b.limit(b.capacity() + 1);
 666             fail("IllegalArgumentException expected for limit beyond capacity");
 667         } catch (IllegalArgumentException e) {
 668             if (e.getMessage() == null) {
 669                 fail("Non-null IllegalArgumentException message expected for"
 670                      + " limit beyond capacity");
 671             }
 672         }
 673 
 674         try {
 675             b.limit(-1);
 676             fail("IllegalArgumentException expected for negative limit");
 677         } catch (IllegalArgumentException e) {
 678             if (e.getMessage() == null) {
 679                 fail("Non-null IllegalArgumentException message expected for"
 680                      + " negative limit");
 681             }
 682         }
 683 
 684         // Exceptions in absolute bulk operations
 685 
 686         catchNullArgument(b, () -> b.get(7, null, 0, 42));
 687         catchNullArgument(b, () -> b.put(7, (byte[])null, 0, 42));
 688         catchNullArgument(b, () -> b.put(7, (ByteBuffer)null, 0, 42));
 689 
 690         byte[] tmpa = new byte[42];
 691         catchIndexOutOfBounds(b, () -> b.get(7, tmpa, -1, 42));
 692         catchIndexOutOfBounds(b, () -> b.get(7, tmpa, 42, 1));
 693         catchIndexOutOfBounds(b, () -> b.get(7, tmpa, 41, -1));
 694         catchIndexOutOfBounds(b, () -> b.get(-1, tmpa, 0, 1));
 695         catchIndexOutOfBounds(b, () -> b.get(b.limit(), tmpa, 0, 1));
 696         catchIndexOutOfBounds(b, () -> b.get(b.limit() - 41, tmpa, 0, 42));
 697 
 698         catchIndexOutOfBounds(b, () -> b.put(7, tmpa, -1, 42));
 699         catchIndexOutOfBounds(b, () -> b.put(7, tmpa, 42, 1));
 700         catchIndexOutOfBounds(b, () -> b.put(7, tmpa, 41, -1));
 701         catchIndexOutOfBounds(b, () -> b.put(-1, tmpa, 0, 1));
 702         catchIndexOutOfBounds(b, () -> b.put(b.limit(), tmpa, 0, 1));
 703         catchIndexOutOfBounds(b, () -> b.put(b.limit() - 41, tmpa, 0, 42));
 704 
 705         ByteBuffer tmpb = ByteBuffer.allocate(42);
 706         catchIndexOutOfBounds(b, () -> b.put(7, tmpb, -1, 42));
 707         catchIndexOutOfBounds(b, () -> b.put(7, tmpb, 42, 1));
 708         catchIndexOutOfBounds(b, () -> b.put(7, tmpb, 42, -1));
 709         catchIndexOutOfBounds(b, () -> b.put(7, tmpb, 41, 2));
 710         catchIndexOutOfBounds(b, () -> b.put(-1, tmpb, 0, 1));
 711         catchIndexOutOfBounds(b, () -> b.put(b.limit(), tmpb, 0, 1));
 712         catchIndexOutOfBounds(b, () -> b.put(0, tmpb, 0, -1));
 713         catchIndexOutOfBounds(b, () -> b.put(b.limit() - 41, tmpb, 0, 42));
 714 
 715         // Values
 716 
 717         b.clear();
 718         b.put((byte)0);
 719         b.put((byte)-1);
 720         b.put((byte)1);
 721         b.put(Byte.MAX_VALUE);
 722         b.put(Byte.MIN_VALUE);
 723 
 724 
 725 
 726 
 727 
 728 
 729 
 730 
 731 
 732 
 733 
 734 


 902             testHet(level + 1, b);
 903         }
 904         b.order(ByteOrder.LITTLE_ENDIAN);
 905         b.position(3);
 906         testHet(level + 1, b);
 907 
 908 
 909 
 910         // Read-only views
 911 
 912         b.rewind();
 913         final ByteBuffer rb = b.asReadOnlyBuffer();
 914         if (!b.equals(rb))
 915             fail("Buffer not equal to read-only view", b, rb);
 916         show(level + 1, rb);
 917 
 918         catchReadOnlyBuffer(b, () -> relPut(rb));
 919         catchReadOnlyBuffer(b, () -> absPut(rb));
 920         catchReadOnlyBuffer(b, () -> bulkPutArray(rb));
 921         catchReadOnlyBuffer(b, () -> bulkPutBuffer(rb));
 922         catchReadOnlyBuffer(b, () -> absBulkPutArray(rb));
 923         catchReadOnlyBuffer(b, () -> absBulkPutBuffer(rb, direct));
 924 
 925         // put(ByteBuffer) should not change source position
 926         final ByteBuffer src = ByteBuffer.allocate(1);
 927         catchReadOnlyBuffer(b, () -> rb.put(src));
 928         ck(src, src.position(), 0);
 929 
 930         catchReadOnlyBuffer(b, () -> rb.compact());
 931 
 932 
 933 
 934         catchReadOnlyBuffer(b, () -> rb.putChar((char)1));
 935         catchReadOnlyBuffer(b, () -> rb.putChar(0, (char)1));
 936         catchReadOnlyBuffer(b, () -> rb.putShort((short)1));
 937         catchReadOnlyBuffer(b, () -> rb.putShort(0, (short)1));
 938         catchReadOnlyBuffer(b, () -> rb.putInt(1));
 939         catchReadOnlyBuffer(b, () -> rb.putInt(0, 1));
 940         catchReadOnlyBuffer(b, () -> rb.putLong((long)1));
 941         catchReadOnlyBuffer(b, () -> rb.putLong(0, (long)1));
 942         catchReadOnlyBuffer(b, () -> rb.putFloat((float)1));
 943         catchReadOnlyBuffer(b, () -> rb.putFloat(0, (float)1));


< prev index next >