Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pde.internal.build.Utils: use some java.nio convenience #637

Merged
merged 1 commit into from
Jul 10, 2023

Conversation

jukzi
Copy link
Contributor

@jukzi jukzi commented Jun 23, 2023

No description provided.

@github-actions
Copy link

github-actions bot commented Jun 23, 2023

Test Results

     246 files  +       6       246 suites  +6   1h 1m 29s ⏱️ + 21m 8s
  3 287 tests  -        1    3 262 ✔️ ±       0  24 💤 ±  0  0  - 1  1 🔥 ±0 
10 158 runs  +2 721  10 085 ✔️ +2 698  72 💤 +24  0  - 1  1 🔥 ±0 

For more details on these errors, see this check.

Results for commit 8f96e69. ± Comparison against base commit 7dd409f.

This pull request removes 1 test.
AllPDEMinimalTests org.eclipse.pde.ui.tests.launcher.AllLauncherTests org.eclipse.pde.ui.tests.launcher.FeatureBasedLaunchTest ‑ Unknown test

♻️ This comment has been updated with latest results.

Copy link
Member

@HannesWell HannesWell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this. In general it looks good, but I have a few suggestions for further improvements below.

Since you replaced all references you can delete the Utils.writeBuffer() method.

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();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Path dest = new File(sourceFragmentDirURL.append(Constants.FRAGMENT_FILENAME_DESCRIPTOR).toOSString()).toPath();
Path dest = sourceFragmentDirURL.append(Constants.FRAGMENT_FILENAME_DESCRIPTOR).toPath();

Comment on lines +694 to +696
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());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better use:

		Path destFile = sourcePluginDirURL.append(Constants.BUNDLE_FILENAME_DESCRIPTOR).toPath();
		try {
			Files.writeString(destFile, buffer.toString());

@@ -934,21 +874,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())));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return new StringBuffer(new String(Files.readAllBytes(target.toPath())));
return new StringBuffer(Files.readString(target.toPath()));

In general I think it would be better to just return the String and let the caller add it to a StringBuffer if necessary.
Then this method is sufficenitly simple to just inline it into all callers.
Additionally the caller should use StringBuilder instead of StringBuffer.

IIRC there is even an automated JDT-clean up to use StringBuilder instead of StringBuffer. However this is probably better done in a follow up.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Files.readString uses UTF8 instead of std charset. Probably UTF8 is meant anyway, but this change is not about changing the charset.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's right that the charset does not match, but then the default charset can be passed as second argument.
This would have to advantages, it saves one string-array copy (Files.readString() re-uses the byte array in the string) and it makes more clear that a 'special' charset is used here and future readers of the code are less likely to over-see it.
Furthermore a second argument is not that much more code and then we can replace this extra method with standard Java.

}
}
return result;
return new StringBuffer(new String(stream.readAllBytes()));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this method is sufficiently simple to just inline it into the callers.
The callers of this method should also use StringBuilder instead of StringBuffer.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would duplicate the flaw that a std charset is used.

@jukzi
Copy link
Contributor Author

jukzi commented Jul 10, 2023

ignoring random fail PlainJavaProjectTest #555

@jukzi jukzi merged commit a2612ef into eclipse-pde:master Jul 10, 2023
10 of 12 checks passed
@jukzi jukzi deleted the Utils branch July 10, 2023 09:37
@HannesWell
Copy link
Member

@jukzi please be so kind and give participants of a discussion time to respond in case of a disagreement before merging.
That being said, I created a follow up on this, your review is welcome:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants