mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
prefer to use standard netty bytebuf.toString since now it use cache under the hood
This commit is contained in:
@@ -6,7 +6,7 @@ import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.handler.codec.http.websocketx.CloseWebSocketFrame;
|
||||
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.io.ChannelBufferToString;
|
||||
import org.jetbrains.io.NettyKt;
|
||||
import org.jetbrains.io.jsonRpc.Client;
|
||||
import org.jetbrains.io.jsonRpc.ClientManager;
|
||||
import org.jetbrains.io.jsonRpc.ClientManagerKt;
|
||||
@@ -43,7 +43,7 @@ final class MessageChannelHandler extends WebSocketProtocolHandler {
|
||||
WebSocketClient client = (WebSocketClient)channel.attr(ClientManagerKt.getCLIENT()).get();
|
||||
CharSequence chars;
|
||||
try {
|
||||
chars = ChannelBufferToString.readChars(message.content());
|
||||
chars = NettyKt.readUtf8(message.content());
|
||||
}
|
||||
catch (Throwable e) {
|
||||
try {
|
||||
|
||||
@@ -4,11 +4,11 @@ import io.netty.buffer.Unpooled
|
||||
import org.assertj.core.api.Assertions.assertThat
|
||||
import org.junit.Test
|
||||
|
||||
private class ChannelBufferToStringTest {
|
||||
internal class ChannelBufferToStringTest {
|
||||
@Test
|
||||
fun readUtf8() {
|
||||
val string = "\ud83d\udd1d"
|
||||
val byteBuffer = Unpooled.copiedBuffer(string, Charsets.UTF_8)
|
||||
assertThat(ChannelBufferToString.readChars(byteBuffer)).isEqualTo(string)
|
||||
assertThat(byteBuffer.readUtf8()).isEqualTo(string)
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2000-2015 JetBrains s.r.o.
|
||||
* Copyright 2000-2016 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -18,10 +18,6 @@ package io.netty.buffer;
|
||||
import io.netty.util.CharsetUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.UTFDataFormatException;
|
||||
import java.nio.CharBuffer;
|
||||
|
||||
// todo pull request
|
||||
public class ByteBufUtilEx {
|
||||
public static int writeUtf8(ByteBuf buf, CharSequence seq) {
|
||||
@@ -93,88 +89,6 @@ public class ByteBufUtilEx {
|
||||
return writerIndex;
|
||||
}
|
||||
|
||||
@SuppressWarnings("SpellCheckingInspection")
|
||||
public static void readUtf8(@NotNull ByteBuf buf, int byteCount, @NotNull CharBuffer charBuffer) throws IOException {
|
||||
AbstractByteBuf buffer = getBuf(buf);
|
||||
int readerIndex = buf.readerIndex();
|
||||
|
||||
int c, char2, char3;
|
||||
int count = 0;
|
||||
|
||||
int byteIndex = readerIndex;
|
||||
int charIndex = charBuffer.position();
|
||||
char[] chars = charBuffer.array();
|
||||
while (count < byteCount) {
|
||||
c = buffer._getByte(byteIndex++) & 0xff;
|
||||
if (c > 127) {
|
||||
break;
|
||||
}
|
||||
|
||||
count++;
|
||||
chars[charIndex++] = (char)c;
|
||||
}
|
||||
|
||||
// byteIndex incremented before check "c > 127", so, we must reset it
|
||||
byteIndex = readerIndex + count;
|
||||
while (count < byteCount) {
|
||||
c = buffer._getByte(byteIndex++) & 0xff;
|
||||
switch (c >> 4) {
|
||||
case 0:
|
||||
case 1:
|
||||
case 2:
|
||||
case 3:
|
||||
case 4:
|
||||
case 5:
|
||||
case 6:
|
||||
case 7:
|
||||
// 0xxxxxxx
|
||||
count++;
|
||||
chars[charIndex++] = (char)c;
|
||||
break;
|
||||
|
||||
case 12:
|
||||
case 13:
|
||||
// 110x xxxx 10xx xxxx
|
||||
count += 2;
|
||||
if (count > byteCount) {
|
||||
throw new UTFDataFormatException("malformed input: partial character at end");
|
||||
}
|
||||
char2 = (int)buffer._getByte(byteIndex++);
|
||||
if ((char2 & 0xC0) != 0x80) {
|
||||
throw new UTFDataFormatException("malformed input around byte " + count);
|
||||
}
|
||||
chars[charIndex++] = (char)(((c & 0x1F) << 6) | (char2 & 0x3F));
|
||||
break;
|
||||
|
||||
case 14:
|
||||
// 1110 xxxx 10xx xxxx 10xx xxxx
|
||||
count += 3;
|
||||
if (count > byteCount) {
|
||||
throw new UTFDataFormatException("malformed input: partial character at end");
|
||||
}
|
||||
char2 = buffer._getByte(byteIndex++);
|
||||
char3 = buffer._getByte(byteIndex++);
|
||||
if (((char2 & 0xC0) != 0x80) || ((char3 & 0xC0) != 0x80)) {
|
||||
throw new UTFDataFormatException("malformed input around byte " + (count - 1));
|
||||
}
|
||||
chars[charIndex++] = (char)(((c & 0x0F) << 12) | ((char2 & 0x3F) << 6) | ((char3 & 0x3F)));
|
||||
break;
|
||||
|
||||
default:
|
||||
// 10xx xxxx, 1111 xxxx
|
||||
throw new UTFDataFormatException("malformed input around byte " + count);
|
||||
}
|
||||
}
|
||||
|
||||
if (buf == buffer) {
|
||||
buffer.readerIndex = readerIndex + byteCount;
|
||||
}
|
||||
else {
|
||||
buf.readerIndex(readerIndex + byteCount);
|
||||
}
|
||||
charBuffer.position(charIndex);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
static AbstractByteBuf getBuf(@NotNull ByteBuf buffer) {
|
||||
if (buffer instanceof AbstractByteBuf) {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2000-2015 JetBrains s.r.o.
|
||||
* Copyright 2000-2016 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -22,6 +22,7 @@ import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.CharBuffer;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
public abstract class MessageDecoder extends Decoder {
|
||||
protected int contentLength;
|
||||
@@ -48,18 +49,21 @@ public abstract class MessageDecoder extends Decoder {
|
||||
chunkedContent = CharBuffer.allocate(contentLength);
|
||||
}
|
||||
|
||||
ChannelBufferToString.readIntoCharBuffer(input, readableBytes, chunkedContent);
|
||||
ChannelBufferToStringKt.readIntoCharBuffer(input, readableBytes, chunkedContent);
|
||||
consumedContentByteCount += readableBytes;
|
||||
input.release();
|
||||
return null;
|
||||
}
|
||||
else {
|
||||
CharBuffer charBuffer = chunkedContent;
|
||||
if (charBuffer != null) {
|
||||
chunkedContent = null;
|
||||
consumedContentByteCount = 0;
|
||||
if (charBuffer == null) {
|
||||
return input.toString(input.readerIndex(), required, StandardCharsets.UTF_8);
|
||||
}
|
||||
return new CharSequenceBackedByChars(ChannelBufferToString.readIntoCharBuffer(input, required, charBuffer));
|
||||
|
||||
chunkedContent = null;
|
||||
consumedContentByteCount = 0;
|
||||
ChannelBufferToStringKt.readIntoCharBuffer(input, required, charBuffer);
|
||||
return new CharSequenceBackedByChars(charBuffer);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -196,4 +196,6 @@ fun HttpRequest.isRegularBrowser() = userAgent?.startsWith("Mozilla/5.0") ?: fal
|
||||
fun HttpRequest.isWriteFromBrowserWithoutOrigin(): Boolean {
|
||||
val method = method()
|
||||
return origin.isNullOrEmpty() && isRegularBrowser() && (method == HttpMethod.POST || method == HttpMethod.PATCH || method == HttpMethod.PUT || method == HttpMethod.DELETE)
|
||||
}
|
||||
}
|
||||
|
||||
fun ByteBuf.readUtf8() = toString(Charsets.UTF_8)
|
||||
Reference in New Issue
Block a user