Path: blob/master/test/jdk/java/nio/Buffer/Order.java
41149 views
/*1* Copyright (c) 2001, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24* @bug 806557025* @summary Unit test for X-Buffer.order methods26*/2728import java.nio.*;293031public class Order {3233static final ByteOrder be = ByteOrder.BIG_ENDIAN;34static final ByteOrder le = ByteOrder.LITTLE_ENDIAN;35static final ByteOrder nord = ByteOrder.nativeOrder();3637protected static final int LENGTH = 16;3839static void ck(ByteOrder ord, ByteOrder expected) {40if (ord != expected)41throw new RuntimeException("Got " + ord + ", expected " + expected);42}4344private static void ckViews(ByteBuffer bb) {45ck(bb.asCharBuffer().order(), bb.order());46ck(bb.asShortBuffer().order(), bb.order());47ck(bb.asIntBuffer().order(), bb.order());48ck(bb.asLongBuffer().order(), bb.order());49ck(bb.asFloatBuffer().order(), bb.order());50ck(bb.asDoubleBuffer().order(), bb.order());51}5253private static void ckCopyViews(ByteBuffer bb) {54ck(bb.asReadOnlyBuffer().order(), be);55ck(bb.duplicate().order(), be);56ck(bb.slice().order(), be);57}5859private static void ckByteBuffer(ByteBuffer bb) {60ckViews(bb);61ckCopyViews(bb);62bb.order(be);63ckViews(bb);64ckCopyViews(bb);65bb.order(le);66ckViews(bb);67ckCopyViews(bb);68}6970public static void main(String args[]) throws Exception {7172ck(ByteBuffer.wrap(new byte[LENGTH], LENGTH/2, LENGTH/2).order(), be);73ck(ByteBuffer.wrap(new byte[LENGTH]).order(), be);74ck(ByteBuffer.wrap(new byte[LENGTH], LENGTH/2, LENGTH/2).order(be).order(), be);75ck(ByteBuffer.wrap(new byte[LENGTH]).order(be).order(), be);76ck(ByteBuffer.wrap(new byte[LENGTH], LENGTH/2, LENGTH/2).order(le).order(), le);77ck(ByteBuffer.wrap(new byte[LENGTH]).order(le).order(), le);78ck(ByteBuffer.allocate(LENGTH).order(), be);79ck(ByteBuffer.allocateDirect(LENGTH).order(), be);80ck(ByteBuffer.allocate(LENGTH).order(be).order(), be);81ck(ByteBuffer.allocate(LENGTH).order(le).order(), le);82ck(ByteBuffer.allocateDirect(LENGTH).order(be).order(), be);83ck(ByteBuffer.allocateDirect(LENGTH).order(le).order(), le);8485ckByteBuffer(ByteBuffer.allocate(LENGTH));86ckByteBuffer(ByteBuffer.allocateDirect(LENGTH));8788OrderChar.ckCharBuffer();89OrderShort.ckShortBuffer();90OrderInt.ckIntBuffer();91OrderLong.ckLongBuffer();92OrderFloat.ckFloatBuffer();93OrderDouble.ckDoubleBuffer();94}95}969798