--- /dev/null 2020-01-10 16:22:15.000000000 -0800 +++ new/test/jdk/java/nio/MappedByteBuffer/AlignmentOffset.java 2020-01-10 16:22:15.000000000 -0800 @@ -0,0 +1,82 @@ +/* + * Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +/* @test + * @bug 8230665 + * @summary Test correct behavior of alignmentOffset() + */ + +import java.io.IOException; +import java.nio.MappedByteBuffer; +import java.nio.channels.FileChannel; +import java.nio.file.Files; +import java.nio.file.Path; + +public class AlignmentOffset { + + public static void main(String[] args) throws Throwable { + test(); + } + + private static void test() throws IOException { + MappedByteBuffer[] mbb = buffers(); + int failures = 0; + for (MappedByteBuffer bb : mbb) { + try { + int offset = bb.alignmentOffset(1, 4); + if (offset >= 0) { + System.out.println("PASSED"); + } else { + System.err.println("FAILED "+offset); + failures++; + } + } catch (UnsupportedOperationException e) { + System.out.println("Not applicable, UOE thrown: "); + } + } + if (failures > 0) { + throw new RuntimeException("Test failed"); + } + } + + private static MappedByteBuffer[] buffers() throws IOException { + return new MappedByteBuffer[]{ + createMBB(new byte[]{0, 1, 2, 3}), + createMBB(new byte[]{0, 1, 2, -3, + 45, 6, 7, 78, 3, -7, 6, 7, -128, 127}), + }; + } + + private static MappedByteBuffer createMBB(byte[] contents) + throws IOException { + Path tempFile = Files.createTempFile("mbb", null); + tempFile.toFile().deleteOnExit(); + Files.write(tempFile, contents); + try (FileChannel fc = FileChannel.open(tempFile)) { + MappedByteBuffer map = + fc.map(FileChannel.MapMode.READ_ONLY, 0, contents.length); + map.load(); + return map; + } + } +}