hotspot/test/compiler/intrinsics/unsafe/ByteBufferTest.java
author hseigel
Wed, 01 Mar 2017 08:00:02 -0500
changeset 46194 5596e6f63072
parent 39967 69932114a0fc
permissions -rw-r--r--
8172307: Remove ununsed JVM API JVM_GetModuleByPackageName() Summary: Remove get_module_by_package_name() etc., and unneeded test. Reviewed-by: sspitsyn, gtriantafill
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
39967
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
     1
/*
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
     2
 * Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
     3
 * Copyright (c) 2015, 2016, Red Hat Inc. All rights reserved.
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
     4
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
     5
 *
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
     6
 * This code is free software; you can redistribute it and/or modify it
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
     7
 * under the terms of the GNU General Public License version 2 only, as
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
     8
 * published by the Free Software Foundation.
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
     9
 *
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
    10
 * This code is distributed in the hope that it will be useful, but WITHOUT
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
    11
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
    12
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
    13
 * version 2 for more details (a copy is included in the LICENSE file that
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
    14
 * accompanied this code).
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
    15
 *
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
    16
 * You should have received a copy of the GNU General Public License version
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
    17
 * 2 along with this work; if not, write to the Free Software Foundation,
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
    18
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
    19
 *
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
    20
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
    21
 * or visit www.oracle.com if you need additional information or have any
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
    22
 * questions.
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
    23
 */
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
    24
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
    25
import jdk.test.lib.Utils;
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
    26
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
    27
import java.nio.Buffer;
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
    28
import java.nio.BufferOverflowException;
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
    29
import java.nio.BufferUnderflowException;
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
    30
import java.nio.ByteBuffer;
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
    31
import java.nio.ByteOrder;
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
    32
import java.nio.CharBuffer;
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
    33
import java.nio.DoubleBuffer;
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
    34
import java.nio.FloatBuffer;
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
    35
import java.nio.IntBuffer;
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
    36
import java.nio.LongBuffer;
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
    37
import java.nio.ShortBuffer;
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
    38
import java.util.Arrays;
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
    39
import java.util.Random;
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
    40
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
    41
import static java.nio.ByteOrder.BIG_ENDIAN;
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
    42
import static java.nio.ByteOrder.LITTLE_ENDIAN;
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
    43
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
    44
// A wrapper for a ByteBuffer which maintains a backing array and a
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
    45
// position.  Whenever this wrapper is written the backing array and
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
    46
// the wrapped byte buffer are updated together, and whenever it is
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
    47
// read we check that the ByteBuffer and the backing array are identical.
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
    48
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
    49
class MyByteBuffer {
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
    50
    final ByteBuffer buf;
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
    51
    final byte[] bytes;
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
    52
    int pos;
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
    53
    ByteOrder byteOrder = BIG_ENDIAN;
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
    54
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
    55
    MyByteBuffer(ByteBuffer buf) {
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
    56
        this.buf = buf;
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
    57
        this.bytes = new byte[buf.capacity()];
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
    58
        pos = 0;
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
    59
    }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
    60
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
    61
    public final MyByteBuffer order(ByteOrder bo) {
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
    62
        byteOrder = bo;
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
    63
        buf.order(bo);
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
    64
        return this;
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
    65
    }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
    66
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
    67
    static MyByteBuffer allocate(int capacity) {
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
    68
        return new MyByteBuffer(ByteBuffer.allocate(capacity));
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
    69
    }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
    70
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
    71
    static MyByteBuffer allocateDirect(int capacity) {
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
    72
        return new MyByteBuffer(ByteBuffer.allocateDirect(capacity));
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
    73
    }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
    74
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
    75
    int capacity() { return bytes.length; }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
    76
    int position() {
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
    77
        if (buf.position() != pos)
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
    78
            throw new RuntimeException();
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
    79
        return buf.position();
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
    80
    }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
    81
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
    82
    byte[] actualArray() {
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
    83
        buf.rewind();
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
    84
        byte[] actual = new byte[bytes.length];
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
    85
        buf.get(actual, 0, actual.length);
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
    86
        buf.rewind();
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
    87
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
    88
        return actual;
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
    89
    }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
    90
    byte[] expectedArray() { return bytes; }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
    91
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
    92
    private static byte long7(long x) { return (byte)(x >> 56); }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
    93
    private static byte long6(long x) { return (byte)(x >> 48); }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
    94
    private static byte long5(long x) { return (byte)(x >> 40); }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
    95
    private static byte long4(long x) { return (byte)(x >> 32); }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
    96
    private static byte long3(long x) { return (byte)(x >> 24); }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
    97
    private static byte long2(long x) { return (byte)(x >> 16); }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
    98
    private static byte long1(long x) { return (byte)(x >>  8); }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
    99
    private static byte long0(long x) { return (byte)(x      ); }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   100
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   101
    private static byte int3(int x) { return (byte)(x >> 24); }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   102
    private static byte int2(int x) { return (byte)(x >> 16); }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   103
    private static byte int1(int x) { return (byte)(x >>  8); }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   104
    private static byte int0(int x) { return (byte)(x      ); }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   105
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   106
    private static byte short1(short x) { return (byte)(x >> 8); }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   107
    private static byte short0(short x) { return (byte)(x     ); }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   108
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   109
    byte _get(long i) { return bytes[(int)i]; }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   110
    void _put(long i, byte x) { bytes[(int)i] = x; }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   111
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   112
    private void putLongX(long a, long x) {
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   113
        if (byteOrder == BIG_ENDIAN) {
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   114
            x = Long.reverseBytes(x);
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   115
        }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   116
        _put(a + 7, long7(x));
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   117
        _put(a + 6, long6(x));
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   118
        _put(a + 5, long5(x));
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   119
        _put(a + 4, long4(x));
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   120
        _put(a + 3, long3(x));
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   121
        _put(a + 2, long2(x));
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   122
        _put(a + 1, long1(x));
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   123
        _put(a    , long0(x));
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   124
    }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   125
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   126
    private void putIntX(long a, int x) {
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   127
        if (byteOrder == BIG_ENDIAN) {
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   128
            x = Integer.reverseBytes(x);
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   129
        }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   130
        _put(a + 3, int3(x));
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   131
        _put(a + 2, int2(x));
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   132
        _put(a + 1, int1(x));
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   133
        _put(a    , int0(x));
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   134
    }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   135
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   136
    private void putShortX(int bi, short x) {
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   137
        if (byteOrder == BIG_ENDIAN) {
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   138
            x = Short.reverseBytes(x);
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   139
        }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   140
        _put(bi    , short0(x));
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   141
        _put(bi + 1, short1(x));
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   142
    }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   143
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   144
    static private int makeInt(byte b3, byte b2, byte b1, byte b0) {
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   145
        return (((b3       ) << 24) |
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   146
                ((b2 & 0xff) << 16) |
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   147
                ((b1 & 0xff) <<  8) |
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   148
                ((b0 & 0xff)      ));
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   149
    }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   150
    int getIntX(long a) {
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   151
        int x = makeInt(_get(a + 3),
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   152
                _get(a + 2),
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   153
                _get(a + 1),
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   154
                _get(a));
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   155
        if (byteOrder == BIG_ENDIAN) {
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   156
            x = Integer.reverseBytes(x);
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   157
        }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   158
        return x;
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   159
    }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   160
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   161
    static private long makeLong(byte b7, byte b6, byte b5, byte b4,
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   162
                                 byte b3, byte b2, byte b1, byte b0)
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   163
    {
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   164
        return ((((long)b7       ) << 56) |
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   165
                (((long)b6 & 0xff) << 48) |
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   166
                (((long)b5 & 0xff) << 40) |
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   167
                (((long)b4 & 0xff) << 32) |
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   168
                (((long)b3 & 0xff) << 24) |
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   169
                (((long)b2 & 0xff) << 16) |
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   170
                (((long)b1 & 0xff) <<  8) |
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   171
                (((long)b0 & 0xff)      ));
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   172
    }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   173
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   174
    long getLongX(long a) {
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   175
        long x = makeLong(_get(a + 7),
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   176
                _get(a + 6),
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   177
                _get(a + 5),
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   178
                _get(a + 4),
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   179
                _get(a + 3),
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   180
                _get(a + 2),
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   181
                _get(a + 1),
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   182
                _get(a));
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   183
        if (byteOrder == BIG_ENDIAN) {
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   184
            x = Long.reverseBytes(x);
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   185
        }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   186
        return x;
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   187
    }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   188
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   189
    static private short makeShort(byte b1, byte b0) {
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   190
        return (short)((b1 << 8) | (b0 & 0xff));
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   191
    }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   192
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   193
    short getShortX(long a) {
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   194
        short x = makeShort(_get(a + 1),
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   195
                            _get(a    ));
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   196
        if (byteOrder == BIG_ENDIAN) {
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   197
            x = Short.reverseBytes(x);
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   198
        }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   199
        return x;
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   200
    }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   201
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   202
    double getDoubleX(long a) {
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   203
        long x = getLongX(a);
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   204
        return Double.longBitsToDouble(x);
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   205
    }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   206
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   207
    double getFloatX(long a) {
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   208
        int x = getIntX(a);
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   209
        return Float.intBitsToFloat(x);
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   210
    }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   211
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   212
    void ck(long x, long y) {
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   213
        if (x != y) {
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   214
            throw new RuntimeException(" x = " + Long.toHexString(x) + ", y = " + Long.toHexString(y));
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   215
        }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   216
    }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   217
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   218
    void ck(double x, double y) {
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   219
        if (x == x && y == y && x != y) {
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   220
            ck(x, y);
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   221
        }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   222
    }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   223
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   224
    // Method accessors
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   225
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   226
    long getLong(int i) { ck(buf.getLong(i), getLongX(i)); return buf.getLong(i); }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   227
    int getInt(int i) { ck(buf.getInt(i), getIntX(i)); return buf.getInt(i); }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   228
    short getShort(int i) { ck(buf.getShort(i), getShortX(i)); return buf.getShort(i); }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   229
    char getChar(int i) { ck(buf.getChar(i), (char)getShortX(i)); return buf.getChar(i); }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   230
    double getDouble(int i) { ck(buf.getDouble(i), getDoubleX(i)); return buf.getDouble(i); }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   231
    float getFloat(int i) { ck(buf.getFloat(i), getFloatX(i)); return buf.getFloat(i); }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   232
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   233
    void putLong(int i, long x) { buf.putLong(i, x); putLongX(i, x); }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   234
    void putInt(int i, int x) { buf.putInt(i, x); putIntX(i, x); }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   235
    void putShort(int i, short x) { buf.putShort(i, x); putShortX(i, x); }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   236
    void putChar(int i, char x) { buf.putChar(i, x); putShortX(i, (short)x); }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   237
    void putDouble(int i, double x) { buf.putDouble(i, x); putLongX(i, Double.doubleToRawLongBits(x)); }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   238
    void putFloat(int i, float x) { buf.putFloat(i, x); putIntX(i, Float.floatToRawIntBits(x)); }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   239
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   240
    long getLong() { ck(buf.getLong(buf.position()), getLongX(pos)); long x = buf.getLong(); pos += 8; return x; }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   241
    int getInt() { ck(buf.getInt(buf.position()), getIntX(pos)); int x = buf.getInt(); pos += 4; return x; }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   242
    short getShort() { ck(buf.getShort(buf.position()), getShortX(pos)); short x = buf.getShort(); pos += 2; return x; }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   243
    char getChar() {  ck(buf.getChar(buf.position()), (char)getShortX(pos)); char x = buf.getChar(); pos += 2; return x; }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   244
    double getDouble() { ck(buf.getDouble(buf.position()), getDoubleX(pos)); double x = buf.getDouble(); pos += 8; return x; }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   245
    float getFloat() { ck(buf.getFloat(buf.position()), getFloatX(pos)); float x = buf.getFloat(); pos += 4; return x; }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   246
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   247
    void putLong(long x) { putLongX(pos, x); pos += 8; buf.putLong(x); }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   248
    void putInt(int x) { putIntX(pos, x); pos += 4; buf.putInt(x); }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   249
    void putShort(short x) { putShortX(pos, x); pos += 2; buf.putShort(x); }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   250
    void putChar(char x) { putShortX(pos, (short)x); pos += 2; buf.putChar(x); }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   251
    void putDouble(double x) { putLongX(pos, Double.doubleToRawLongBits(x)); pos += 8; buf.putDouble(x); }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   252
    void putFloat(float x) { putIntX(pos, Float.floatToRawIntBits(x)); pos += 4; buf.putFloat(x); }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   253
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   254
    // View accessors
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   255
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   256
    long getLong(LongBuffer vb, int i) { ck(vb.get(i / 8), getLongX(i)); return vb.get(i / 8); }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   257
    int getInt(IntBuffer vb, int i) { ck(vb.get(i / 4), getIntX(i)); return vb.get(i / 4); }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   258
    short getShort(ShortBuffer vb, int i) { ck(vb.get(i / 2), getShortX(i)); return vb.get(i / 2); }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   259
    char getChar(CharBuffer vb, int i) { ck(vb.get(i / 2), (char)getShortX(i)); return vb.get(i / 2); }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   260
    double getDouble(DoubleBuffer vb, int i) { ck(vb.get(i / 8), getDoubleX(i)); return vb.get(i / 8); }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   261
    float getFloat(FloatBuffer vb, int i) { ck(vb.get(i / 4), getFloatX(i)); return vb.get(i / 4); }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   262
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   263
    void putLong(LongBuffer vb, int i, long x) { vb.put(i / 8, x); putLongX(i, x); }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   264
    void putInt(IntBuffer vb, int i, int x) { vb.put(i / 4, x); putIntX(i, x); }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   265
    void putShort(ShortBuffer vb, int i, short x) { vb.put(i / 2, x); putShortX(i, x); }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   266
    void putChar(CharBuffer vb, int i, char x) { vb.put(i / 2, x); putShortX(i, (short)x); }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   267
    void putDouble(DoubleBuffer vb, int i, double x) { vb.put(i / 8, x); putLongX(i, Double.doubleToRawLongBits(x)); }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   268
    void putFloat(FloatBuffer vb, int i, float x) { vb.put(i / 4, x); putIntX(i, Float.floatToRawIntBits(x)); }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   269
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   270
    long getLong(LongBuffer v) { ck(v.get(v.position()), getLongX(pos)); long x = v.get(); pos += 8; return x; }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   271
    int getInt(IntBuffer v) { ck(v.get(v.position()), getIntX(pos)); int x = v.get(); pos += 4; return x; }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   272
    short getShort(ShortBuffer v) { ck(v.get(v.position()), getShortX(pos)); short x = v.get(); pos += 2; return x; }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   273
    char getChar(CharBuffer v) { ck(v.get(v.position()), (char)getShortX(pos)); char x = v.get(); pos += 2; return x; }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   274
    double getDouble(DoubleBuffer v) { ck(v.get(v.position()), getDoubleX(pos)); double x = v.get(); pos += 8; return x; }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   275
    float getFloat(FloatBuffer v) { ck(v.get(v.position()), getFloatX(pos)); float x = v.get(); pos += 4; return x; }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   276
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   277
    void putLong(LongBuffer v, long x) { putLongX(pos, x); pos += 8; v.put(x); }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   278
    void putInt(IntBuffer v, int x) { putIntX(pos, x); pos += 4; v.put(x); }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   279
    void putShort(ShortBuffer v, short x) { putShortX(pos, x); pos += 2; v.put(x); }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   280
    void putChar(CharBuffer v, char x) { putShortX(pos, (short)x); pos += 2; v.put(x); }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   281
    void putDouble(DoubleBuffer v, double x) { putLongX(pos, Double.doubleToRawLongBits(x)); pos += 8; v.put(x); }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   282
    void putFloat(FloatBuffer v, float x) { putIntX(pos, Float.floatToRawIntBits(x)); pos += 4; v.put(x); }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   283
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   284
    void rewind() { pos = 0; buf.rewind(); }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   285
}
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   286
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   287
public abstract class ByteBufferTest implements Runnable {
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   288
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   289
    Random random = Utils.getRandomInstance();
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   290
    MyByteBuffer data;
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   291
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   292
    static int randomOffset(Random r, MyByteBuffer buf, int size) {
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   293
        return r.nextInt(buf.capacity() - size);
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   294
    }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   295
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   296
    static int randomAlignedOffset(Random r, MyByteBuffer buf, int unitSize) {
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   297
        return r.nextInt(buf.capacity() / unitSize) * unitSize;
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   298
    }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   299
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   300
    long iterations;
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   301
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   302
    ByteBufferTest(long iterations, boolean direct) {
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   303
        this.iterations = iterations;
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   304
        data = direct
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   305
               ? MyByteBuffer.allocateDirect(1024)
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   306
               : MyByteBuffer.allocate(1024);
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   307
    }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   308
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   309
    // The core of the test.  Walk over the buffer reading and writing
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   310
    // random data, XORing it as we go.  We can detect writes in the
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   311
    // wrong place, writes which are too long or too short, and reads
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   312
    // or writes of the wrong data,
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   313
    void step(Random r) {
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   314
        stepUsingAccessors(r);
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   315
        stepUsingViews(r);
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   316
    }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   317
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   318
    void stepUsingAccessors(Random r) {
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   319
        data.order((r.nextInt() & 1) != 0 ? BIG_ENDIAN : LITTLE_ENDIAN);
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   320
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   321
        data.rewind();
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   322
        while (data.position() < data.capacity())
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   323
            data.putLong(data.getLong() ^ random.nextLong());
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   324
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   325
        data.rewind();
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   326
        while (data.position() < data.capacity())
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   327
            data.putInt(data.getInt() ^ random.nextInt());
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   328
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   329
        data.rewind();
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   330
        while (data.position() < data.capacity())
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   331
            data.putShort((short)(data.getShort() ^ random.nextInt()));
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   332
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   333
        data.rewind();
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   334
        while (data.position() < data.capacity())
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   335
            data.putChar((char)(data.getChar() ^ random.nextInt()));
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   336
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   337
        data.rewind();
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   338
        while (data.position() < data.capacity())
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   339
            data.putDouble(combine(data.getDouble(), random.nextLong()));
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   340
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   341
        data.rewind();
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   342
        while (data.position() < data.capacity())
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   343
            data.putFloat(combine(data.getFloat(), random.nextInt()));
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   344
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   345
        for (int i = 0; i < 100; i++) {
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   346
            int offset = randomOffset(r, data, Long.BYTES);
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   347
            data.putLong(offset, data.getLong(offset) ^ random.nextLong());
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   348
        }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   349
        for (int i = 0; i < 100; i++) {
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   350
            int offset = randomOffset(r, data, Integer.BYTES);
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   351
            data.putInt(offset, data.getInt(offset) ^ random.nextInt());
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   352
        }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   353
        for (int i = 0; i < 100; i++) {
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   354
            int offset = randomOffset(r, data, Short.BYTES);
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   355
            data.putShort(offset, (short)(data.getShort(offset) ^ random.nextInt()));
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   356
        }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   357
        for (int i = 0; i < 100; i++) {
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   358
            int offset = randomOffset(r, data, Character.BYTES);
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   359
            data.putChar(offset, (char)(data.getChar(offset) ^ random.nextInt()));
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   360
        }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   361
        for (int i = 0; i < 100; i++) {
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   362
            int offset = randomOffset(r, data, Double.BYTES);
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   363
            data.putDouble(offset, combine(data.getDouble(offset), random.nextLong()));
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   364
        }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   365
        for (int i = 0; i < 100; i++) {
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   366
            int offset = randomOffset(r, data, Float.BYTES);
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   367
            data.putFloat(offset, combine(data.getFloat(offset), random.nextInt()));
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   368
        }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   369
    }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   370
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   371
    void stepUsingViews(Random r) {
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   372
        data.order((r.nextInt() & 1) != 0 ? BIG_ENDIAN : LITTLE_ENDIAN);
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   373
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   374
        data.rewind();
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   375
        LongBuffer lbuf = data.buf.asLongBuffer();
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   376
        while (lbuf.position() < data.capacity() / Long.BYTES)
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   377
            data.putLong(lbuf, data.getLong(lbuf) ^ random.nextLong());
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   378
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   379
        data.rewind();
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   380
        IntBuffer ibuf = data.buf.asIntBuffer();
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   381
        while (ibuf.position() < data.capacity() / Integer.BYTES)
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   382
            data.putInt(ibuf, data.getInt(ibuf) ^ random.nextInt());
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   383
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   384
        data.rewind();
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   385
        ShortBuffer sbuf = data.buf.asShortBuffer();
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   386
        while (sbuf.position() < data.capacity() / Short.BYTES)
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   387
            data.putShort(sbuf, (short)(data.getShort(sbuf) ^ random.nextInt()));
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   388
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   389
        data.rewind();
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   390
        CharBuffer cbuf = data.buf.asCharBuffer();
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   391
        while (cbuf.position() < data.capacity() / Character.BYTES)
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   392
            data.putChar(cbuf, (char)(data.getChar(cbuf) ^ random.nextInt()));
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   393
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   394
        data.rewind();
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   395
        DoubleBuffer dbuf = data.buf.asDoubleBuffer();
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   396
        while (dbuf.position() < data.capacity() / Double.BYTES)
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   397
            data.putDouble(dbuf, combine(data.getDouble(dbuf), random.nextLong()));
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   398
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   399
        data.rewind();
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   400
        FloatBuffer fbuf = data.buf.asFloatBuffer();
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   401
        while (fbuf.position() < data.capacity() / Float.BYTES)
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   402
            data.putFloat(fbuf, combine(data.getFloat(fbuf), random.nextInt()));
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   403
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   404
        for (int i = 0; i < 100; i++) {
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   405
            int offset = randomAlignedOffset(r, data, Long.BYTES);
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   406
            data.putLong(lbuf, offset, data.getLong(lbuf, offset) ^ random.nextLong());
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   407
        }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   408
        for (int i = 0; i < 100; i++) {
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   409
            int offset = randomAlignedOffset(r, data, Integer.BYTES);
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   410
            data.putInt(ibuf, offset, data.getInt(ibuf, offset) ^ random.nextInt());
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   411
        }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   412
        for (int i = 0; i < 100; i++) {
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   413
            int offset = randomAlignedOffset(r, data, Short.BYTES);
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   414
            data.putShort(sbuf, offset, (short)(data.getShort(sbuf, offset) ^ random.nextInt()));
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   415
        }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   416
        for (int i = 0; i < 100; i++) {
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   417
            int offset = randomAlignedOffset(r, data, Character.BYTES);
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   418
            data.putChar(cbuf, offset, (char)(data.getChar(cbuf, offset) ^ random.nextInt()));
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   419
        }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   420
        for (int i = 0; i < 100; i++) {
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   421
            int offset = randomAlignedOffset(r, data, Double.BYTES);
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   422
            data.putDouble(dbuf, offset, combine(data.getDouble(dbuf, offset), random.nextLong()));
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   423
        }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   424
        for (int i = 0; i < 100; i++) {
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   425
            int offset = randomAlignedOffset(r, data, Float.BYTES);
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   426
            data.putFloat(fbuf, offset, combine(data.getFloat(fbuf, offset), random.nextInt()));
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   427
        }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   428
    }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   429
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   430
    // XOR the bit pattern of a double and a long, returning the
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   431
    // result as a double.
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   432
    //
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   433
    // We convert signalling NaNs to quiet NaNs.  We need to do this
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   434
    // because some platforms (in particular legacy 80x87) do not
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   435
    // provide transparent conversions between integer and
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   436
    // floating-point types even when using raw conversions but
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   437
    // quietly convert sNaN to qNaN.  This causes spurious test
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   438
    // failures when the template interpreter uses 80x87 and the JITs
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   439
    // use XMM registers.
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   440
    //
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   441
    public double combine(double prev, long bits) {
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   442
        bits ^= Double.doubleToRawLongBits(prev);
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   443
        double result = Double.longBitsToDouble(bits);
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   444
        if (Double.isNaN(result)) {
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   445
            result = Double.longBitsToDouble(bits | 0x8000000000000l);
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   446
        }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   447
        return result;
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   448
    }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   449
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   450
    // XOR the bit pattern of a float and an int, returning the result
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   451
    // as a float.  Convert sNaNs to qNaNs.
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   452
    public Float combine(float prev, int bits) {
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   453
        bits ^= Float.floatToRawIntBits(prev);
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   454
        Float result = Float.intBitsToFloat(bits);
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   455
        if (Float.isNaN(result)) {
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   456
            result = Float.intBitsToFloat(bits | 0x400000);
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   457
        }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   458
        return result;
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   459
    }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   460
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   461
    enum PrimitiveType {
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   462
        BYTE(1), CHAR(2), SHORT(2), INT(4), LONG(8), FLOAT(4), DOUBLE(8);
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   463
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   464
        public final int size;
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   465
        PrimitiveType(int size) {
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   466
            this.size = size;
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   467
        }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   468
    }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   469
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   470
    Buffer asView(ByteBuffer b, PrimitiveType t) {
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   471
        switch (t) {
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   472
            case BYTE: return b;
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   473
            case CHAR: return b.asCharBuffer();
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   474
            case SHORT: return b.asShortBuffer();
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   475
            case INT: return b.asIntBuffer();
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   476
            case LONG: return b.asLongBuffer();
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   477
            case FLOAT: return b.asFloatBuffer();
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   478
            case DOUBLE: return b.asDoubleBuffer();
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   479
        }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   480
        throw new InternalError("Should not reach here");
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   481
    }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   482
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   483
    void getOne(ByteBuffer b, PrimitiveType t) {
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   484
        switch (t) {
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   485
        case BYTE: b.get(); break;
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   486
        case CHAR: b.getChar(); break;
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   487
        case SHORT: b.getShort(); break;
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   488
        case INT: b.getInt(); break;
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   489
        case LONG: b.getLong(); break;
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   490
        case FLOAT: b.getFloat(); break;
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   491
        case DOUBLE: b.getDouble(); break;
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   492
        }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   493
    }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   494
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   495
    void putOne(ByteBuffer b, PrimitiveType t) {
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   496
        switch (t) {
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   497
        case BYTE: b.put((byte)0); break;
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   498
        case CHAR: b.putChar('0'); break;
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   499
        case SHORT: b.putShort((short)0); break;
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   500
        case INT: b.putInt(0); break;
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   501
        case LONG: b.putLong(0); break;
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   502
        case FLOAT: b.putFloat(0); break;
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   503
        case DOUBLE: b.putDouble(0); break;
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   504
        }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   505
    }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   506
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   507
    void asViewGetOne(ByteBuffer b, PrimitiveType t) {
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   508
        switch (t) {
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   509
            case BYTE: b.get(); break;
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   510
            case CHAR: b.asCharBuffer().get(); break;
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   511
            case SHORT: b.asShortBuffer().get(); break;
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   512
            case INT: b.asIntBuffer().get(); break;
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   513
            case LONG: b.asLongBuffer().get(); break;
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   514
            case FLOAT: b.asFloatBuffer().get(); break;
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   515
            case DOUBLE: b.asDoubleBuffer().get(); break;
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   516
        }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   517
    }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   518
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   519
    void asViewPutOne(ByteBuffer b, PrimitiveType t) {
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   520
        switch (t) {
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   521
            case BYTE: b.put((byte)0); break;
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   522
            case CHAR: b.asCharBuffer().put('0'); break;
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   523
            case SHORT: b.asShortBuffer().put((short)0); break;
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   524
            case INT: b.asIntBuffer().put(0); break;
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   525
            case LONG: b.asLongBuffer().put(0); break;
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   526
            case FLOAT: b.asFloatBuffer().put(0); break;
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   527
            case DOUBLE: b.asDoubleBuffer().put(0); break;
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   528
        }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   529
    }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   530
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   531
    void getOne(ByteBuffer b, PrimitiveType t, int index) {
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   532
        switch (t) {
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   533
        case BYTE: b.get(index); break;
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   534
        case CHAR: b.getChar(index); break;
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   535
        case SHORT: b.getShort(index); break;
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   536
        case INT: b.getInt(index); break;
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   537
        case LONG: b.getLong(index); break;
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   538
        case FLOAT: b.getFloat(index); break;
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   539
        case DOUBLE: b.getDouble(index); break;
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   540
        }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   541
    }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   542
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   543
    void putOne(ByteBuffer b, PrimitiveType t, int index) {
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   544
        switch (t) {
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   545
        case BYTE: b.put(index, (byte)0); break;
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   546
        case CHAR: b.putChar(index, '0'); break;
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   547
        case SHORT: b.putShort(index, (short)0); break;
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   548
        case INT: b.putInt(index, 0); break;
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   549
        case LONG: b.putLong(index, 0); break;
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   550
        case FLOAT: b.putFloat(index, 0); break;
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   551
        case DOUBLE: b.putDouble(index, 0); break;
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   552
        }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   553
    }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   554
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   555
    void asViewGetOne(Buffer v, PrimitiveType t, int index) {
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   556
        switch (t) {
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   557
            case BYTE: ((ByteBuffer) v).get(index); break;
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   558
            case CHAR: ((CharBuffer) v).get(index); break;
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   559
            case SHORT: ((ShortBuffer) v).get(index); break;
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   560
            case INT: ((IntBuffer) v).get(index); break;
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   561
            case LONG: ((LongBuffer) v).get(index); break;
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   562
            case FLOAT: ((FloatBuffer) v).get(index); break;
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   563
            case DOUBLE: ((DoubleBuffer) v).get(index); break;
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   564
        }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   565
    }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   566
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   567
    void asViewPutOne(Buffer v, PrimitiveType t, int index) {
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   568
        switch (t) {
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   569
            case BYTE: ((ByteBuffer) v).put(index, (byte)0); break;
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   570
            case CHAR: ((CharBuffer) v).put(index, '0'); break;
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   571
            case SHORT: ((ShortBuffer) v).put(index, (short)0); break;
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   572
            case INT: ((IntBuffer) v).put(index, 0); break;
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   573
            case LONG: ((LongBuffer) v).put(index, 0); break;
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   574
            case FLOAT: ((FloatBuffer) v).put(index, 0); break;
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   575
            case DOUBLE: ((DoubleBuffer) v).put(index, 0); break;
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   576
        }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   577
    }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   578
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   579
    void checkBoundaryConditions() {
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   580
        for (int i = 0; i < 100; i++) {
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   581
            int bufSize = random.nextInt(16);
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   582
            ByteBuffer buf = data.buf.isDirect()
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   583
                    ? ByteBuffer.allocateDirect(bufSize)
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   584
                    : ByteBuffer.allocate(bufSize);
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   585
            for (PrimitiveType t : PrimitiveType.values()) {
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   586
                buf.rewind();
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   587
                Buffer viewBuf = asView(buf, t);
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   588
                for (int j = 0; j < 100; j++) {
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   589
                    int offset = random.nextInt(32) - 8;
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   590
                    int threw = 0;
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   591
                    int checks = 6;
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   592
                    try {
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   593
                        try {
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   594
                            buf.position(offset);
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   595
                            getOne(buf, t);
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   596
                        }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   597
                        catch (BufferUnderflowException e) {
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   598
                            if (offset + t.size < bufSize)
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   599
                                throw new RuntimeException
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   600
                                        ("type = " + t + ", offset = " + offset + ", bufSize = " + bufSize, e);
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   601
                            threw++;
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   602
                        }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   603
                        catch (IllegalArgumentException e) {
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   604
                            if (offset >= 0 && offset + t.size < bufSize)
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   605
                                throw new RuntimeException
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   606
                                        ("type = " + t + ", offset = " + offset + ", bufSize = " + bufSize, e);
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   607
                            threw++;
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   608
                        }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   609
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   610
                        try {
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   611
                            buf.position(offset);
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   612
                            asViewGetOne(buf, t);
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   613
                        }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   614
                        catch (BufferUnderflowException e) {
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   615
                            if (offset + t.size < bufSize)
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   616
                                throw new RuntimeException
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   617
                                        ("type = " + t + ", offset = " + offset + ", bufSize = " + bufSize, e);
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   618
                            threw++;
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   619
                        }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   620
                        catch (IllegalArgumentException e) {
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   621
                            if (offset >= 0 && offset + t.size < bufSize)
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   622
                                throw new RuntimeException
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   623
                                        ("type = " + t + ", offset = " + offset + ", bufSize = " + bufSize, e);
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   624
                            threw++;
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   625
                        }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   626
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   627
                        try {
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   628
                            buf.position(offset);
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   629
                            putOne(buf, t);
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   630
                        }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   631
                        catch (BufferOverflowException e) {
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   632
                            if (offset + t.size < bufSize)
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   633
                                throw new RuntimeException
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   634
                                        ("type = " + t + ", offset = " + offset + ", bufSize = " + bufSize, e);
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   635
                            threw++;
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   636
                        }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   637
                        catch (IllegalArgumentException e) {
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   638
                            if (offset >= 0 && offset + t.size < bufSize)
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   639
                                throw new RuntimeException
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   640
                                        ("type = " + t + ", offset = " + offset + ", bufSize = " + bufSize, e);
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   641
                            threw++;
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   642
                        }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   643
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   644
                        try {
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   645
                            buf.position(offset);
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   646
                            asViewPutOne(buf, t);
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   647
                        }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   648
                        catch (BufferOverflowException e) {
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   649
                            if (offset + t.size < bufSize)
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   650
                                throw new RuntimeException
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   651
                                        ("type = " + t + ", offset = " + offset + ", bufSize = " + bufSize, e);
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   652
                            threw++;
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   653
                        }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   654
                        catch (IllegalArgumentException e) {
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   655
                            if (offset >= 0 && offset + t.size < bufSize)
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   656
                                throw new RuntimeException
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   657
                                        ("type = " + t + ", offset = " + offset + ", bufSize = " + bufSize, e);
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   658
                            threw++;
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   659
                        }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   660
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   661
                        try {
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   662
                            putOne(buf, t, offset);
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   663
                        }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   664
                        catch (IndexOutOfBoundsException e) {
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   665
                            if (offset >= 0 && offset + t.size < bufSize)
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   666
                                throw new RuntimeException
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   667
                                        ("type = " + t + ", offset = " + offset + ", bufSize = " + bufSize, e);
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   668
                            threw++;
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   669
                        }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   670
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   671
                        try {
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   672
                            getOne(buf, t, offset);
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   673
                        }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   674
                        catch (IndexOutOfBoundsException e) {
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   675
                            if (offset >= 0 && offset + t.size < bufSize)
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   676
                                throw new RuntimeException
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   677
                                        ("type = " + t + ", offset = " + offset + ", bufSize = " + bufSize, e);
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   678
                            threw++;
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   679
                        }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   680
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   681
                        // If offset is aligned access using the view
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   682
                        if (offset % t.size == 0) {
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   683
                            checks = 8;
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   684
                            int viewOffset = offset / t.size;
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   685
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   686
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   687
                            try {
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   688
                                asViewPutOne(viewBuf, t, viewOffset);
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   689
                            }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   690
                            catch (IndexOutOfBoundsException e) {
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   691
                                if (offset >= 0 && offset + t.size < bufSize)
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   692
                                    throw new RuntimeException
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   693
                                            ("type = " + t + ", offset = " + offset + ", bufSize = " + bufSize, e);
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   694
                                threw++;
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   695
                            }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   696
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   697
                            try {
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   698
                                asViewGetOne(viewBuf, t, viewOffset);
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   699
                            }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   700
                            catch (IndexOutOfBoundsException e) {
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   701
                                if (offset >= 0 && offset + t.size < bufSize)
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   702
                                    throw new RuntimeException
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   703
                                            ("type = " + t + ", offset = " + offset + ", bufSize = " + bufSize, e);
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   704
                                threw++;
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   705
                            }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   706
                        }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   707
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   708
                        if (threw == 0) {
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   709
                            // Make sure that we should not have thrown.
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   710
                            if (offset < 0 || offset + t.size > bufSize) {
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   711
                                throw new RuntimeException
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   712
                                        ("should have thrown but did not, type = " + t
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   713
                                         + ", offset = " + offset + ", bufSize = " + bufSize);
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   714
                            }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   715
                        }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   716
                        else if (threw != checks) {
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   717
                            // If one of the {get,put} operations threw
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   718
                            // due to an invalid offset then all four of
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   719
                            // them should have thrown.
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   720
                            throw new RuntimeException
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   721
                                    ("should have thrown but at least one did not, type = " + t
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   722
                                     + ", offset = " + offset + ", bufSize = " + bufSize);
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   723
                        }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   724
                    }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   725
                    catch (Throwable th) {
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   726
                        throw new RuntimeException
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   727
                                ("unexpected throw: type  = " + t + ", offset = " + offset + ", bufSize = " + bufSize, th);
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   728
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   729
                    }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   730
                }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   731
            }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   732
        }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   733
    }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   734
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   735
    public void run() {
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   736
        checkBoundaryConditions();
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   737
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   738
        for (int i = 0; i < data.capacity(); i += 8) {
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   739
            data.putLong(i, random.nextLong());
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   740
        }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   741
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   742
        for (int i = 0; i < iterations; i++) {
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   743
            step(random);
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   744
        }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   745
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   746
        if (!Arrays.equals(data.actualArray(), data.expectedArray())) {
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   747
            throw new RuntimeException();
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   748
        }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   749
    }
69932114a0fc 8151163: All Buffer implementations should leverage Unsafe unaligned accessors
psandoz
parents:
diff changeset
   750
}