Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/nio/MappedByteBuffer/Truncate.java
41149 views
1
/*
2
* Copyright (c) 2010, 2012, 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 6934977
26
* @summary Test MappedByteBuffer operations after mapped bye buffer becomes
27
* inaccessible
28
* @run main/othervm Truncate
29
*/
30
31
import java.io.*;
32
import java.nio.*;
33
import java.nio.channels.*;
34
import java.util.concurrent.Callable;
35
36
public class Truncate {
37
38
static final long INITIAL_FILE_SIZE = 32000L;
39
static final long TRUNCATED_FILE_SIZE = 512L;
40
41
public static void main(String[] args) throws Exception {
42
File blah = File.createTempFile("blah", null);
43
blah.deleteOnExit();
44
45
final FileChannel fc = new RandomAccessFile(blah, "rw").getChannel();
46
fc.position(INITIAL_FILE_SIZE).write(ByteBuffer.wrap("THE END".getBytes()));
47
final MappedByteBuffer mbb =
48
fc.map(FileChannel.MapMode.READ_WRITE, 0, fc.size());
49
boolean truncated;
50
try {
51
fc.truncate(TRUNCATED_FILE_SIZE);
52
truncated = true;
53
} catch (IOException ioe) {
54
// probably on Windows where a file cannot be truncated when
55
// there is a file mapping.
56
truncated = false;
57
}
58
if (truncated) {
59
// Test 1: access region that is no longer accessible
60
execute(new Callable<Void>() {
61
public Void call() {
62
mbb.get((int)TRUNCATED_FILE_SIZE + 1);
63
mbb.put((int)TRUNCATED_FILE_SIZE + 2, (byte)123);
64
return null;
65
}
66
});
67
// Test 2: load buffer into memory
68
execute(new Callable<Void>() {
69
public Void call() throws IOException {
70
mbb.load();
71
return null;
72
}
73
});
74
}
75
fc.close();
76
}
77
78
// Runs the given task in its own thread. If operating correcting the
79
// the thread will terminate with an InternalError as the mapped buffer
80
// is inaccessible.
81
static void execute(final Callable<?> c) {
82
Runnable r = new Runnable() {
83
public void run() {
84
try {
85
Object ignore = c.call();
86
} catch (Exception ignore) {
87
}
88
}
89
};
90
Thread t = new Thread(r);
91
t.setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
92
public void uncaughtException(Thread t, Throwable e) {
93
e.printStackTrace();
94
}
95
});
96
t.start();
97
try { t.join(); } catch (InterruptedException ignore) { }
98
}
99
}
100
101