Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.base/linux/classes/jdk/internal/platform/CgroupUtil.java
40948 views
1
/*
2
* Copyright (c) 2020, Red Hat Inc.
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. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
10
*
11
* This code is distributed in the hope that it will be useful, but WITHOUT
12
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
* version 2 for more details (a copy is included in the LICENSE file that
15
* accompanied this code).
16
*
17
* You should have received a copy of the GNU General Public License version
18
* 2 along with this work; if not, write to the Free Software Foundation,
19
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20
*
21
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
23
* questions.
24
*/
25
26
package jdk.internal.platform;
27
28
import java.io.BufferedReader;
29
import java.io.IOException;
30
import java.io.UncheckedIOException;
31
import java.nio.file.Files;
32
import java.nio.file.Path;
33
import java.nio.file.Paths;
34
import java.security.AccessController;
35
import java.security.PrivilegedActionException;
36
import java.security.PrivilegedExceptionAction;
37
import java.util.List;
38
import java.util.stream.Stream;
39
40
public final class CgroupUtil {
41
42
@SuppressWarnings("removal")
43
public static Stream<String> readFilePrivileged(Path path) throws IOException {
44
try {
45
PrivilegedExceptionAction<Stream<String>> pea = () -> Files.lines(path);
46
return AccessController.doPrivileged(pea);
47
} catch (PrivilegedActionException e) {
48
unwrapIOExceptionAndRethrow(e);
49
throw new InternalError(e.getCause());
50
} catch (UncheckedIOException e) {
51
throw e.getCause();
52
}
53
}
54
55
static void unwrapIOExceptionAndRethrow(PrivilegedActionException pae) throws IOException {
56
Throwable x = pae.getCause();
57
if (x instanceof IOException)
58
throw (IOException) x;
59
if (x instanceof RuntimeException)
60
throw (RuntimeException) x;
61
if (x instanceof Error)
62
throw (Error) x;
63
}
64
65
static String readStringValue(CgroupSubsystemController controller, String param) throws IOException {
66
PrivilegedExceptionAction<BufferedReader> pea = () ->
67
Files.newBufferedReader(Paths.get(controller.path(), param));
68
try (@SuppressWarnings("removal") BufferedReader bufferedReader =
69
AccessController.doPrivileged(pea)) {
70
String line = bufferedReader.readLine();
71
return line;
72
} catch (PrivilegedActionException e) {
73
unwrapIOExceptionAndRethrow(e);
74
throw new InternalError(e.getCause());
75
} catch (UncheckedIOException e) {
76
throw e.getCause();
77
}
78
}
79
80
@SuppressWarnings("removal")
81
public static List<String> readAllLinesPrivileged(Path path) throws IOException {
82
try {
83
PrivilegedExceptionAction<List<String>> pea = () -> Files.readAllLines(path);
84
return AccessController.doPrivileged(pea);
85
} catch (PrivilegedActionException e) {
86
unwrapIOExceptionAndRethrow(e);
87
throw new InternalError(e.getCause());
88
} catch (UncheckedIOException e) {
89
throw e.getCause();
90
}
91
}
92
}
93
94