Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/nio/channels/FileChannel/MapTest.java
38828 views
/*1* Copyright (c) 2000, 2012, 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 4429043 800218025* @summary Test file mapping with FileChannel26* @run main/othervm MapTest27* @key randomness28*/2930import java.io.*;31import java.nio.MappedByteBuffer;32import java.nio.channels.*;33import java.nio.channels.FileChannel.MapMode;34import java.nio.file.Files;35import static java.nio.file.StandardOpenOption.*;36import static java.nio.charset.StandardCharsets.*;37import java.util.Random;383940/**41* Testing FileChannel's mapping capabilities.42*/4344public class MapTest {4546private static PrintStream out = System.out;47private static PrintStream err = System.err;4849private static Random generator = new Random();5051private static int CHARS_PER_LINE = File.separatorChar == '/' ? 5 : 6;5253private static File blah;5455public static void main(String[] args) throws Exception {56blah = File.createTempFile("blah", null);57blah.deleteOnExit();58initTestFile(blah);59try {60out.println("Test file " + blah + " initialized");61testZero();62out.println("Zero size: OK");63testRead();64out.println("Read: OK");65testWrite();66out.println("Write: OK");67testHighOffset();68out.println("High offset: OK");69testExceptions();70out.println("Exceptions: OK");71} finally {72blah.delete();73}74}7576/**77* Creates file blah:78* 000079* 000180* 000281* 000382* .83* .84* .85* 399986*87* Blah extends beyond a single page of memory so that the88* ability to index into a file of multiple pages is tested.89*/90private static void initTestFile(File blah) throws Exception {91try (BufferedWriter writer = Files.newBufferedWriter(blah.toPath(), ISO_8859_1)) {92for (int i=0; i<4000; i++) {93String number = new Integer(i).toString();94for (int h=0; h<4-number.length(); h++)95writer.write("0");96writer.write(""+i);97writer.newLine();98}99}100}101102/**103* Tests zero size file mapping104*/105private static void testZero() throws Exception {106try (FileInputStream fis = new FileInputStream(blah)) {107FileChannel fc = fis.getChannel();108MappedByteBuffer b = fc.map(MapMode.READ_ONLY, 0, 0);109}110}111112/**113* Maps blah file with a random offset and checks to see if read114* from the ByteBuffer gets the right line number115*/116private static void testRead() throws Exception {117StringBuilder sb = new StringBuilder();118sb.setLength(4);119120for (int x=0; x<1000; x++) {121try (FileInputStream fis = new FileInputStream(blah)) {122FileChannel fc = fis.getChannel();123124long offset = generator.nextInt(10000);125long expectedResult = offset / CHARS_PER_LINE;126offset = expectedResult * CHARS_PER_LINE;127128MappedByteBuffer b = fc.map(MapMode.READ_ONLY,129offset, 100);130131for (int i=0; i<4; i++) {132byte aByte = b.get(i);133sb.setCharAt(i, (char)aByte);134}135136int result = Integer.parseInt(sb.toString());137if (result != expectedResult) {138err.println("I expected "+expectedResult);139err.println("I got "+result);140throw new Exception("Read test failed");141}142}143}144}145146/**147* Maps blah file with a random offset and checks to see if data148* written out to the file can be read back in149*/150private static void testWrite() throws Exception {151StringBuilder sb = new StringBuilder();152sb.setLength(4);153154for (int x=0; x<1000; x++) {155try (RandomAccessFile raf = new RandomAccessFile(blah, "rw")) {156FileChannel fc = raf.getChannel();157158long offset = generator.nextInt(1000);159MappedByteBuffer b = fc.map(MapMode.READ_WRITE,160offset, 100);161162for (int i=0; i<4; i++) {163b.put(i, (byte)('0' + i));164}165166for (int i=0; i<4; i++) {167byte aByte = b.get(i);168sb.setCharAt(i, (char)aByte);169}170if (!sb.toString().equals("0123"))171throw new Exception("Write test failed");172}173}174}175176private static void testHighOffset() throws Exception {177StringBuilder sb = new StringBuilder();178sb.setLength(4);179180for (int x=0; x<1000; x++) {181try (RandomAccessFile raf = new RandomAccessFile(blah, "rw")) {182FileChannel fc = raf.getChannel();183long offset = 66000;184MappedByteBuffer b = fc.map(MapMode.READ_WRITE,185offset, 100);186}187}188}189190/**191* Test exceptions specified by map method192*/193private static void testExceptions() throws Exception {194// check exceptions when channel opened for read access195try (FileChannel fc = FileChannel.open(blah.toPath(), READ)) {196testExceptions(fc);197198checkException(fc, MapMode.READ_WRITE, 0L, fc.size(),199NonWritableChannelException.class);200201checkException(fc, MapMode.READ_WRITE, -1L, fc.size(),202NonWritableChannelException.class, IllegalArgumentException.class);203204checkException(fc, MapMode.READ_WRITE, 0L, -1L,205NonWritableChannelException.class, IllegalArgumentException.class);206207checkException(fc, MapMode.PRIVATE, 0L, fc.size(),208NonWritableChannelException.class);209210checkException(fc, MapMode.PRIVATE, -1L, fc.size(),211NonWritableChannelException.class, IllegalArgumentException.class);212213checkException(fc, MapMode.PRIVATE, 0L, -1L,214NonWritableChannelException.class, IllegalArgumentException.class);215}216217// check exceptions when channel opened for write access218try (FileChannel fc = FileChannel.open(blah.toPath(), WRITE)) {219testExceptions(fc);220221checkException(fc, MapMode.READ_ONLY, 0L, fc.size(),222NonReadableChannelException.class);223224checkException(fc, MapMode.READ_ONLY, -1L, fc.size(),225NonReadableChannelException.class, IllegalArgumentException.class);226227/*228* implementation/spec mismatch, these tests disabled for now229*/230//checkException(fc, MapMode.READ_WRITE, 0L, fc.size(),231// NonWritableChannelException.class);232//checkException(fc, MapMode.PRIVATE, 0L, fc.size(),233// NonWritableChannelException.class);234}235236// check exceptions when channel opened for read and write access237try (FileChannel fc = FileChannel.open(blah.toPath(), READ, WRITE)) {238testExceptions(fc);239}240}241242private static void testExceptions(FileChannel fc) throws IOException {243checkException(fc, null, 0L, fc.size(),244NullPointerException.class);245246checkException(fc, MapMode.READ_ONLY, -1L, fc.size(),247IllegalArgumentException.class);248249checkException(fc, null, -1L, fc.size(),250IllegalArgumentException.class, NullPointerException.class);251252checkException(fc, MapMode.READ_ONLY, 0L, -1L,253IllegalArgumentException.class);254255checkException(fc, null, 0L, -1L,256IllegalArgumentException.class, NullPointerException.class);257258checkException(fc, MapMode.READ_ONLY, 0L, Integer.MAX_VALUE + 1L,259IllegalArgumentException.class);260261checkException(fc, null, 0L, Integer.MAX_VALUE + 1L,262IllegalArgumentException.class, NullPointerException.class);263264checkException(fc, MapMode.READ_ONLY, Long.MAX_VALUE, 1L,265IllegalArgumentException.class);266267checkException(fc, null, Long.MAX_VALUE, 1L,268IllegalArgumentException.class, NullPointerException.class);269270}271272/**273* Checks that FileChannel map throws one of the expected exceptions274* when invoked with the given inputs.275*/276private static void checkException(FileChannel fc,277MapMode mode,278long position,279long size,280Class<?>... expected)281throws IOException282{283Exception exc = null;284try {285fc.map(mode, position, size);286} catch (Exception actual) {287exc = actual;288}289if (exc != null) {290for (Class<?> clazz: expected) {291if (clazz.isInstance(exc)) {292return;293}294}295}296System.err.println("Expected one of");297for (Class<?> clazz: expected) {298System.out.println(clazz);299}300if (exc == null) {301throw new RuntimeException("No expection thrown");302} else {303throw new RuntimeException("Unexpected exception thrown", exc);304}305}306}307308309