Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 15 additions & 18 deletions src/java.base/linux/classes/jdk/internal/platform/CgroupUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,30 +26,22 @@
package jdk.internal.platform;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.AccessController;
import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;

public final class CgroupUtil {

@SuppressWarnings("removal")
public static Stream<String> readFilePrivileged(Path path) throws IOException {
try {
PrivilegedExceptionAction<Stream<String>> pea = () -> Files.lines(path);
return AccessController.doPrivileged(pea);
} catch (PrivilegedActionException e) {
unwrapIOExceptionAndRethrow(e);
throw new InternalError(e.getCause());
} catch (UncheckedIOException e) {
throw e.getCause();
}
return readAllLinesPrivileged(path).stream();
}

static void unwrapIOExceptionAndRethrow(PrivilegedActionException pae) throws IOException {
Expand All @@ -64,29 +56,34 @@ static void unwrapIOExceptionAndRethrow(PrivilegedActionException pae) throws IO

static String readStringValue(CgroupSubsystemController controller, String param) throws IOException {
PrivilegedExceptionAction<BufferedReader> pea = () ->
Files.newBufferedReader(Paths.get(controller.path(), param));
new BufferedReader(new FileReader(Paths.get(controller.path(), param).toString(), StandardCharsets.UTF_8));
try (@SuppressWarnings("removal") BufferedReader bufferedReader =
AccessController.doPrivileged(pea)) {
String line = bufferedReader.readLine();
return line;
} catch (PrivilegedActionException e) {
unwrapIOExceptionAndRethrow(e);
throw new InternalError(e.getCause());
} catch (UncheckedIOException e) {
throw e.getCause();
}
}

@SuppressWarnings("removal")
public static List<String> readAllLinesPrivileged(Path path) throws IOException {
try {
PrivilegedExceptionAction<List<String>> pea = () -> Files.readAllLines(path);
PrivilegedExceptionAction<List<String>> pea = () -> {
try (BufferedReader bufferedReader = new BufferedReader(new FileReader(path.toString(), StandardCharsets.UTF_8))) {
String line;
List<String> lines = new ArrayList<>();
while ((line = bufferedReader.readLine()) != null) {
lines.add(line);
}
return lines;
}
};
return AccessController.doPrivileged(pea);
} catch (PrivilegedActionException e) {
unwrapIOExceptionAndRethrow(e);
throw new InternalError(e.getCause());
} catch (UncheckedIOException e) {
throw e.getCause();
}
}
}