8373660: Add explicit null check in DataOutputStream.writeBytes #28869
+30 −0
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.
Summary
Add explicit null check using
Objects.requireNonNullinDataOutputStream.writeBytes(String s)to provide a clearer error messageand make the API contract explicit.
Problem
The
writeBytesmethod currently throws aNullPointerExceptionwhennullis passed, but the error message may not be clear in all contexts.While JDK 14+ provides helpful NullPointerException messages through
JEP 358 (Helpful NullPointerExceptions), adding an explicit null check
using
Objects.requireNonNullmakes the API contract more explicit andprovides consistent error messaging across different JDK versions.
Solution
Added
Objects.requireNonNull(s, "s")at the beginning of thewriteBytes(String s)method. This ensures:Issue
Fixes JDK-8373660
JBS Issue Link: https://bugs.java.com/bugdatabase/view_bug?bug_id=JDK-8373660
Type of Change
Testing
A new JUnit test has been added to verify the null check behavior: