Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/nio/file/Files/CallWithInterruptSet.java
41153 views
1
/*
2
* Copyright (c) 2018, 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 8205612
27
* @run testng CallWithInterruptSet
28
* @summary Test invoking Files methods with the interrupt status set
29
*/
30
31
import java.io.InputStream;
32
import java.io.OutputStream;
33
import java.io.IOException;
34
import java.io.BufferedReader;
35
import java.io.BufferedWriter;
36
import java.io.Reader;
37
import java.io.Writer;
38
import java.nio.file.Files;
39
import java.nio.file.Path;
40
41
import org.testng.annotations.Test;
42
import static org.testng.Assert.*;
43
44
public class CallWithInterruptSet {
45
46
@Test
47
public void testReadAllBytes() throws Exception {
48
Path file = mkfile(100);
49
Thread.currentThread().interrupt();
50
try {
51
byte[] bytes = Files.readAllBytes(file);
52
assertTrue(bytes.length == 100);
53
} finally {
54
assertTrue(Thread.interrupted()); // clear interrupt
55
}
56
}
57
58
@Test
59
public void testInputStream() throws IOException {
60
Path file = mkfile(100);
61
Thread.currentThread().interrupt();
62
try (InputStream in = Files.newInputStream(file)) {
63
int n = in.read(new byte[10]);
64
assertTrue(n > 0);
65
} finally {
66
assertTrue(Thread.interrupted()); // clear interrupt
67
}
68
}
69
70
@Test
71
public void testOutputStream() throws Exception {
72
Path file = mkfile();
73
try (OutputStream out = Files.newOutputStream(file)) {
74
Thread.currentThread().interrupt();
75
out.write(new byte[10]);
76
} finally {
77
assertTrue(Thread.interrupted()); // clear interrupt
78
}
79
assertTrue(Files.size(file) == 10);
80
}
81
82
@Test
83
public void testReadString() throws Exception {
84
Path file = mkfile();
85
Files.writeString(file, "hello");
86
Thread.currentThread().interrupt();
87
try {
88
String msg = Files.readString(file);
89
assertEquals(msg, "hello");
90
} finally {
91
assertTrue(Thread.interrupted()); // clear interrupt
92
}
93
}
94
95
@Test
96
public void testWriteString() throws Exception {
97
Path file = mkfile();
98
Thread.currentThread().interrupt();
99
try {
100
Files.writeString(file, "hello");
101
} finally {
102
assertTrue(Thread.interrupted()); // clear interrupt
103
}
104
String msg = Files.readString(file);
105
assertEquals(msg, "hello");
106
}
107
108
@Test
109
public void testBufferedReader() throws Exception {
110
Path file = mkfile();
111
Files.writeString(file, "hello");
112
Thread.currentThread().interrupt();
113
try (BufferedReader reader = Files.newBufferedReader(file)) {
114
String msg = reader.readLine();
115
assertEquals(msg, "hello");
116
} finally {
117
assertTrue(Thread.interrupted()); // clear interrupt
118
}
119
}
120
121
@Test
122
public void testBufferedWriter() throws Exception {
123
Path file = mkfile();
124
Thread.currentThread().interrupt();
125
try (BufferedWriter writer = Files.newBufferedWriter(file)) {
126
writer.write("hello");
127
} finally {
128
assertTrue(Thread.interrupted()); // clear interrupt
129
}
130
String msg = Files.readString(file);
131
assertEquals(msg, "hello");
132
}
133
134
private Path mkfile() throws IOException {
135
return Files.createTempFile(Path.of("."), "tmp", "tmp");
136
}
137
138
private Path mkfile(int size) throws IOException {
139
return Files.write(mkfile(), new byte[size]);
140
}
141
142
}
143
144