Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/nio/channels/FileChannel/TryLock.java
41154 views
1
/*
2
* Copyright (c) 2001, 2010, 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
/**
25
* @test
26
* @bug 4486154 4495729
27
* @summary The FileChannel file locking
28
*/
29
30
import java.io.*;
31
import java.nio.channels.*;
32
import java.nio.*;
33
34
/**
35
* Testing FileChannel's locking methods.
36
*/
37
38
public class TryLock {
39
40
public static void main(String[] args) throws Exception {
41
test1(true, true);
42
test1(false, true);
43
test1(true, false);
44
test1(false, false);
45
46
test2(true, true);
47
test2(false, true);
48
test2(true, false);
49
test2(false, false);
50
51
test3(true, true);
52
test3(false, true);
53
test3(true, false);
54
test3(false, false);
55
}
56
57
public static void test1(boolean shared, boolean trylock) throws Exception {
58
File testFile = File.createTempFile("test1", null);
59
FileInputStream fis = new FileInputStream(testFile);
60
FileChannel fc = fis.getChannel();
61
FileLock fl = null;
62
try {
63
if (trylock)
64
fl = fc.tryLock(0, fc.size(), shared);
65
else
66
fl = fc.lock(0, fc.size(), shared);
67
if (!shared)
68
throw new RuntimeException("No exception thrown for test1");
69
} catch (NonWritableChannelException e) {
70
if (shared)
71
throw new RuntimeException("Exception thrown for wrong case test1");
72
} finally {
73
if (fl != null)
74
fl.release();
75
fc.close();
76
testFile.delete();
77
}
78
}
79
80
public static void test2(boolean shared, boolean trylock) throws Exception {
81
File testFile = File.createTempFile("test2", null);
82
FileOutputStream fis = new FileOutputStream(testFile);
83
FileChannel fc = fis.getChannel();
84
FileLock fl = null;
85
try {
86
if (trylock)
87
fl = fc.tryLock(0, fc.size(), shared);
88
else
89
fl = fc.lock(0, fc.size(), shared);
90
if (shared)
91
throw new RuntimeException("No exception thrown for test2");
92
} catch (NonReadableChannelException e) {
93
if (!shared)
94
throw new RuntimeException("Exception thrown incorrectly for test2");
95
} finally {
96
if (fl != null)
97
fl.release();
98
fc.close();
99
testFile.delete();
100
}
101
}
102
103
public static void test3(boolean shared, boolean trylock) throws Exception {
104
File testFile = File.createTempFile("test3", null);
105
RandomAccessFile fis = new RandomAccessFile(testFile, "rw");
106
FileChannel fc = fis.getChannel();
107
try {
108
FileLock fl = null;
109
if (trylock)
110
fl = fc.tryLock(0, fc.size(), shared);
111
else
112
fl = fc.lock(0, fc.size(), shared);
113
fl.release();
114
} finally {
115
fc.close();
116
testFile.delete();
117
}
118
}
119
}
120
121