Skip to content

Commit

Permalink
pde.internal.build.Utils: use some java.nio convenience
Browse files Browse the repository at this point in the history
  • Loading branch information
EcljpseB0T authored and jukzi committed Jul 10, 2023
1 parent 64ca662 commit a2612ef
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 105 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@
import static org.junit.Assert.assertTrue;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.util.Dictionary;
import java.util.HashSet;
import java.util.Properties;
Expand Down Expand Up @@ -360,10 +360,10 @@ public void testIndividualSourceBundles() throws Exception {

try (ZipFile zip = new ZipFile(buildFolder.getFile("I.TestBuild/eclipse.zip").getLocation().toFile())) {
ZipEntry entry = zip.getEntry("eclipse/plugins/bundleA.source_1.0.0.jar");
InputStream in = new BufferedInputStream(zip.getInputStream(entry));
IFile jar = buildFolder.getFile("bundleA.source_1.0.0.jar");
OutputStream out = new BufferedOutputStream(new FileOutputStream(jar.getLocation().toFile()));
org.eclipse.pde.internal.build.Utils.transferStreams(in, out);
try (InputStream in = zip.getInputStream(entry)) {
Files.copy(in, jar.getLocation().toFile().toPath(), StandardCopyOption.REPLACE_EXISTING);
}
}

entries.clear();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -409,8 +410,10 @@ public void assertZipPermissions(IFile zip, String file, String permissions) thr

command = new String[] { "ls", "-la", extractedFile.getLocation().toOSString() };
proc = Runtime.getRuntime().exec(command);
Utils.transferStreams(proc.getInputStream(),
new FileOutputStream(tempFolder.getFile("ls.out").getLocation().toFile()));
Path dest = tempFolder.getFile("ls.out").getLocation().toFile().toPath();
try (InputStream inputStream = proc.getInputStream()) {
Files.copy(inputStream, dest, StandardCopyOption.REPLACE_EXISTING);
}
proc.waitFor();

assertLogContainsLine(tempFolder.getFile("ls.out"), permissions);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Dictionary;
Expand Down Expand Up @@ -187,7 +188,7 @@ public boolean generateP2Info() throws CoreException {

try {
File p2Inf = new File(root, "p2.inf"); //$NON-NLS-1$
Utils.writeBuffer(buffer, p2Inf);
Files.writeString(p2Inf.toPath(), buffer.toString());
} catch (IOException e) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,17 @@
*******************************************************************************/
package org.eclipse.pde.internal.build;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
Expand Down Expand Up @@ -383,39 +380,6 @@ public static IPath makeRelative(IPath location, IPath base) {
return IPath.fromOSString(temp).append(location.removeFirstSegments(count));
}

/**
* Transfers all available bytes from the given input stream to the given
* output stream. Regardless of failure, this method closes both streams.
*
* @param source
* @param destination
* @throws IOException
*/
public static void transferStreams(InputStream source, OutputStream destination) throws IOException {
source = new BufferedInputStream(source);
destination = new BufferedOutputStream(destination);
try {
byte[] buffer = new byte[8192];
while (true) {
int bytesRead = -1;
if ((bytesRead = source.read(buffer)) == -1)
break;
destination.write(buffer, 0, bytesRead);
}
} finally {
try {
source.close();
} catch (IOException e) {
// ignore
}
try {
destination.close();
} catch (IOException e) {
// ignore
}
}
}

static public void copyFile(String src, String dest) throws IOException {
File source = new File(src);
if (!source.exists())
Expand All @@ -432,17 +396,6 @@ public static void copy(File source, File destination) throws IOException {
org.eclipse.pde.internal.publishing.Utils.copy(source, destination);
}

public static void writeBuffer(StringBuffer buffer, File outputFile) throws IOException {
FileOutputStream stream = null;
try {
outputFile.getParentFile().mkdirs();
stream = new FileOutputStream(outputFile);
stream.write(buffer.toString().getBytes());
} finally {
close(stream);
}
}

public static void writeProperties(Properties properites, File outputFile, String comment) throws IOException {
outputFile.getParentFile().mkdirs();
OutputStream buildFile = new BufferedOutputStream(new FileOutputStream(outputFile));
Expand Down Expand Up @@ -521,32 +474,9 @@ public static Collection<String> copyFiles(String fromDir, String toDir) throws
}
continue;
}

FileInputStream inputStream = null;
FileOutputStream outputStream = null;

try {
inputStream = new FileInputStream(file);
} catch (FileNotFoundException e) {
String message = NLS.bind(Messages.exception_missingFile, file.getAbsolutePath());
throw new CoreException(new Status(IStatus.ERROR, PI_PDEBUILD, EXCEPTION_READING_FILE, message, e));
}

String fileToCopy = toDir + '/' + file.getName();
try {
outputStream = new FileOutputStream(fileToCopy);
} catch (FileNotFoundException e) {
try {
inputStream.close();
} catch (IOException e1) {
// Ignored
}
String message = NLS.bind(Messages.exception_missingFile, fileToCopy);
throw new CoreException(new Status(IStatus.ERROR, PI_PDEBUILD, EXCEPTION_READING_FILE, message, e));
}

try {
Utils.transferStreams(inputStream, outputStream);
Files.copy(file.toPath(), Path.of(fileToCopy));
copiedFiles.add(file.getName());
} catch (IOException e) {
String message = NLS.bind(Messages.exception_writingFile, fileToCopy);
Expand Down Expand Up @@ -934,21 +864,11 @@ static public int scan(StringBuffer buf, int start, String[] targets) {
* @throws IOException
*/
static public StringBuffer readFile(File target) throws IOException {
return readFile(new FileInputStream(target));
return new StringBuffer(new String(Files.readAllBytes(target.toPath())));
}

static public StringBuffer readFile(InputStream stream) throws IOException {
StringBuffer result = new StringBuffer();
char[] buf = new char[4096];
int count;
try (InputStreamReader reader = new InputStreamReader(new BufferedInputStream(stream))) {
count = reader.read(buf, 0, buf.length);
while (count != -1) {
result.append(buf, 0, count);
count = reader.read(buf, 0, buf.length);
}
}
return result;
return new StringBuffer(new String(stream.readAllBytes()));
}

/**
Expand Down Expand Up @@ -983,7 +903,7 @@ public static void updateVersion(File buildFile, String propertyName, String ver
if (currentVersion.equals(newVersion))
return;
buffer.replace(begin, end, newVersion);
transferStreams(new ByteArrayInputStream(buffer.toString().getBytes()), new FileOutputStream(buildFile));
Files.writeString(buildFile.toPath(), buffer.toString());
}

public static Enumeration<Object> getArrayEnumerator(Object[] array) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
import java.io.OutputStream;
import java.io.SequenceInputStream;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
Expand Down Expand Up @@ -320,9 +323,9 @@ private void create30SourceFragment(FeatureEntry fragment, FeatureEntry plugin)
}

//Copy the fragment.xml
try {
InputStream fragmentXML = BundleHelper.getDefault().getBundle().getEntry(TEMPLATE + "/30/fragment/fragment.xml").openStream(); //$NON-NLS-1$
Utils.transferStreams(fragmentXML, new FileOutputStream(sourceFragmentDirURL.append(Constants.FRAGMENT_FILENAME_DESCRIPTOR).toOSString()));
Path dest = new File(sourceFragmentDirURL.append(Constants.FRAGMENT_FILENAME_DESCRIPTOR).toOSString()).toPath();
try (InputStream fragmentXML = BundleHelper.getDefault().getBundle().getEntry(TEMPLATE + "/30/fragment/fragment.xml").openStream()) { //$NON-NLS-1$
Files.copy(fragmentXML, dest, StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e1) {
String message = NLS.bind(Messages.exception_readingFile, TEMPLATE + "/30/fragment/fragment.xml"); //$NON-NLS-1$
throw new CoreException(new Status(IStatus.ERROR, PI_PDEBUILD, EXCEPTION_WRITING_FILE, message, e1));
Expand All @@ -345,8 +348,8 @@ private void create30SourceFragment(FeatureEntry fragment, FeatureEntry plugin)
// Set the platform filter of the fragment
beginId = Utils.scan(buffer, beginId, REPLACED_PLATFORM_FILTER);
buffer.replace(beginId, beginId + REPLACED_PLATFORM_FILTER.length(), "(& (osgi.ws=" + fragment.getWS() + ") (osgi.os=" + fragment.getOS() + ") (osgi.arch=" + fragment.getArch() + "))"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$

Utils.transferStreams(new ByteArrayInputStream(buffer.toString().getBytes()), new FileOutputStream(sourceFragmentDirURL.append(Constants.BUNDLE_FILENAME_DESCRIPTOR).toOSString()));
Path destManifest = sourceFragmentDirURL.append(Constants.BUNDLE_FILENAME_DESCRIPTOR).toPath();
Files.writeString(destManifest, buffer.toString());
Collection<String> copiedFiles = Utils.copyFiles(featureRootLocation + '/' + "sourceTemplateFragment", sourceFragmentDir.getAbsolutePath()); //$NON-NLS-1$
if (copiedFiles.contains(Constants.BUNDLE_FILENAME_DESCRIPTOR)) {
//make sure the manifest.mf has the versions we want
Expand Down Expand Up @@ -467,7 +470,7 @@ private void replaceXMLAttribute(String location, String tag, String attr, Strin
}
if (attrFound) {
try {
Utils.transferStreams(new ByteArrayInputStream(buffer.toString().getBytes()), new FileOutputStream(featureFile));
Files.writeString(featureFile.toPath(), buffer.toString());
} catch (IOException e) {
//ignore
}
Expand Down Expand Up @@ -688,17 +691,18 @@ private FeatureEntry create30SourcePlugin(BuildTimeFeature sourceFeature) throws
//set the version number
beginId = Utils.scan(buffer, beginId, REPLACED_PLUGIN_VERSION);
buffer.replace(beginId, beginId + REPLACED_PLUGIN_VERSION.length(), result.getVersion());
String destName = sourcePluginDirURL.append(Constants.BUNDLE_FILENAME_DESCRIPTOR).toOSString();
try {
Utils.transferStreams(new ByteArrayInputStream(buffer.toString().getBytes()), new FileOutputStream(sourcePluginDirURL.append(Constants.BUNDLE_FILENAME_DESCRIPTOR).toOSString()));
Files.writeString(new File(destName).toPath(), buffer.toString());
} catch (IOException e1) {
String message = NLS.bind(Messages.exception_writingFile, templateManifestURL.toExternalForm());
throw new CoreException(new Status(IStatus.ERROR, PI_PDEBUILD, EXCEPTION_READING_FILE, message, e1));
}

//Copy the plugin.xml
try {
InputStream pluginXML = BundleHelper.getDefault().getBundle().getEntry(TEMPLATE + "/30/plugin/plugin.xml").openStream(); //$NON-NLS-1$
Utils.transferStreams(pluginXML, new FileOutputStream(sourcePluginDirURL.append(Constants.PLUGIN_FILENAME_DESCRIPTOR).toOSString()));
Path dest = sourcePluginDirURL.append(Constants.PLUGIN_FILENAME_DESCRIPTOR).toPath();
try (InputStream pluginXML = BundleHelper.getDefault().getBundle().getEntry(TEMPLATE + "/30/plugin/plugin.xml").openStream()) { //$NON-NLS-1$
Files.copy(pluginXML, dest, StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e1) {
String message = NLS.bind(Messages.exception_readingFile, TEMPLATE + "/30/plugin/plugin.xml"); //$NON-NLS-1$
throw new CoreException(new Status(IStatus.ERROR, PI_PDEBUILD, EXCEPTION_WRITING_FILE, message, e1));
Expand Down

0 comments on commit a2612ef

Please sign in to comment.