lucene-solr icon indicating copy to clipboard operation
lucene-solr copied to clipboard

SOLR-15111: Use JDK8 Base64 instead of own implementation

Open asalamon74 opened this issue 4 years ago • 3 comments

Description

JDK8 has a builtin Base64 encoder and decoder, there is no need to use own implementaion for this.

Solution

Eliminate own implementation.

Tests

Unit tests.

Checklist

Please review the following and check all that apply:

  • [x] I have reviewed the guidelines for How to Contribute and my code conforms to the standards described there to the best of my ability.
  • [x] I have created a Jira issue and added the issue ID to my pull request title.
  • [x] I have given Solr maintainers access to contribute to my PR branch. (optional but recommended)
  • [x] I have developed this patch against the master branch.
  • [x] I have run ./gradlew check.
  • [ ] I have added tests for my changes.
  • [ ] I have added documentation for the Ref Guide (for Solr changes only).

asalamon74 avatar Jan 27 '21 13:01 asalamon74

@madrob

ISO_8859_1

In the first version I used UTF_8 but later I checked the source code of java.util.Base64 ( http://hg.openjdk.java.net/jdk8/jdk8/jdk/file/687fd7c7986d/src/share/classes/java/util/Base64.java ) and they are using ISO-8859-1 internally, so I thought to use the same.

public byte[] decode(String src) {
    return decode(src.getBytes(StandardCharsets.ISO_8859_1));
}

Probably we could use both character encodings, since base64 encoded strings only use a small subset of the characters.

encode + decode

StandardCharsets.ISO_8859_1.decode(Base64.getEncoder().encode(bytes.wrapToByteBuffer())).toString());

First I base64 encode the ByteBuffer which gives me a ByteBuffer then I convert this ByteBuffer to String using Charsets decode ( https://stackoverflow.com/a/39845152 ). So it's an encode + a decode but it's a different type of encoding/decoding.

asalamon74 avatar Jan 29 '21 07:01 asalamon74

Unfortunately, we can’t use the solution provided from stack overflow -http://apache.org/legal/resolved.html#stackoverflow

Can you contact the original author and ask for permission to use this? Otherwise we will need somebody who hasn’t looked this code to create a clean room implementation.

madrob avatar Jan 29 '21 13:01 madrob

We also used a different way for String conversion, I modified the lines.

asalamon74 avatar Feb 04 '21 11:02 asalamon74