Skip to content

Replace byte array zeroing loop with Arrays.fill and specify charset in String construction #9458

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

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from

Conversation

cat-and-tomato
Copy link

@cat-and-tomato cat-and-tomato commented Jun 9, 2025

Which Issue(s) This PR Fixes

Fixes #9460

Brief Description

This PR improves code readability and portability by replacing manual byte array zeroing and by explicitly specifying a charset when converting bytes to string.

How Did You Test This Change?

These changes are refactorings that do not alter functionality. Existing unit tests continue to pass. Manual testing was also performed to ensure behavior remains consistent.

…2.原代码使用 new String(bos.toByteArray()) 会使用平台默认的字符集进行解码,可能导致跨平台或环境不一致时出现乱码
for (int i = 0; i < bytes.length; i++) {
bytes[i] = (byte) 0x00;
}
Arrays.fill(bytes, (byte) 0x00); // 替换为 Arrays.fill()
Copy link
Contributor

Choose a reason for hiding this comment

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

Chinese comments are not allowed

Copy link
Author

Choose a reason for hiding this comment

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

I've made the suggested changes. Please take another look when you have time:)

@RongtongJin
Copy link
Contributor

It would be better to create an issue first and link it to the PR. Additionally, it is recommended to write the title in English and follow the specified format.

@cat-and-tomato
Copy link
Author

It would be better to create an issue first and link it to the PR. Additionally, it is recommended to write the title in English and follow the specified format.

Thanks for your guidance!

@cat-and-tomato cat-and-tomato changed the title 1.使用 for 循环初始化字节数组为 0,可读性和效率较低,可以将 for 循环替换为更简洁、高效的 Arrays.fill() 方法,… Replace byte array zeroing loop with Arrays.fill and specify charset in String construction Jun 10, 2025
@codecov-commenter
Copy link

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 48.00%. Comparing base (53a3f69) to head (aa6586c).

Additional details and impacted files
@@              Coverage Diff              @@
##             develop    #9458      +/-   ##
=============================================
- Coverage      48.11%   48.00%   -0.11%     
+ Complexity     12015    11989      -26     
=============================================
  Files           1308     1308              
  Lines          92253    92252       -1     
  Branches       11808    11807       -1     
=============================================
- Hits           44385    44288      -97     
- Misses         42373    42454      +81     
- Partials        5495     5510      +15     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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.

[Enhancement] Replace byte array zeroing loop with Arrays.fill and specify charset in String construction
3 participants