Path: blob/master/test/jdk/java/nio/MappedByteBuffer/ForceViews.java
41152 views
/*1* Copyright (c) 2021, 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/*24* @test25* @bug 483371926* @summary Verify MappedByteBuffer force on compact, duplicate, and slice views27* @run testng ForceViews28*/29import java.io.IOException;30import java.nio.ByteBuffer;31import java.nio.MappedByteBuffer;32import java.nio.ReadOnlyBufferException;33import java.nio.channels.FileChannel;34import static java.nio.channels.FileChannel.MapMode.*;35import java.nio.file.Path;36import static java.nio.file.StandardOpenOption.*;37import java.util.function.BiFunction;3839import org.testng.Assert;40import org.testng.annotations.AfterTest;41import org.testng.annotations.BeforeTest;42import org.testng.annotations.DataProvider;43import org.testng.annotations.Test;4445public class ForceViews {4647static record Segment(int position, int length) {}4849private FileChannel fc;5051@BeforeTest(alwaysRun=true)52public void openChannel() throws IOException {53Path file = Path.of(System.getProperty("test.src", "."), "junk");54fc = FileChannel.open(file, CREATE_NEW, READ, WRITE, DELETE_ON_CLOSE);55ByteBuffer buf = ByteBuffer.wrap(new byte[1024]);56fc.write(buf);57fc.position(0);58}5960@AfterTest(alwaysRun=true)61public void closeChannel() throws IOException {62fc.close();63}6465@DataProvider66public Object[][] provider() throws IOException {67BiFunction<MappedByteBuffer,Segment,MappedByteBuffer> absSlice =68(m, s) -> { return m.slice(s.position, s.length); };69BiFunction<MappedByteBuffer,Segment,MappedByteBuffer> relSlice =70(m, s) -> { m.position(s.position); m.limit(s.position + s.length);71return m.slice(); };72BiFunction<MappedByteBuffer,Segment,MappedByteBuffer> duplicate=73(m, s) -> { return m.duplicate(); };74BiFunction<MappedByteBuffer,Segment,MappedByteBuffer> compact =75(m, s) -> { return m.compact(); };7677Object[][] result = new Object[][] {78{"Absolute slice", fc, 256, 512, 128, 128, 32, 32, absSlice},79{"Relative slice", fc, 256, 512, 0, 128, 32, 32, relSlice},80{"Duplicate", fc, 256, 512, 0, 256, 32, 32, duplicate},81{"Compact", fc, 256, 512, 0, 256, 32, 32, compact}82};8384return result;85}8687@Test(dataProvider = "provider")88public void test(String tst, FileChannel fc, int mapPosition, int mapLength,89int sliceIndex, int sliceLength, int regionOffset, int regionLength,90BiFunction<MappedByteBuffer,Segment,MappedByteBuffer> f)91throws Exception {92MappedByteBuffer mbb = fc.map(READ_WRITE, mapPosition, mapLength);93mbb = f.apply(mbb, new Segment(sliceIndex, sliceLength));94for (int i = regionOffset; i < regionOffset + regionLength; i++) {95mbb.put(i, (byte)i);96}97mbb.force(regionOffset, regionOffset + regionLength);9899int fcPos = mapPosition + sliceIndex + regionOffset;100int mbbPos = regionOffset;101int length = regionLength;102103ByteBuffer buf = ByteBuffer.allocate(length);104fc.position(fcPos);105fc.read(buf);106for (int i = 0; i < length; i++) {107int fcVal = buf.get(i);108int mbbVal = mbb.get(mbbPos + i);109int val = regionOffset + i;110Assert.assertTrue(fcVal == val && mbbVal == val,111String.format("%s: i %d, fcVal %d, mbbVal %d, val %d",112tst, i, fcVal, mbbVal, val));113}114}115}116117118