Path: blob/master/test/jdk/java/nio/MappedByteBuffer/Truncate.java
41149 views
/*1* Copyright (c) 2010, 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 693497725* @summary Test MappedByteBuffer operations after mapped bye buffer becomes26* inaccessible27* @run main/othervm Truncate28*/2930import java.io.*;31import java.nio.*;32import java.nio.channels.*;33import java.util.concurrent.Callable;3435public class Truncate {3637static final long INITIAL_FILE_SIZE = 32000L;38static final long TRUNCATED_FILE_SIZE = 512L;3940public static void main(String[] args) throws Exception {41File blah = File.createTempFile("blah", null);42blah.deleteOnExit();4344final FileChannel fc = new RandomAccessFile(blah, "rw").getChannel();45fc.position(INITIAL_FILE_SIZE).write(ByteBuffer.wrap("THE END".getBytes()));46final MappedByteBuffer mbb =47fc.map(FileChannel.MapMode.READ_WRITE, 0, fc.size());48boolean truncated;49try {50fc.truncate(TRUNCATED_FILE_SIZE);51truncated = true;52} catch (IOException ioe) {53// probably on Windows where a file cannot be truncated when54// there is a file mapping.55truncated = false;56}57if (truncated) {58// Test 1: access region that is no longer accessible59execute(new Callable<Void>() {60public Void call() {61mbb.get((int)TRUNCATED_FILE_SIZE + 1);62mbb.put((int)TRUNCATED_FILE_SIZE + 2, (byte)123);63return null;64}65});66// Test 2: load buffer into memory67execute(new Callable<Void>() {68public Void call() throws IOException {69mbb.load();70return null;71}72});73}74fc.close();75}7677// Runs the given task in its own thread. If operating correcting the78// the thread will terminate with an InternalError as the mapped buffer79// is inaccessible.80static void execute(final Callable<?> c) {81Runnable r = new Runnable() {82public void run() {83try {84Object ignore = c.call();85} catch (Exception ignore) {86}87}88};89Thread t = new Thread(r);90t.setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {91public void uncaughtException(Thread t, Throwable e) {92e.printStackTrace();93}94});95t.start();96try { t.join(); } catch (InterruptedException ignore) { }97}98}99100101