jdk/test/java/nio/Buffer/Order.java
author bpb
Fri, 24 Jul 2015 11:52:30 -0700
changeset 31873 87b015c2cd36
parent 31723 2e16a59cc5cb
child 37913 3cc95b690353
permissions -rw-r--r--
8065556: (bf) Buffer.position and other methods should include detail in IAE Summary: Add messages to IAEs which have none. Reviewed-by: alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
31723
2e16a59cc5cb 8065570: (bf spec) ByteBuffer.slice() should make it clear that the initial order is BIG_ENDIAN
bpb
parents: 5506
diff changeset
     2
 * Copyright (c) 2001, 2015, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/* @test
31723
2e16a59cc5cb 8065570: (bf spec) ByteBuffer.slice() should make it clear that the initial order is BIG_ENDIAN
bpb
parents: 5506
diff changeset
    25
 * @bug 8065570
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @summary Unit test for X-Buffer.order methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.nio.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
public class Order {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
    static final ByteOrder be = ByteOrder.BIG_ENDIAN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
    static final ByteOrder le = ByteOrder.LITTLE_ENDIAN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
    static final ByteOrder nord = ByteOrder.nativeOrder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
31723
2e16a59cc5cb 8065570: (bf spec) ByteBuffer.slice() should make it clear that the initial order is BIG_ENDIAN
bpb
parents: 5506
diff changeset
    38
    protected static final int LENGTH = 16;
2e16a59cc5cb 8065570: (bf spec) ByteBuffer.slice() should make it clear that the initial order is BIG_ENDIAN
bpb
parents: 5506
diff changeset
    39
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    static void ck(ByteOrder ord, ByteOrder expected) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
        if (ord != expected)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
            throw new RuntimeException("Got " + ord
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
                                       + ", expected " + expected);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    static void ckViews(ByteBuffer bb, ByteOrder ord) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        ck(bb.asCharBuffer().order(), bb.order());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        ck(bb.asIntBuffer().order(), bb.order());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        ck(bb.asLongBuffer().order(), bb.order());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        ck(bb.asFloatBuffer().order(), bb.order());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        ck(bb.asDoubleBuffer().order(), bb.order());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    static void ckByteBuffer(ByteBuffer bb) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        ckViews(bb, bb.order());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        bb.order(be);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        ckViews(bb, be);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        bb.order(le);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        ckViews(bb, le);
31723
2e16a59cc5cb 8065570: (bf spec) ByteBuffer.slice() should make it clear that the initial order is BIG_ENDIAN
bpb
parents: 5506
diff changeset
    60
2e16a59cc5cb 8065570: (bf spec) ByteBuffer.slice() should make it clear that the initial order is BIG_ENDIAN
bpb
parents: 5506
diff changeset
    61
        if (bb.hasArray()) {
2e16a59cc5cb 8065570: (bf spec) ByteBuffer.slice() should make it clear that the initial order is BIG_ENDIAN
bpb
parents: 5506
diff changeset
    62
            byte[] array = bb.array();
2e16a59cc5cb 8065570: (bf spec) ByteBuffer.slice() should make it clear that the initial order is BIG_ENDIAN
bpb
parents: 5506
diff changeset
    63
            ck(ByteBuffer.wrap(array, LENGTH/2, LENGTH/2).order(), be);
2e16a59cc5cb 8065570: (bf spec) ByteBuffer.slice() should make it clear that the initial order is BIG_ENDIAN
bpb
parents: 5506
diff changeset
    64
            ck(ByteBuffer.wrap(array).order(), be);
2e16a59cc5cb 8065570: (bf spec) ByteBuffer.slice() should make it clear that the initial order is BIG_ENDIAN
bpb
parents: 5506
diff changeset
    65
            ck(bb.asReadOnlyBuffer().order(), be);
2e16a59cc5cb 8065570: (bf spec) ByteBuffer.slice() should make it clear that the initial order is BIG_ENDIAN
bpb
parents: 5506
diff changeset
    66
            ck(bb.duplicate().order(), be);
2e16a59cc5cb 8065570: (bf spec) ByteBuffer.slice() should make it clear that the initial order is BIG_ENDIAN
bpb
parents: 5506
diff changeset
    67
            ck(bb.slice().order(), be);
2e16a59cc5cb 8065570: (bf spec) ByteBuffer.slice() should make it clear that the initial order is BIG_ENDIAN
bpb
parents: 5506
diff changeset
    68
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    public static void main(String args[]) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
31723
2e16a59cc5cb 8065570: (bf spec) ByteBuffer.slice() should make it clear that the initial order is BIG_ENDIAN
bpb
parents: 5506
diff changeset
    73
        ck(ByteBuffer.allocate(LENGTH).order(), be);
2e16a59cc5cb 8065570: (bf spec) ByteBuffer.slice() should make it clear that the initial order is BIG_ENDIAN
bpb
parents: 5506
diff changeset
    74
        ck(ByteBuffer.allocateDirect(LENGTH).order(), be);
2e16a59cc5cb 8065570: (bf spec) ByteBuffer.slice() should make it clear that the initial order is BIG_ENDIAN
bpb
parents: 5506
diff changeset
    75
        ck(ByteBuffer.allocate(LENGTH).order(be).order(), be);
2e16a59cc5cb 8065570: (bf spec) ByteBuffer.slice() should make it clear that the initial order is BIG_ENDIAN
bpb
parents: 5506
diff changeset
    76
        ck(ByteBuffer.allocate(LENGTH).order(le).order(), le);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
31723
2e16a59cc5cb 8065570: (bf spec) ByteBuffer.slice() should make it clear that the initial order is BIG_ENDIAN
bpb
parents: 5506
diff changeset
    78
        ckByteBuffer(ByteBuffer.allocate(LENGTH));
2e16a59cc5cb 8065570: (bf spec) ByteBuffer.slice() should make it clear that the initial order is BIG_ENDIAN
bpb
parents: 5506
diff changeset
    79
        ckByteBuffer(ByteBuffer.allocateDirect(LENGTH));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
31723
2e16a59cc5cb 8065570: (bf spec) ByteBuffer.slice() should make it clear that the initial order is BIG_ENDIAN
bpb
parents: 5506
diff changeset
    81
        OrderChar.ckCharBuffer();
2e16a59cc5cb 8065570: (bf spec) ByteBuffer.slice() should make it clear that the initial order is BIG_ENDIAN
bpb
parents: 5506
diff changeset
    82
        OrderShort.ckShortBuffer();
2e16a59cc5cb 8065570: (bf spec) ByteBuffer.slice() should make it clear that the initial order is BIG_ENDIAN
bpb
parents: 5506
diff changeset
    83
        OrderInt.ckIntBuffer();
2e16a59cc5cb 8065570: (bf spec) ByteBuffer.slice() should make it clear that the initial order is BIG_ENDIAN
bpb
parents: 5506
diff changeset
    84
        OrderLong.ckLongBuffer();
2e16a59cc5cb 8065570: (bf spec) ByteBuffer.slice() should make it clear that the initial order is BIG_ENDIAN
bpb
parents: 5506
diff changeset
    85
        OrderFloat.ckFloatBuffer();
2e16a59cc5cb 8065570: (bf spec) ByteBuffer.slice() should make it clear that the initial order is BIG_ENDIAN
bpb
parents: 5506
diff changeset
    86
        OrderDouble.ckDoubleBuffer();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
}