1 /*
   2  * Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /* @test
  25  * @bug 8230665
  26  * @summary Test correct behavior of alignmentOffset()
  27  */
  28 
  29 import java.io.IOException;
  30 import java.nio.MappedByteBuffer;
  31 import java.nio.channels.FileChannel;
  32 import java.nio.file.Files;
  33 import java.nio.file.Path;
  34 
  35 public class AlignmentOffset {
  36 
  37     public static void main(String[] args) throws Throwable {
  38         test();
  39     }
  40 
  41     private static void test() throws IOException {
  42         MappedByteBuffer[] mbb = buffers();
  43         int failures = 0;
  44         for (MappedByteBuffer bb : mbb) {
  45             try {
  46                 int offset = bb.alignmentOffset(1, 4);
  47                 if (offset >= 0) {
  48                     System.out.println("PASSED");
  49                 } else {
  50                     System.err.println("FAILED "+offset);
  51                     failures++;
  52                 }
  53             } catch (UnsupportedOperationException e) {
  54                 System.out.println("Not applicable, UOE thrown: ");
  55             }
  56         }
  57         if (failures > 0) {
  58             throw new RuntimeException("Test failed");
  59         }
  60     }
  61 
  62     private static MappedByteBuffer[] buffers() throws IOException {
  63         return new MappedByteBuffer[]{
  64                 createMBB(new byte[]{0, 1, 2, 3}),
  65                 createMBB(new byte[]{0, 1, 2, -3,
  66                     45, 6, 7, 78, 3, -7, 6, 7, -128, 127}),
  67         };
  68     }
  69 
  70     private static MappedByteBuffer createMBB(byte[] contents)
  71         throws IOException {
  72         Path tempFile = Files.createTempFile("mbb", null);
  73         tempFile.toFile().deleteOnExit();
  74         Files.write(tempFile, contents);
  75         try (FileChannel fc = FileChannel.open(tempFile)) {
  76             MappedByteBuffer map =
  77                 fc.map(FileChannel.MapMode.READ_ONLY, 0, contents.length);
  78             map.load();
  79             return map;
  80         }
  81     }
  82 }