[JENKINS-42934] Avoid using new FileInputStream / new FileOutputStream

This commit is contained in:
Stephen Connolly committed 2017-03-20 12:50:46 +00:00
1 parent 4948d6c556
commit f0cd7ae8ff
41 files changed
+188 -159

No files matched your search

+9 -8
View File
@@ -43,6 +43,7 @@ import java.net.URL;
import java.net.URLConnection;
import java.net.URLStreamHandler;
import java.net.URLStreamHandlerFactory;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
@@ -134,12 +135,12 @@ public class FilePathTest {
}
private void givenSomeContentInFile(File file, int size) throws IOException {
FileOutputStream os = new FileOutputStream(file);
byte[] buf = new byte[size];
for (int i=0; i<buf.length; i++)
buf[i] = (byte)(i%256);
os.write(buf);
os.close();
try (OutputStream os = Files.newOutputStream(file.toPath())) {
byte[] buf = new byte[size];
for (int i = 0; i < buf.length; i++)
buf[i] = (byte) (i % 256);
os.write(buf);
}
}
private List<Future<Integer>> whenFileIsCopied100TimesConcurrently(final File file) throws InterruptedException {
@@ -369,7 +370,7 @@ public class FilePathTest {
// Compress archive
final FilePath tmpDirPath = new FilePath(tmpDir);
int tar = tmpDirPath.tar(new FileOutputStream(tarFile), tempFile.getName());
int tar = tmpDirPath.tar(Files.newOutputStream(tarFile.toPath()), tempFile.getName());
assertEquals("One file should have been compressed", 1, tar);
// Decompress
@@ -725,7 +726,7 @@ public class FilePathTest {
// Compress archive
final FilePath tmpDirPath = new FilePath(srcFolder);
int tarred = tmpDirPath.tar(new FileOutputStream(archive), "**");
int tarred = tmpDirPath.tar(Files.newOutputStream(archive.toPath()), "**");
assertEquals("One file should have been compressed", 3, tarred);
// Decompress
@@ -26,6 +26,7 @@ package hudson;
import java.io.File;
import java.io.FileOutputStream;
import java.nio.file.Files;
import org.apache.tools.ant.filters.StringInputStream;
import org.junit.Test;
import org.xml.sax.SAXException;
@@ -151,7 +152,7 @@ public class PluginManagerTest {
FileUtils.write(new File(newFolder, manifestPath), SAMPLE_MANIFEST_FILE);
final File f = new File(tmp.getRoot(), "my.hpi");
try(ZipOutputStream out = new ZipOutputStream(new FileOutputStream(f))) {
try(ZipOutputStream out = new ZipOutputStream(Files.newOutputStream(f.toPath()))) {
ZipEntry e = new ZipEntry(manifestPath);
out.putNextEntry(e);
byte[] data = SAMPLE_MANIFEST_FILE.getBytes();
+2 -1
View File
@@ -24,6 +24,7 @@
*/
package hudson;
import java.nio.file.Files;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
@@ -490,7 +491,7 @@ public class UtilTest {
// On unix, can't use "chattr +u" because ext fs ignores it.
// On Windows, we can't delete files that are open for reading, so we use that.
assert Functions.isWindows();
final InputStream s = new FileInputStream(f);
final InputStream s = Files.newInputStream(f.toPath());
unlockFileCallables.put(f, new Callable<Void>() {
public Void call() throws IOException { s.close(); return null; };
});
@@ -26,6 +26,9 @@ package hudson.model;
import hudson.model.MultiStageTimeSeries.TimeScale;
import hudson.model.queue.SubTask;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.StandardOpenOption;
import org.apache.commons.io.IOUtils;
import org.jfree.chart.JFreeChart;
import org.junit.Test;
@@ -87,11 +90,9 @@ public class LoadStatisticsTest {
BufferedImage image = chart.createBufferedImage(400, 200);
File tempFile = File.createTempFile("chart-", "png");
FileOutputStream os = new FileOutputStream(tempFile);
try {
try (OutputStream os = Files.newOutputStream(tempFile.toPath(), StandardOpenOption.DELETE_ON_CLOSE)) {
ImageIO.write(image, "PNG", os);
} finally {
IOUtils.closeQuietly(os);
tempFile.delete();
}
}
+3 -1
View File
@@ -1,6 +1,8 @@
package hudson.os;
import hudson.util.StreamTaskListener;
import java.io.File;
import java.nio.file.Files;
import jenkins.security.MasterToSlaveCallable;
import java.io.FileOutputStream;
@@ -13,7 +15,7 @@ public class SUTester {
SU.execute(StreamTaskListener.fromStdout(),"kohsuke","bogus",new MasterToSlaveCallable<Object, Throwable>() {
public Object call() throws Throwable {
System.out.println("Touching /tmp/x");
new FileOutputStream("/tmp/x").close();
Files.newOutputStream(new File("/tmp/x").toPath()).close();
return null;
}
});
@@ -33,6 +33,8 @@ import hudson.util.StreamTaskListener;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Files;
import java.util.Arrays;
import static org.junit.Assert.*;
import org.junit.Assume;
@@ -71,8 +73,8 @@ public class TarArchiverTest {
f.chmod(0644);
int dirMode = dir.child("subdir").mode();
dir.tar(new FileOutputStream(tar),"**/*");
dir.zip(new FileOutputStream(zip));
dir.tar(Files.newOutputStream(tar.toPath()),"**/*");
dir.zip(Files.newOutputStream(zip.toPath()));
FilePath e = dir.child("extract");
@@ -149,12 +151,12 @@ public class TarArchiverTest {
File openFile = file;
try {
openFile.createNewFile();
FileOutputStream fos = new FileOutputStream(openFile);
for (int i = 0; !finish && i < 5000000; i++) { // limit the max size, just in case.
fos.write(0);
// Thread.sleep(5);
try (OutputStream fos = Files.newOutputStream(openFile.toPath())) {
for (int i = 0; !finish && i < 5000000; i++) { // limit the max size, just in case.
fos.write(0);
// Thread.sleep(5);
}
}
fos.close();
} catch (Exception e) {
ex = e;
}
@@ -5,6 +5,7 @@ import static org.junit.Assert.assertEquals;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.zip.ZipEntry;
@@ -62,7 +63,7 @@ public class ZipArchiverTest {
try {
zipFile = File.createTempFile("test", ".zip");
archiver = new ZipArchiver(new FileOutputStream(zipFile));
archiver = new ZipArchiver(Files.newOutputStream(zipFile.toPath()));
archiver.visit(tmpFile, "foo\\bar\\baz\\Test.txt");
} catch (Exception e) {