jdk/test/java/nio/Buffer/BasicByte.java
changeset 37913 3cc95b690353
parent 36933 3e6453e2d833
--- a/jdk/test/java/nio/Buffer/BasicByte.java	Mon May 16 07:01:26 2016 +0200
+++ b/jdk/test/java/nio/Buffer/BasicByte.java	Mon May 16 09:54:01 2016 +0100
@@ -31,7 +31,6 @@
 // -- This file was mechanically generated: Do not edit! -- //
 
 import java.nio.*;
-import java.lang.reflect.Method;
 
 
 public class BasicByte
@@ -60,7 +59,6 @@
 
     private static void relGet(ByteBuffer b) {
         int n = b.capacity();
-        byte v;
         for (int i = 0; i < n; i++)
             ck(b, (long)b.get(), (long)((byte)ic(i)));
         b.rewind();
@@ -68,7 +66,6 @@
 
     private static void relGet(ByteBuffer b, int start) {
         int n = b.remaining();
-        byte v;
         for (int i = start; i < n; i++)
             ck(b, (long)b.get(), (long)((byte)ic(i)));
         b.rewind();
@@ -76,7 +73,6 @@
 
     private static void absGet(ByteBuffer b) {
         int n = b.capacity();
-        byte v;
         for (int i = 0; i < n; i++)
             ck(b, (long)b.get(), (long)((byte)ic(i)));
         b.rewind();
@@ -86,8 +82,9 @@
         int n = b.capacity();
         byte[] a = new byte[n + 7];
         b.get(a, 7, n);
-        for (int i = 0; i < n; i++)
+        for (int i = 0; i < n; i++) {
             ck(b, (long)a[i + 7], (long)((byte)ic(i)));
+        }
     }
 
     private static void relPut(ByteBuffer b) {
@@ -203,13 +200,14 @@
     private static void checkBytes(ByteBuffer b, byte[] bs) {
         int n = bs.length;
         int p = b.position();
-        byte v;
         if (b.order() == ByteOrder.BIG_ENDIAN) {
-            for (int i = 0; i < n; i++)
+            for (int i = 0; i < n; i++) {
                 ck(b, b.get(), bs[i]);
+            }
         } else {
-            for (int i = n - 1; i >= 0; i--)
+            for (int i = n - 1; i >= 0; i--) {
                 ck(b, b.get(), bs[i]);
+            }
         }
         b.position(p);
     }
@@ -217,7 +215,7 @@
     private static void compact(Buffer b) {
         try {
             Class<?> cl = b.getClass();
-            Method m = cl.getDeclaredMethod("compact");
+            java.lang.reflect.Method m = cl.getDeclaredMethod("compact");
             m.setAccessible(true);
             m.invoke(b);
         } catch (Exception e) {
@@ -226,12 +224,11 @@
     }
 
     private static void checkInvalidMarkException(final Buffer b) {
-        tryCatch(b, InvalidMarkException.class, new Runnable() {
-            public void run() {
+        tryCatch(b, InvalidMarkException.class, () -> {
                 b.mark();
                 compact(b);
                 b.reset();
-            }});
+            });
     }
 
     private static void testViews(int level, ByteBuffer b, boolean direct) {
@@ -338,41 +335,50 @@
 
     private static void testAlign(final ByteBuffer b, boolean direct) {
         // index out-of bounds
-        tryCatch(b, IllegalArgumentException.class, () -> b.alignmentOffset(-1, (short) 1));
+        catchIllegalArgument(b, () -> b.alignmentOffset(-1, (short) 1));
 
         // unit size values
-        tryCatch(b, IllegalArgumentException.class, () -> b.alignmentOffset(0, (short) 0));
+        catchIllegalArgument(b, () -> b.alignmentOffset(0, (short) 0));
         for (int us = 1; us < 65; us++) {
             int _us = us;
             if ((us & (us - 1)) != 0) {
                 // unit size not a power of two
-                tryCatch(b, IllegalArgumentException.class, () -> b.alignmentOffset(0, _us));
+                catchIllegalArgument(b, () -> b.alignmentOffset(0, _us));
             } else {
                 if (direct || us <= 8) {
                     b.alignmentOffset(0, us);
                 } else {
                     // unit size > 8 with non-direct buffer
-                    tryCatch(b, UnsupportedOperationException.class, () -> b.alignmentOffset(0, _us));
+                    tryCatch(b, UnsupportedOperationException.class,
+                            () -> b.alignmentOffset(0, _us));
                 }
             }
         }
 
         // Probe for long misalignment at index zero for a newly created buffer
-        ByteBuffer empty = direct ? ByteBuffer.allocateDirect(0) : ByteBuffer.allocate(0);
+        ByteBuffer empty =
+                direct ? ByteBuffer.allocateDirect(0) : ByteBuffer.allocate(0);
         int longMisalignmentAtZero = empty.alignmentOffset(0, 8);
 
         if (direct) {
             // Freshly created direct byte buffers should be aligned at index 0
             // for ref and primitive values (see Unsafe.allocateMemory)
-            if (longMisalignmentAtZero != 0)
-                fail("Direct byte buffer misalligned at index 0 for ref and primitive values " + longMisalignmentAtZero);
+            if (longMisalignmentAtZero != 0) {
+                fail("Direct byte buffer misaligned at index 0"
+                        + " for ref and primitive values "
+                        + longMisalignmentAtZero);
+            }
         } else {
             // For heap byte buffers misalignment may occur on 32-bit systems
             // where Unsafe.ARRAY_BYTE_BASE_OFFSET % 8 == 4 and not 0
             // Note the GC will preserve alignment of the base address of the
             // array
-            if (jdk.internal.misc.Unsafe.ARRAY_BYTE_BASE_OFFSET % 8 != longMisalignmentAtZero)
-                fail("Heap byte buffer misalligned at index 0 for ref and primitive values " + longMisalignmentAtZero);
+            if (jdk.internal.misc.Unsafe.ARRAY_BYTE_BASE_OFFSET % 8
+                    != longMisalignmentAtZero) {
+                fail("Heap byte buffer misaligned at index 0"
+                        + " for ref and primitive values "
+                        + longMisalignmentAtZero);
+            }
         }
 
         // Ensure test buffer is correctly aligned at index 0
@@ -385,8 +391,10 @@
                 int am = b.alignmentOffset(i, us);
                 int expectedAm = (longMisalignmentAtZero + i) % us;
 
-                if (am != expectedAm)
-                    fail(String.format("b.alignmentOffset(%d, %d) == %d incorrect, expected %d", i, us, am, expectedAm));
+                if (am != expectedAm) {
+                    String f = "b.alignmentOffset(%d, %d) == %d incorrect, expected %d";
+                    fail(String.format(f, i, us, am, expectedAm));
+                }
             }
         }
 
@@ -395,8 +403,10 @@
         int al = b.limit() - b.alignmentOffset(b.limit(), 8);
         ByteBuffer ab = b.position(ap).limit(al).
                 slice();
-        if (ab.limit() == 0)
-            fail("Test input buffer not sufficiently sized to cover an aligned region for all values", b);
+        if (ab.limit() == 0) {
+            fail("Test input buffer not sufficiently sized to cover" +
+                    " an aligned region for all values", b);
+        }
         if (ab.alignmentOffset(0, 8) != 0)
             fail("Aligned test input buffer not correctly aligned at index 0", ab);
 
@@ -428,8 +438,9 @@
                 l = l - l_mod;
 
                 int ec = l - p;
-                if (as.limit() != ec)
+                if (as.limit() != ec) {
                     fail("Buffer capacity incorrect, expected: " + ec, as);
+                }
             }
         }
     }
@@ -441,6 +452,22 @@
         fail(problem + String.format(": x=%s y=%s", x, y), xb, yb);
     }
 
+    private static void catchIllegalArgument(Buffer b, Runnable thunk) {
+        tryCatch(b, IllegalArgumentException.class, thunk);
+    }
+
+    private static void catchReadOnlyBuffer(Buffer b, Runnable thunk) {
+        tryCatch(b, ReadOnlyBufferException.class, thunk);
+    }
+
+    private static void catchIndexOutOfBounds(Buffer b, Runnable thunk) {
+        tryCatch(b, IndexOutOfBoundsException.class, thunk);
+    }
+
+    private static void catchIndexOutOfBounds(byte[] t, Runnable thunk) {
+        tryCatch(t, IndexOutOfBoundsException.class, thunk);
+    }
+
     private static void tryCatch(Buffer b, Class<?> ex, Runnable thunk) {
         boolean caught = false;
         try {
@@ -452,11 +479,12 @@
                 fail(x.getMessage() + " not expected");
             }
         }
-        if (!caught)
+        if (!caught) {
             fail(ex.getName() + " not thrown", b);
+        }
     }
 
-    private static void tryCatch(byte [] t, Class<?> ex, Runnable thunk) {
+    private static void tryCatch(byte[] t, Class<?> ex, Runnable thunk) {
         tryCatch(ByteBuffer.wrap(t), ex, thunk);
     }
 
@@ -521,8 +549,6 @@
 
 
 
-
-
         // Compact
 
         relPut(b);
@@ -537,38 +563,14 @@
         b.limit(b.capacity() / 2);
         b.position(b.limit());
 
-        tryCatch(b, BufferUnderflowException.class, new Runnable() {
-                public void run() {
-                    b.get();
-                }});
-
-        tryCatch(b, BufferOverflowException.class, new Runnable() {
-                public void run() {
-                    b.put((byte)42);
-                }});
-
-        // The index must be non-negative and lesss than the buffer's limit.
-        tryCatch(b, IndexOutOfBoundsException.class, new Runnable() {
-                public void run() {
-                    b.get(b.limit());
-                }});
-        tryCatch(b, IndexOutOfBoundsException.class, new Runnable() {
-                public void run() {
-                    b.get(-1);
-                }});
-
-        tryCatch(b, IndexOutOfBoundsException.class, new Runnable() {
-                public void run() {
-                    b.put(b.limit(), (byte)42);
-                }});
-
-        tryCatch(b, InvalidMarkException.class, new Runnable() {
-                public void run() {
-                    b.position(0);
-                    b.mark();
-                    b.compact();
-                    b.reset();
-                }});
+        tryCatch(b, BufferUnderflowException.class, () -> b.get());
+        tryCatch(b, BufferOverflowException.class, () -> b.put((byte)42));
+        // The index must be non-negative and less than the buffer's limit.
+        catchIndexOutOfBounds(b, () -> b.get(b.limit()));
+        catchIndexOutOfBounds(b, () -> b.get(-1));
+        catchIndexOutOfBounds(b, () -> b.put(b.limit(), (byte)42));
+        tryCatch(b, InvalidMarkException.class,
+                () -> b.position(0).mark().compact().reset());
 
         try {
             b.position(b.limit() + 1);
@@ -635,7 +637,6 @@
 
 
 
-        byte v;
         b.flip();
         ck(b, b.get(), 0);
         ck(b, b.get(), (byte)-1);
@@ -665,6 +666,10 @@
 
 
 
+
+
+
+
         // Comparison
         b.rewind();
         ByteBuffer b2 = ByteBuffer.allocate(b.capacity());
@@ -683,14 +688,15 @@
 
 
 
-                    )
+                    ) {
                     out.println("[" + i + "] " + x + " != " + y);
+                }
             }
             fail("Identical buffers not equal", b, b2);
         }
-        if (b.compareTo(b2) != 0)
+        if (b.compareTo(b2) != 0) {
             fail("Comparison to identical buffer != 0", b, b2);
-
+        }
         b.limit(b.limit() + 1);
         b.position(b.limit() - 1);
         b.put((byte)99);
@@ -714,7 +720,7 @@
             if (xb.compareTo(xb) != 0) {
                 fail("compareTo not reflexive", xb, xb, x, x);
             }
-            if (! xb.equals(xb)) {
+            if (!xb.equals(xb)) {
                 fail("equals not reflexive", xb, xb, x, x);
             }
             for (byte y : VALUES) {
@@ -765,9 +771,10 @@
 
         if (!sb.equals(sb2))
             fail("Sliced slices do not match", sb, sb2);
-        if ((sb.hasArray()) && (sb.arrayOffset() != sb2.arrayOffset()))
+        if ((sb.hasArray()) && (sb.arrayOffset() != sb2.arrayOffset())) {
             fail("Array offsets do not match: "
                  + sb.arrayOffset() + " != " + sb2.arrayOffset(), sb, sb2);
+        }
 
 
 
@@ -808,102 +815,32 @@
             fail("Buffer not equal to read-only view", b, rb);
         show(level + 1, rb);
 
-        tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
-                public void run() {
-                    relPut(rb);
-                }});
-
-        tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
-                public void run() {
-                    absPut(rb);
-                }});
-
-        tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
-                public void run() {
-                    bulkPutArray(rb);
-                }});
-
-        tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
-                public void run() {
-                    bulkPutBuffer(rb);
-                }});
+        catchReadOnlyBuffer(b, () -> relPut(rb));
+        catchReadOnlyBuffer(b, () -> absPut(rb));
+        catchReadOnlyBuffer(b, () -> bulkPutArray(rb));
+        catchReadOnlyBuffer(b, () -> bulkPutBuffer(rb));
 
         // put(ByteBuffer) should not change source position
         final ByteBuffer src = ByteBuffer.allocate(1);
-        tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
-                public void run() {
-                    rb.put(src);
-                 }});
+        catchReadOnlyBuffer(b, () -> rb.put(src));
         ck(src, src.position(), 0);
 
-        tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
-                public void run() {
-                    rb.compact();
-                }});
+        catchReadOnlyBuffer(b, () -> rb.compact());
 
 
 
-        tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
-                public void run() {
-                    rb.putChar((char)1);
-                }});
-        tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
-                public void run() {
-                    rb.putChar(0, (char)1);
-                }});
-
-        tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
-                public void run() {
-                    rb.putShort((short)1);
-                }});
-        tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
-                public void run() {
-                    rb.putShort(0, (short)1);
-                }});
-
-        tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
-                public void run() {
-                    rb.putInt(1);
-                }});
-        tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
-                public void run() {
-                    rb.putInt(0, 1);
-                }});
-
-        tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
-                public void run() {
-                    rb.putLong((long)1);
-                }});
-        tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
-                public void run() {
-                    rb.putLong(0, (long)1);
-                }});
-
-        tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
-                public void run() {
-                    rb.putFloat((float)1);
-                }});
-        tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
-                public void run() {
-                    rb.putFloat(0, (float)1);
-                }});
-
-        tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
-                public void run() {
-                    rb.putDouble((double)1);
-                }});
-        tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
-                public void run() {
-                    rb.putDouble(0, (double)1);
-                }});
-
-
-
-
-
-
-
-
+        catchReadOnlyBuffer(b, () -> rb.putChar((char)1));
+        catchReadOnlyBuffer(b, () -> rb.putChar(0, (char)1));
+        catchReadOnlyBuffer(b, () -> rb.putShort((short)1));
+        catchReadOnlyBuffer(b, () -> rb.putShort(0, (short)1));
+        catchReadOnlyBuffer(b, () -> rb.putInt(1));
+        catchReadOnlyBuffer(b, () -> rb.putInt(0, 1));
+        catchReadOnlyBuffer(b, () -> rb.putLong((long)1));
+        catchReadOnlyBuffer(b, () -> rb.putLong(0, (long)1));
+        catchReadOnlyBuffer(b, () -> rb.putFloat((float)1));
+        catchReadOnlyBuffer(b, () -> rb.putFloat(0, (float)1));
+        catchReadOnlyBuffer(b, () -> rb.putDouble((double)1));
+        catchReadOnlyBuffer(b, () -> rb.putDouble(0, (double)1));
 
 
 
@@ -916,21 +853,11 @@
 
 
         if (rb.getClass().getName().startsWith("java.nio.Heap")) {
-
-            tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
-                    public void run() {
-                        rb.array();
-                    }});
-
-            tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
-                    public void run() {
-                        rb.arrayOffset();
-                    }});
-
-            if (rb.hasArray())
-                fail("Read-only heap buffer's backing array is accessible",
-                     rb);
-
+            catchReadOnlyBuffer(b, () -> rb.array());
+            catchReadOnlyBuffer(b, () -> rb.arrayOffset());
+            if (rb.hasArray()) {
+                fail("Read-only heap buffer's backing array is accessible", rb);
+            }
         }
 
         // Bulk puts from read-only buffers
@@ -1001,47 +928,6 @@
 
 
 
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
     public static void test(final byte [] ba) {
         int offset = 47;
         int length = 900;
@@ -1052,40 +938,21 @@
         ck(b, b.limit(), offset + length);
 
         // The offset must be non-negative and no larger than <array.length>.
-        tryCatch(ba, IndexOutOfBoundsException.class, new Runnable() {
-                public void run() {
-                    ByteBuffer.wrap(ba, -1, ba.length);
-                }});
-        tryCatch(ba, IndexOutOfBoundsException.class, new Runnable() {
-                public void run() {
-                    ByteBuffer.wrap(ba, ba.length + 1, ba.length);
-                }});
-        tryCatch(ba, IndexOutOfBoundsException.class, new Runnable() {
-                public void run() {
-                    ByteBuffer.wrap(ba, 0, -1);
-                }});
-        tryCatch(ba, IndexOutOfBoundsException.class, new Runnable() {
-                public void run() {
-                    ByteBuffer.wrap(ba, 0, ba.length + 1);
-                }});
+        catchIndexOutOfBounds(ba, () -> ByteBuffer.wrap(ba, -1, ba.length));
+        catchIndexOutOfBounds(ba, () -> ByteBuffer.wrap(ba, ba.length + 1, ba.length));
+        catchIndexOutOfBounds(ba, () -> ByteBuffer.wrap(ba, 0, -1));
+        catchIndexOutOfBounds(ba, () -> ByteBuffer.wrap(ba, 0, ba.length + 1));
 
         // A NullPointerException will be thrown if the array is null.
-        tryCatch(ba, NullPointerException.class, new Runnable() {
-                public void run() {
-                    ByteBuffer.wrap((byte []) null, 0, 5);
-                }});
-        tryCatch(ba, NullPointerException.class, new Runnable() {
-                public void run() {
-                    ByteBuffer.wrap((byte []) null);
-                }});
+        tryCatch(ba, NullPointerException.class,
+                () -> ByteBuffer.wrap((byte []) null, 0, 5));
+        tryCatch(ba, NullPointerException.class,
+                () -> ByteBuffer.wrap((byte []) null));
     }
 
     private static void testAllocate() {
         // An IllegalArgumentException will be thrown for negative capacities.
-        tryCatch((Buffer) null, IllegalArgumentException.class, new Runnable() {
-                public void run() {
-                    ByteBuffer.allocate(-1);
-                }});
+        catchIllegalArgument((Buffer) null, () -> ByteBuffer.allocate(-1));
         try {
             ByteBuffer.allocate(-1);
         } catch (IllegalArgumentException e) {
@@ -1095,10 +962,7 @@
             }
         }
 
-        tryCatch((Buffer) null, IllegalArgumentException.class, new Runnable() {
-                public void run() {
-                    ByteBuffer.allocateDirect(-1);
-                }});
+        catchIllegalArgument((Buffer) null, () -> ByteBuffer.allocateDirect(-1));
         try {
             ByteBuffer.allocateDirect(-1);
         } catch (IllegalArgumentException e) {