1 /* 2 * Copyright (C) 2018 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 import java.io.File; 18 import java.io.RandomAccessFile; 19 import java.nio.ByteBuffer; 20 import java.nio.MappedByteBuffer; 21 import java.nio.channels.FileChannel; 22 23 public class Main { assertEquals(int expected, int actual)24 public static void assertEquals(int expected, int actual) { 25 if (expected != actual) { 26 throw new Error("Assertion failed: " + expected + " != " + actual); 27 } 28 } 29 testRelativePositions(ByteBuffer b)30 private static void testRelativePositions(ByteBuffer b) throws Exception { 31 // This goes into Memory.pokeByte(), which is an intrinsic that has 32 // kWriteSideEffects. Stores before this call need to be kept. 33 b.put((byte) 0); 34 assertEquals(1, b.position()); 35 } 36 allocateMapped(int size)37 private static ByteBuffer allocateMapped(int size) throws Exception { 38 File f = File.createTempFile("mapped", "tmp"); 39 f.deleteOnExit(); 40 RandomAccessFile raf = new RandomAccessFile(f, "rw"); 41 raf.setLength(size); 42 FileChannel ch = raf.getChannel(); 43 MappedByteBuffer result = ch.map(FileChannel.MapMode.READ_WRITE, 0, size); 44 ch.close(); 45 return result; 46 } 47 testRelativePositionsMapped()48 public static void testRelativePositionsMapped() throws Exception { 49 testRelativePositions(allocateMapped(10)); 50 } 51 main(String[] args)52 public static void main(String[] args) throws Exception { 53 testRelativePositionsMapped(); 54 } 55 } 56