< prev index next >

test/java/nio/Buffer/BasicByte.java

Print this page

        

@@ -29,11 +29,10 @@
  */
 
 // -- This file was mechanically generated: Do not edit! -- //
 
 import java.nio.*;
-import java.lang.reflect.Method;
 
 
 public class BasicByte
     extends Basic
 {

@@ -58,74 +57,79 @@
 
     };
 
     private static void relGet(ByteBuffer b) {
         int n = b.capacity();
-        byte v;
-        for (int i = 0; i < n; i++)
+        for (int i = 0; i < n; i++) {
             ck(b, (long)b.get(), (long)((byte)ic(i)));
+        }
         b.rewind();
     }
 
     private static void relGet(ByteBuffer b, int start) {
         int n = b.remaining();
-        byte v;
-        for (int i = start; i < n; i++)
+        for (int i = start; i < n; i++) {
             ck(b, (long)b.get(), (long)((byte)ic(i)));
+        }
         b.rewind();
     }
 
     private static void absGet(ByteBuffer b) {
         int n = b.capacity();
-        byte v;
-        for (int i = 0; i < n; i++)
+        for (int i = 0; i < n; i++) {
             ck(b, (long)b.get(), (long)((byte)ic(i)));
+        }
         b.rewind();
     }
 
     private static void bulkGet(ByteBuffer b) {
         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) {
         int n = b.capacity();
         b.clear();
-        for (int i = 0; i < n; i++)
+        for (int i = 0; i < n; i++) {
             b.put((byte)ic(i));
+        }
         b.flip();
     }
 
     private static void absPut(ByteBuffer b) {
         int n = b.capacity();
         b.clear();
-        for (int i = 0; i < n; i++)
+        for (int i = 0; i < n; i++) {
             b.put(i, (byte)ic(i));
+        }
         b.limit(n);
         b.position(0);
     }
 
     private static void bulkPutArray(ByteBuffer b) {
         int n = b.capacity();
         b.clear();
         byte[] a = new byte[n + 7];
-        for (int i = 0; i < n; i++)
+        for (int i = 0; i < n; i++) {
             a[i + 7] = (byte)ic(i);
+        }
         b.put(a, 7, n);
         b.flip();
     }
 
     private static void bulkPutBuffer(ByteBuffer b) {
         int n = b.capacity();
         b.clear();
         ByteBuffer c = ByteBuffer.allocate(n + 7);
         c.position(7);
-        for (int i = 0; i < n; i++)
+        for (int i = 0; i < n; i++) {
             c.put((byte)ic(i));
+        }
         c.flip();
         c.position(7);
         b.put(c);
         b.flip();
         try {

@@ -186,54 +190,57 @@
 
 
 
 
 
+
     private static void checkSlice(ByteBuffer b, ByteBuffer slice) {
         ck(slice, 0, slice.position());
         ck(slice, b.remaining(), slice.limit());
         ck(slice, b.remaining(), slice.capacity());
-        if (b.isDirect() != slice.isDirect())
+        if (b.isDirect() != slice.isDirect()) {
             fail("Lost direction", slice);
-        if (b.isReadOnly() != slice.isReadOnly())
+        }
+        if (b.isReadOnly() != slice.isReadOnly()) {
             fail("Lost read-only", slice);
     }
+    }
 
 
 
     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);
     }
 
     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) {
             fail(e.getMessage(), b);
         }
     }
 
     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) {
 
         ShortBuffer sb = b.asShortBuffer();

@@ -362,79 +369,86 @@
         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
-        if (b.alignmentOffset(0, 8) != longMisalignmentAtZero)
+        if (b.alignmentOffset(0, 8) != longMisalignmentAtZero) {
             fail("Test input buffer not correctly aligned at index 0", b);
-
+        }
         // Test misalignment values
         for (int us : new int[]{1, 2, 4, 8}) {
             for (int i = 0; i < us * 2; i++) {
                 int am = b.alignmentOffset(i, us);
                 int expectedAm = (longMisalignmentAtZero + i) % us;
 
-                if (am != expectedAm)
+                if (am != expectedAm) {
                     fail(String.format("b.alignmentOffset(%d, %d) == %d incorrect, expected %d", i, us, am, expectedAm));
             }
         }
+        }
 
         // Created aligned slice to test against
         int ap = 8 - longMisalignmentAtZero;
         int al = b.limit() - b.alignmentOffset(b.limit(), 8);
         ByteBuffer ab = b.position(ap).limit(al).
                 slice();
-        if (ab.limit() == 0)
+        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)
+        }
+        if (ab.alignmentOffset(0, 8) != 0) {
             fail("Aligned test input buffer not correctly aligned at index 0", ab);
+        }
 
         for (int us : new int[]{1, 2, 4, 8}) {
             for (int p = 1; p < 16; p++) {
                 int l = ab.limit() - p;
 
                 ByteBuffer as = ab.slice().position(p).limit(l).
                         alignedSlice(us);
 
                 ck(as, 0, as.position());
                 ck(as, as.capacity(), as.limit());
-                if (b.isDirect() != as.isDirect())
+                if (b.isDirect() != as.isDirect()) {
                     fail("Lost direction", as);
-                if (b.isReadOnly() != as.isReadOnly())
+                }
+                if (b.isReadOnly() != as.isReadOnly()) {
                     fail("Lost read-only", as);
-
-                if (as.alignmentOffset(0, us) != 0)
+                }
+                if (as.alignmentOffset(0, us) != 0) {
                     fail("Buffer not correctly aligned at index 0", as);
-
-                if (as.alignmentOffset(as.limit(), us) != 0)
+                }
+                if (as.alignmentOffset(as.limit(), us) != 0) {
                     fail("Buffer not correctly aligned at limit", as);
-
+                }
                 int p_mod = ab.alignmentOffset(p, us);
                 int l_mod = ab.alignmentOffset(l, us);
                 // Round up position
                 p = (p_mod > 0) ? p + (us - p_mod) : p;
                 // Round down limit
                 l = l - l_mod;
 
                 int ec = l - p;
-                if (as.limit() != ec)
+                if (as.limit() != ec) {
                     fail("Buffer capacity incorrect, expected: " + ec, as);
             }
         }
     }
+    }
 
 
     private static void fail(String problem,
                              ByteBuffer xb, ByteBuffer yb,
                              byte x, byte y) {

@@ -450,24 +464,26 @@
                 caught = true;
             } else {
                 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) {
         tryCatch(ByteBuffer.wrap(t), ex, thunk);
     }
 
     public static void test(int level, final ByteBuffer b, boolean direct) {
 
         show(level, b);
 
-        if (direct != b.isDirect())
+        if (direct != b.isDirect()) {
             fail("Wrong direction", b);
+        }
 
         // Gets and puts
 
         relPut(b);
         relGet(b);

@@ -520,11 +536,10 @@
 
 
 
 
 
-
         // Compact
 
         relPut(b);
         b.position(13);
         b.compact();

@@ -535,42 +550,17 @@
 
         relPut(b);
         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.
+        tryCatch(b, IndexOutOfBoundsException.class, () -> b.get(b.limit()));
+        tryCatch(b, IndexOutOfBoundsException.class, () -> b.get(-1));
+        tryCatch(b, IndexOutOfBoundsException.class, () -> b.put(b.limit(), (byte)42));
+        tryCatch(b, InvalidMarkException.class, () -> b.position(0).mark().compact().reset());
 
         try {
             b.position(b.limit() + 1);
             fail("IllegalArgumentException expected for position beyond limit");
         } catch (IllegalArgumentException e) {

@@ -633,11 +623,10 @@
 
 
 
 
 
-        byte v;
         b.flip();
         ck(b, b.get(), 0);
         ck(b, b.get(), (byte)-1);
         ck(b, b.get(), 1);
         ck(b, b.get(), Byte.MAX_VALUE);

@@ -663,10 +652,14 @@
 
 
 
 
 
+
+
+
+
         // Comparison
         b.rewind();
         ByteBuffer b2 = ByteBuffer.allocate(b.capacity());
         b2.put(b);
         b2.flip();

@@ -681,42 +674,47 @@
 
 
 
 
 
-                    )
+                    ) {
                     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);
         b.rewind();
         b2.rewind();
-        if (b.equals(b2))
+        if (b.equals(b2)) {
             fail("Non-identical buffers equal", b, b2);
-        if (b.compareTo(b2) <= 0)
+        }
+        if (b.compareTo(b2) <= 0) {
             fail("Comparison to shorter buffer <= 0", b, b2);
+        }
         b.limit(b.limit() - 1);
 
         b.put(2, (byte)42);
-        if (b.equals(b2))
+        if (b.equals(b2)) {
             fail("Non-identical buffers equal", b, b2);
-        if (b.compareTo(b2) <= 0)
+        }
+        if (b.compareTo(b2) <= 0) {
             fail("Comparison to lesser buffer <= 0", b, b2);
+        }
 
         // Check equals and compareTo with interesting values
         for (byte x : VALUES) {
             ByteBuffer xb = ByteBuffer.wrap(new byte[] { x });
             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) {
                 ByteBuffer yb = ByteBuffer.wrap(new byte[] { y });
                 if (xb.compareTo(yb) != - yb.compareTo(xb)) {

@@ -761,15 +759,17 @@
         checkSlice(b, sb);
         b.position(0);
         ByteBuffer sb2 = sb.slice();
         checkSlice(sb, sb2);
 
-        if (!sb.equals(sb2))
+        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);
+        }
 
 
 
         // Views
 

@@ -802,102 +802,41 @@
 
         // Read-only views
 
         b.rewind();
         final ByteBuffer rb = b.asReadOnlyBuffer();
-        if (!b.equals(rb))
+        if (!b.equals(rb)) {
             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);
-                }});
+        tryCatch(b, ReadOnlyBufferException.class, () -> relPut(rb));
+        tryCatch(b, ReadOnlyBufferException.class, () -> absPut(rb));
+        tryCatch(b, ReadOnlyBufferException.class, () -> bulkPutArray(rb));
+        tryCatch(b, ReadOnlyBufferException.class, () -> 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);
-                 }});
+        tryCatch(b, ReadOnlyBufferException.class, () -> rb.put(src));
         ck(src, src.position(), 0);
 
-        tryCatch(b, ReadOnlyBufferException.class, new Runnable() {
-                public void run() {
-                    rb.compact();
-                }});
+        tryCatch(b, ReadOnlyBufferException.class, () -> 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);
-                }});
+        tryCatch(b, ReadOnlyBufferException.class, () -> rb.putChar((char)1));
+        tryCatch(b, ReadOnlyBufferException.class, () -> rb.putChar(0, (char)1));
+        tryCatch(b, ReadOnlyBufferException.class, () -> rb.putShort((short)1));
+        tryCatch(b, ReadOnlyBufferException.class, () -> rb.putShort(0, (short)1));
+        tryCatch(b, ReadOnlyBufferException.class, () -> rb.putInt(1));
+        tryCatch(b, ReadOnlyBufferException.class, () -> rb.putInt(0, 1));
+        tryCatch(b, ReadOnlyBufferException.class, () -> rb.putLong((long)1));
+        tryCatch(b, ReadOnlyBufferException.class, () -> rb.putLong(0, (long)1));
+        tryCatch(b, ReadOnlyBufferException.class, () -> rb.putFloat((float)1));
+        tryCatch(b, ReadOnlyBufferException.class, () -> rb.putFloat(0, (float)1));
+        tryCatch(b, ReadOnlyBufferException.class, () -> rb.putDouble((double)1));
+        tryCatch(b, ReadOnlyBufferException.class, () -> rb.putDouble(0, (double)1));
 
 
 
 
 

@@ -914,25 +853,15 @@
 
 
 
 
         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);
-
+            tryCatch(b, ReadOnlyBufferException.class, () -> rb.array());
+            tryCatch(b, ReadOnlyBufferException.class, () -> rb.arrayOffset());
+            if (rb.hasArray()) {
+                fail("Read-only heap buffer's backing array is accessible", rb);
+            }
         }
 
         // Bulk puts from read-only buffers
 
         b.clear();

@@ -1050,57 +979,33 @@
         ck(b, b.capacity(), ba.length);
         ck(b, b.position(), offset);
         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);
-                }});
+        tryCatch(ba, IndexOutOfBoundsException.class, () -> ByteBuffer.wrap(ba, -1, ba.length));
+        tryCatch(ba, IndexOutOfBoundsException.class, () -> ByteBuffer.wrap(ba, ba.length + 1, ba.length));
+        tryCatch(ba, IndexOutOfBoundsException.class, () -> ByteBuffer.wrap(ba, 0, -1));
+        tryCatch(ba, IndexOutOfBoundsException.class, () -> 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);
-                }});
+        tryCatch((Buffer) null, IllegalArgumentException.class, () -> ByteBuffer.allocate(-1));
         try {
             ByteBuffer.allocate(-1);
         } catch (IllegalArgumentException e) {
             if (e.getMessage() == null) {
                 fail("Non-null IllegalArgumentException message expected for"
                      + " attempt to allocate negative capacity buffer");
             }
         }
 
-        tryCatch((Buffer) null, IllegalArgumentException.class, new Runnable() {
-                public void run() {
-                    ByteBuffer.allocateDirect(-1);
-                }});
+        tryCatch((Buffer) null, IllegalArgumentException.class, () -> ByteBuffer.allocateDirect(-1));
         try {
             ByteBuffer.allocateDirect(-1);
         } catch (IllegalArgumentException e) {
             if (e.getMessage() == null) {
                 fail("Non-null IllegalArgumentException message expected for"

@@ -1115,12 +1020,13 @@
         test(0, ByteBuffer.allocate(7 * 1024), false);
         test(0, ByteBuffer.wrap(new byte[7 * 1024], 0, 7 * 1024), false);
         test(new byte[1024]);
 
         ByteBuffer b = ByteBuffer.allocateDirect(7 * 1024);
-        for (b.position(0); b.position() < b.limit(); )
+        for (b.position(0); b.position() < b.limit(); ) {
             ck(b, b.get(), 0);
+        }
         test(0, b, true);
 
 
 
 
< prev index next >