Java 8 migration. Move WebSocketUtil.base64 to java version instead of netty (#8837)

Motivation:

Since Java 8, JDK has `java.util.Base64` that could replace custom netty implementation. It is faster (3x for this particular PR) and simpler.

Modifications:

Use Base64 directly.


Result:

```
Benchmark                   Mode  Cnt        Score        Error  Units
Base64Benchmark.base64New  thrpt    6  7739181.025 ± 114230.467  ops/s
Base64Benchmark.base64Old  thrpt    6  2689783.304 ± 454710.641  ops/s
```
This commit is contained in:
Dmitriy Dumanskiy 2019-02-12 20:10:06 +02:00 committed by Norman Maurer
parent dd96b4a876
commit 900d00d5db

View File

@ -15,14 +15,12 @@
*/
package io.netty.handler.codec.http.websocketx;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.handler.codec.base64.Base64;
import io.netty.util.CharsetUtil;
import io.netty.util.concurrent.FastThreadLocal;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;
import java.util.concurrent.ThreadLocalRandom;
/**
@ -91,13 +89,8 @@ final class WebSocketUtil {
* @return An encoded string containing the data
*/
static String base64(byte[] data) {
ByteBuf encodedData = Unpooled.wrappedBuffer(data);
ByteBuf encoded = Base64.encode(encodedData);
String encodedString = encoded.toString(CharsetUtil.UTF_8);
encoded.release();
return encodedString;
return Base64.getEncoder().encodeToString(data);
}
/**
* Creates an arbitrary number of random bytes
*