Skip to content

Commit

Permalink
Use Java 11 methods where possible (#719)
Browse files Browse the repository at this point in the history
  • Loading branch information
basil authored Jan 11, 2024
1 parent 5ad405b commit f8d6d2d
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 47 deletions.
17 changes: 1 addition & 16 deletions src/main/java/hudson/remoting/Checksum.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package hudson.remoting;

import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;

import java.io.ByteArrayInputStream;
Expand Down Expand Up @@ -79,26 +78,12 @@ static Checksum forFile(File file) throws IOException {
static Checksum forURL(URL url) throws IOException {
try {
MessageDigest md = MessageDigest.getInstance(JarLoaderImpl.DIGEST_ALGORITHM);
try(InputStream istream = url.openStream(); OutputStream ostream = new DigestOutputStream(new NullOutputStream(), md)) {
try(InputStream istream = url.openStream(); OutputStream ostream = new DigestOutputStream(OutputStream.nullOutputStream(), md)) {
Util.copy(istream, ostream);
return new Checksum(md.digest(), md.getDigestLength() / 8);
}
} catch (NoSuchAlgorithmException e) {
throw new AssertionError(e);
}
}

private static class NullOutputStream extends OutputStream {
@Override
public void write(int b) {
}

@Override
public void write(@NonNull byte[] b) {
}

@Override
public void write(@NonNull byte[] b, int off, int len) {
}
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package hudson.remoting;

import hudson.remoting.Channel.Mode;
import org.apache.commons.io.output.NullOutputStream;
import org.junit.Test;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.StreamCorruptedException;
import java.io.StringWriter;
Expand All @@ -28,7 +28,7 @@ public void exercise() throws Exception {
new ChannelBuilder("dummy",null)
.withMode(Mode.BINARY)
.withBaseLoader(getClass().getClassLoader())
.negotiate(new ByteArrayInputStream(payload), NullOutputStream.INSTANCE);
.negotiate(new ByteArrayInputStream(payload), OutputStream.nullOutputStream());

verify(ct);
}
Expand Down Expand Up @@ -63,7 +63,7 @@ public void blockingStreamShouldNotPreventDiagnosis() throws Exception {
new ChannelBuilder("dummy",null)
.withMode(Mode.BINARY)
.withBaseLoader(getClass().getClassLoader())
.negotiate(in, NullOutputStream.INSTANCE);
.negotiate(in, OutputStream.nullOutputStream());

verify(ct);
}
Expand Down
3 changes: 1 addition & 2 deletions src/test/java/hudson/remoting/PipeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
import java.util.Arrays;
import java.util.concurrent.ExecutionException;
import org.apache.commons.io.IOUtils;
import org.apache.commons.io.output.NullOutputStream;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
Expand Down Expand Up @@ -319,7 +318,7 @@ public void testQuickBurstWrite(ChannelRunner channelRunner) throws Exception {
private static class DevNullSink extends CallableBase<OutputStream, IOException> {
@Override
public OutputStream call() {
return new RemoteOutputStream(NullOutputStream.INSTANCE);
return new RemoteOutputStream(OutputStream.nullOutputStream());
}
private static final long serialVersionUID = 1L;
}
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/hudson/remoting/throughput/DumbReceiver.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package hudson.remoting.throughput;

import org.apache.commons.io.IOUtils;
import org.apache.commons.io.output.NullOutputStream;

import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

Expand All @@ -16,7 +16,7 @@ public static void main(String[] args) throws Exception {
System.out.println("Ready");
try (Socket s = ss.accept()) {
System.out.println("Accepted");
IOUtils.copy(s.getInputStream(), NullOutputStream.INSTANCE);
IOUtils.copy(s.getInputStream(), OutputStream.nullOutputStream());
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/hudson/remoting/throughput/Sender.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
import hudson.remoting.Pipe;
import hudson.remoting.SocketChannelStream;
import org.apache.commons.io.IOUtils;
import org.apache.commons.io.output.NullOutputStream;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.security.DigestOutputStream;
import java.security.MessageDigest;
Expand Down Expand Up @@ -59,7 +59,7 @@ public static void main(String[] args) throws Exception {
}

private static byte[] digest(InputStream in) throws NoSuchAlgorithmException, IOException {
DigestOutputStream dos = new DigestOutputStream(NullOutputStream.INSTANCE, MessageDigest.getInstance("MD5"));
DigestOutputStream dos = new DigestOutputStream(OutputStream.nullOutputStream(), MessageDigest.getInstance("MD5"));
IOUtils.copy(in, dos);
return dos.getMessageDigest().digest();
}
Expand Down
37 changes: 15 additions & 22 deletions src/test/java/org/jenkinsci/remoting/engine/WorkDirManagerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,12 @@
package org.jenkinsci.remoting.engine;

import edu.umd.cs.findbugs.annotations.NonNull;
import org.apache.commons.io.FileUtils;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
Expand All @@ -44,7 +42,6 @@
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.io.IOUtils;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
Expand Down Expand Up @@ -73,20 +70,20 @@ public void shouldInitializeCorrectlyForExistingDirectory() throws Exception {
final File dir = tmpDir.newFolder("foo");

// Probe files to confirm the directory does not get wiped
final File probeFileInWorkDir = new File(dir, "probe.txt");
FileUtils.write(probeFileInWorkDir, "Hello!", StandardCharsets.UTF_8);
final File remotingDir = new File(dir, DirType.INTERNAL_DIR.getDefaultLocation());
Files.createDirectory(remotingDir.toPath());
final File probeFileInInternalDir = new File(remotingDir, "/probe.txt");
FileUtils.write(probeFileInInternalDir, "Hello!", StandardCharsets.UTF_8);
final Path probeFileInWorkDir = dir.toPath().resolve("probe.txt");
Files.writeString(probeFileInWorkDir, "Hello!", StandardCharsets.UTF_8);
final Path remotingDir = dir.toPath().resolve(DirType.INTERNAL_DIR.getDefaultLocation());
Files.createDirectory(remotingDir);
final Path probeFileInInternalDir = remotingDir.resolve("probe.txt");
Files.writeString(probeFileInInternalDir, "Hello!", StandardCharsets.UTF_8);

// Initialize and check the results
final Path createdDir = WorkDirManager.getInstance().initializeWorkDir(dir, DirType.INTERNAL_DIR.getDefaultLocation(), false);
assertThat("The initialized " + DirType.INTERNAL_DIR + " differs from the expected one", createdDir.toFile(), equalTo(remotingDir));
assertThat("The initialized " + DirType.INTERNAL_DIR + " differs from the expected one", createdDir, equalTo(remotingDir));

// Ensure that the files have not been wiped
Assert.assertTrue("Probe file in the " + DirType.WORK_DIR + " has been wiped", probeFileInWorkDir.exists());
Assert.assertTrue("Probe file in the " + DirType.INTERNAL_DIR + " has been wiped", probeFileInInternalDir.exists());
Assert.assertTrue("Probe file in the " + DirType.WORK_DIR + " has been wiped", Files.exists(probeFileInWorkDir));
Assert.assertTrue("Probe file in the " + DirType.INTERNAL_DIR + " has been wiped", Files.exists(probeFileInInternalDir));

// Ensure that sub directories are in place
assertExists(DirType.JAR_CACHE_DIR);
Expand Down Expand Up @@ -230,11 +227,9 @@ public void shouldCreateLogFilesOnTheDisk() throws Exception {
assertFileLogsExist(logsDir, "remoting.log", 0);

// Ensure the entry has been written
File log0 = new File(logsDir, "remoting.log.0");
try (FileInputStream istr = new FileInputStream(log0)) {
String contents = IOUtils.toString(istr, StandardCharsets.UTF_8);
assertThat("Log file " + log0 + " should contain the probe message", contents, containsString(message));
}
Path log0 = logsDir.toPath().resolve("remoting.log.0");
String contents = Files.readString(log0, StandardCharsets.UTF_8);
assertThat("Log file " + log0 + " should contain the probe message", contents, containsString(message));
}

@Test
Expand Down Expand Up @@ -290,11 +285,9 @@ private void doTestLoggingConfig(File loggingConfigFile, boolean passToManager)

// Assert that logs have been written to the specified custom destination
assertFileLogsExist(customLogDir, "mylog.log", 1);
File log0 = new File(customLogDir, "mylog.log.0");
try (FileInputStream istr = new FileInputStream(log0)) {
String contents = IOUtils.toString(istr, StandardCharsets.UTF_8);
assertThat("Log file " + log0 + " should contain the probe message", contents, containsString(message));
}
Path log0 = customLogDir.toPath().resolve("mylog.log.0");
String contents = Files.readString(log0, StandardCharsets.UTF_8);
assertThat("Log file " + log0 + " should contain the probe message", contents, containsString(message));
}

private void assertFileLogsExist(File logsDir, String prefix, int logFilesNumber) {
Expand Down

0 comments on commit f8d6d2d

Please sign in to comment.