Skip to content

Remove unnecessary assertion in String#getBytes(byte[], int, byte) #144

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

Merged
merged 1 commit into from
Aug 16, 2018

Conversation

gayanW
Copy link
Collaborator

@gayanW gayanW commented Aug 15, 2018

/**
* If two coders are different and target is big enough,
* invoker guarantees that the target is in UTF16
*/
void getBytes(byte dst[], int dstBegin, byte coder) {
assert coder == UTF16;
if (coder() == coder) {
System.arraycopy(value, 0, dst, dstBegin << coder, value.length);
} else { // this.coder == LATIN1 && coder == UTF16
StringLatin1.inflate(value, 0, dst, dstBegin, value.length);
}
}

Our initial conclusion that the coder is always UTF16 is wrong. As it states "If two coders are different and the target is big enough, invoker guarantees that the target is in UTF16 ". So the invoker guarantees not to call when the source encoding is UTF-8 and the target is in LATIN-1.

But what if the source and target encoding are in "UTF-8". So the assertion statement needs to be removed.

This also fixes:

[junit] java.lang.AssertionError
[junit]     at java.lang.String.getBytes(String.java:254)

The error can be seen on ObjectStreamTest, ClassLoaderTest, StringTest and in a few other tests.

@cyrille-artho
Copy link
Member

Thanks for catching this. In this case, please turn the comment in the "else" condition into an assertion.

As it states "If two coders are different and the target is big enough,
invoker guarantees that the target is in UTF16"

So the invoker guarantees not to call when the source encoding is UTF-8
and the target is in LATIN-1. However the source and target can both be
in "UTF-8".

So the assertion that the coder is always UTF16 is wrong.
@gayanW
Copy link
Collaborator Author

gayanW commented Aug 16, 2018

Done.

Please delete the java-10_134-1 branch once this is being merged. The PR is initially created from a branch in my fork. But the PR did not reflect the changes when I pushed changes. So I tried pushing directly to this repo and it resulted in a new branch being created in the javapathfinder repo.

@cyrille-artho cyrille-artho merged commit 51a6fc3 into java-10 Aug 16, 2018
@cyrille-artho cyrille-artho deleted the java-10_134-1 branch August 16, 2018 16:03
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.

2 participants