Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/security/provider/FileInputStreamPool/FileInputStreamPoolTest.java
41154 views
1
/*
2
* Copyright (c) 2014, 2016, 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 8047769
27
* @modules java.base/java.io:open
28
* java.base/java.lang.ref:open
29
* java.base/sun.security.provider:open
30
* @summary SecureRandom should be more frugal with file descriptors
31
*/
32
33
import java.io.File;
34
import java.io.FileDescriptor;
35
import java.io.FileInputStream;
36
import java.io.FileOutputStream;
37
import java.io.FilterInputStream;
38
import java.io.IOException;
39
import java.io.InputStream;
40
import java.lang.ref.Reference;
41
import java.lang.ref.ReferenceQueue;
42
import java.lang.ref.WeakReference;
43
import java.lang.reflect.Field;
44
import java.lang.reflect.InvocationTargetException;
45
import java.lang.reflect.Method;
46
import java.lang.reflect.UndeclaredThrowableException;
47
import java.util.Arrays;
48
import java.util.HashSet;
49
50
public class FileInputStreamPoolTest {
51
52
static final byte[] bytes = new byte[]{1, 2, 3, 4, 5, 6, 7, 8};
53
54
static FilterInputStream testCaching(File file) throws IOException {
55
InputStream in1 = TestProxy.FileInputStreamPool_getInputStream(file);
56
InputStream in2 = TestProxy.FileInputStreamPool_getInputStream(file);
57
assertTrue(in1 == in2,
58
"1st InputStream: " + in1 +
59
" is not same as 2nd: " + in2);
60
61
byte[] readBytes = new byte[bytes.length];
62
int nread = in1.read(readBytes);
63
assertTrue(bytes.length == nread,
64
"short read: " + nread +
65
" bytes of expected: " + bytes.length);
66
assertTrue(Arrays.equals(readBytes, bytes),
67
"readBytes: " + Arrays.toString(readBytes) +
68
" not equal to expected: " + Arrays.toString(bytes));
69
70
return (FilterInputStream)in1;
71
}
72
73
static void assertTrue(boolean test, String message) {
74
if (!test) {
75
throw new AssertionError(message);
76
}
77
}
78
79
static void processReferences(FilterInputStream in1) throws InterruptedException, IOException {
80
FileInputStream fis = TestProxy.FilterInputStream_getInField(in1);
81
FileDescriptor fd = fis.getFD();
82
System.out.printf("fis: %s, fd: %s%n", fis, fd);
83
// Prepare to wait for FD to be reclaimed
84
ReferenceQueue<Object> queue = new ReferenceQueue<>();
85
HashSet<Reference<?>> pending = new HashSet<>();
86
pending.add(new WeakReference<>(in1, queue));
87
pending.add(new WeakReference<>(fis, queue));
88
pending.add(new WeakReference<>(fd, queue));
89
90
Reference<?> r;
91
while (((r = queue.remove(10L)) != null)
92
|| !pending.isEmpty()) {
93
System.out.printf("r: %s, pending: %d%n", r, pending.size());
94
if (r != null) {
95
pending.remove(r);
96
} else {
97
fd = null;
98
fis = null;
99
in1 = null;
100
System.gc(); // attempt to reclaim the FD
101
}
102
Thread.sleep(10L);
103
}
104
Reference.reachabilityFence(fd);
105
Reference.reachabilityFence(fis);
106
Reference.reachabilityFence(in1);
107
}
108
109
public static void main(String[] args) throws Exception {
110
// 1st create temporary file
111
File file = File.createTempFile("test", ".dat");
112
try (AutoCloseable acf = () -> {
113
// On Windows, failure to delete file is probably a consequence
114
// of the file still being opened - so the test should fail.
115
assertTrue(file.delete(),
116
"Can't delete: " + file + " (is it still open?)");
117
}) {
118
try (FileOutputStream out = new FileOutputStream(file)) {
119
out.write(bytes);
120
}
121
122
// test caching 1st time
123
124
processReferences(testCaching(file));
125
126
// test caching 2nd time - this should only succeed if the stream
127
// is re-opened as a consequence of cleared WeakReference
128
129
processReferences(testCaching(file));
130
}
131
}
132
133
/**
134
* A proxy for (package)private static methods:
135
* sun.security.provider.FileInputStreamPool.getInputStream
136
* java.lang.ref.Reference.waitForReferenceProcessing
137
*/
138
static class TestProxy {
139
private static final Method getInputStreamMethod;
140
private static final Method waitForReferenceProcessingMethod;
141
private static final Field inField;
142
143
static {
144
try {
145
Class<?> fileInputStreamPoolClass =
146
Class.forName("sun.security.provider.FileInputStreamPool");
147
getInputStreamMethod =
148
fileInputStreamPoolClass.getDeclaredMethod(
149
"getInputStream", File.class);
150
getInputStreamMethod.setAccessible(true);
151
152
waitForReferenceProcessingMethod =
153
Reference.class.getDeclaredMethod("waitForReferenceProcessing");
154
waitForReferenceProcessingMethod.setAccessible(true);
155
156
inField = FilterInputStream.class.getDeclaredField("in");
157
inField.setAccessible(true);
158
} catch (Exception e) {
159
throw new Error(e);
160
}
161
}
162
163
static InputStream FileInputStreamPool_getInputStream(File file)
164
throws IOException {
165
try {
166
return (InputStream) getInputStreamMethod.invoke(null, file);
167
} catch (InvocationTargetException e) {
168
Throwable te = e.getTargetException();
169
if (te instanceof IOException) {
170
throw (IOException) te;
171
} else if (te instanceof RuntimeException) {
172
throw (RuntimeException) te;
173
} else if (te instanceof Error) {
174
throw (Error) te;
175
} else {
176
throw new UndeclaredThrowableException(te);
177
}
178
} catch (IllegalAccessException e) {
179
throw new RuntimeException(e);
180
}
181
}
182
183
static boolean Reference_waitForReferenceProcessing() {
184
try {
185
return (boolean) waitForReferenceProcessingMethod.invoke(null);
186
} catch (InvocationTargetException e) {
187
Throwable te = e.getTargetException();
188
if (te instanceof InterruptedException) {
189
return true;
190
} else if (te instanceof RuntimeException) {
191
throw (RuntimeException) te;
192
} else if (te instanceof Error) {
193
throw (Error) te;
194
} else {
195
throw new UndeclaredThrowableException(te);
196
}
197
} catch (IllegalAccessException e) {
198
throw new RuntimeException(e);
199
}
200
}
201
202
static FileInputStream FilterInputStream_getInField(FilterInputStream fis) {
203
try {
204
return (FileInputStream) inField.get(fis);
205
} catch (IllegalAccessException e) {
206
throw new RuntimeException(e);
207
}
208
}
209
}
210
}
211
212