Skip to content

Commit

Permalink
Use Java 11 language features where possible (#466)
Browse files Browse the repository at this point in the history
  • Loading branch information
basil authored Sep 8, 2022
1 parent 9058ac4 commit 37febad
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 20 deletions.
8 changes: 2 additions & 6 deletions client/src/main/java/hudson/plugins/swarm/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,7 @@ private static void validateOptions(Options options) {
int oldPid;
try {
oldPid =
NumberUtils.toInt(
new String(Files.readAllBytes(pidFile), StandardCharsets.UTF_8),
0);
NumberUtils.toInt(Files.readString(pidFile, StandardCharsets.UTF_8), 0);
} catch (IOException e) {
throw new UncheckedIOException("Failed to read PID file " + pidFile, e);
}
Expand Down Expand Up @@ -146,9 +144,7 @@ private static void validateOptions(Options options) {
if (options.password == null && options.passwordFile != null) {
try {
options.password =
new String(
Files.readAllBytes(Paths.get(options.passwordFile)),
StandardCharsets.UTF_8)
Files.readString(Paths.get(options.passwordFile), StandardCharsets.UTF_8)
.trim();
} catch (IOException e) {
throw new UncheckedIOException("Failed to read password from file", e);
Expand Down
13 changes: 4 additions & 9 deletions client/src/main/java/hudson/plugins/swarm/LabelFileWatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,7 @@ public LabelFileWatcher(URL url, Options options, String name, String... args)
this.url = url;
this.options = options;
this.name = name;
this.labels =
new String(
Files.readAllBytes(Paths.get(options.labelsFile)), StandardCharsets.UTF_8);
this.labels = Files.readString(Paths.get(options.labelsFile), StandardCharsets.UTF_8);
this.args = args;
logger.config("Labels loaded: " + labels);
}
Expand Down Expand Up @@ -242,9 +240,7 @@ public void run() {
}
try {
sTempLabels =
new String(
Files.readAllBytes(Paths.get(options.labelsFile)),
StandardCharsets.UTF_8);
Files.readString(Paths.get(options.labelsFile), StandardCharsets.UTF_8);
if (sTempLabels.equalsIgnoreCase(labels)) {
logger.log(
Level.FINEST,
Expand All @@ -255,9 +251,8 @@ public void run() {
// through the plugin APIs
softLabelUpdate(sTempLabels);
labels =
new String(
Files.readAllBytes(Paths.get(options.labelsFile)),
StandardCharsets.UTF_8);
Files.readString(
Paths.get(options.labelsFile), StandardCharsets.UTF_8);
} catch (SoftLabelUpdateException e) {
// if we're unable to
logger.log(
Expand Down
9 changes: 4 additions & 5 deletions client/src/main/java/hudson/plugins/swarm/SwarmClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,7 @@ public SwarmClient(Options options) {
logger.info("Loading labels from " + options.labelsFile + "...");
try {
String labels =
new String(
Files.readAllBytes(Paths.get(options.labelsFile)),
StandardCharsets.UTF_8);
Files.readString(Paths.get(options.labelsFile), StandardCharsets.UTF_8);
options.labels.addAll(Arrays.asList(labels.trim().split("\\s+")));
logger.info("Labels found in file: " + labels);
logger.info(
Expand Down Expand Up @@ -355,7 +353,8 @@ private static synchronized Crumb getCsrfCrumb(
url
+ "crumbIssuer/api/xml?xpath="
+ URLEncoder.encode(
"concat(//crumbRequestField,\":\",//crumb)", "UTF-8"));
"concat(//crumbRequestField,\":\",//crumb)",
StandardCharsets.UTF_8));
try (CloseableHttpResponse response = client.execute(httpGet, context)) {
if (response.getCode() != HttpStatus.SC_OK) {
logger.log(
Expand Down Expand Up @@ -589,7 +588,7 @@ static synchronized void postLabelAppend(
private static synchronized String encode(String value) throws UnsupportedEncodingException {
logger.finer("encode() invoked");

return URLEncoder.encode(value, "UTF-8");
return URLEncoder.encode(value, StandardCharsets.UTF_8);
}

private static synchronized String param(String name, String value)
Expand Down

0 comments on commit 37febad

Please sign in to comment.