decode process output streams using the native encoding#2689
Open
netliomax25-code wants to merge 1 commit into
Open
decode process output streams using the native encoding#2689netliomax25-code wants to merge 1 commit into
netliomax25-code wants to merge 1 commit into
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR updates Groovy’s process I/O helpers to decode child process output using the OS native encoding (via the native.encoding system property) instead of relying on Charset.defaultCharset(), avoiding silent character corruption on non-UTF-8 hosts post–JEP 400.
Changes:
- Add a
nativeEncoding()helper that resolvesSystem.getProperty("native.encoding")with fallback toCharset.defaultCharset(). - Use
nativeEncoding()when decodingProcess#getInputStream()ingetText(Process). - Use
nativeEncoding()inTextDumper(used byconsumeProcessOutput*/waitForProcessOutput*pathways) when reading process output.
Comments suppressed due to low confidence (1)
src/main/java/org/codehaus/groovy/runtime/ProcessGroovyMethods.java:658
- TextDumper creates BufferedReader/InputStreamReader but never closes it, so the underlying process stream can remain open if the thread exits early (e.g., due to an exception). Wrapping the reader in try-with-resources ensures the stream is always closed and avoids resource leaks.
InputStreamReader isr = new InputStreamReader(in, nativeEncoding());
BufferedReader br = new BufferedReader(isr);
String next;
try {
while ((next = br.readLine()) != null) {
Comment on lines
+98
to
+107
| String name = System.getProperty("native.encoding"); | ||
| if (name != null && !name.isEmpty()) { | ||
| try { | ||
| return Charset.forName(name); | ||
| } catch (IllegalArgumentException ignore) { | ||
| // unknown or unsupported name - fall back below | ||
| } | ||
| } | ||
| return Charset.defaultCharset(); | ||
| } |
| */ | ||
| public static String getText(Process self) throws IOException { | ||
| String text = IOGroovyMethods.getText(new BufferedReader(new InputStreamReader(self.getInputStream()))); | ||
| String text = IOGroovyMethods.getText(new BufferedReader(new InputStreamReader(self.getInputStream(), nativeEncoding()))); |
✅ All tests passed ✅🏷️ Commit: 913be1a Learn more about TestLens at testlens.app. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Decode with the native.encoding system property (JDK 17+, the documented way to obtain the OS native encoding), falling back to Charset.defaultCharset() when it is unavailable. UTF-8 hosts are unaffected.