diff --git a/platform/built-in-server/src/org/jetbrains/io/webSocket/MessageChannelHandler.java b/platform/built-in-server/src/org/jetbrains/io/webSocket/MessageChannelHandler.java index 8a94ed4807b3..fe482bc618f1 100644 --- a/platform/built-in-server/src/org/jetbrains/io/webSocket/MessageChannelHandler.java +++ b/platform/built-in-server/src/org/jetbrains/io/webSocket/MessageChannelHandler.java @@ -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 { diff --git a/platform/built-in-server/testSrc/ChannelBufferToStringTest.kt b/platform/built-in-server/testSrc/ChannelBufferToStringTest.kt index 85770bd1391f..6948eaab879e 100644 --- a/platform/built-in-server/testSrc/ChannelBufferToStringTest.kt +++ b/platform/built-in-server/testSrc/ChannelBufferToStringTest.kt @@ -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) } } \ No newline at end of file diff --git a/platform/platform-impl/src/io/netty/buffer/ByteBufUtilEx.java b/platform/platform-impl/src/io/netty/buffer/ByteBufUtilEx.java index 72252ed51b82..3911c9cadfc4 100644 --- a/platform/platform-impl/src/io/netty/buffer/ByteBufUtilEx.java +++ b/platform/platform-impl/src/io/netty/buffer/ByteBufUtilEx.java @@ -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) { diff --git a/platform/platform-impl/src/org/jetbrains/io/MessageDecoder.java b/platform/platform-impl/src/org/jetbrains/io/MessageDecoder.java index e4a5fb987cb1..dca61410e12e 100644 --- a/platform/platform-impl/src/org/jetbrains/io/MessageDecoder.java +++ b/platform/platform-impl/src/org/jetbrains/io/MessageDecoder.java @@ -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); } } diff --git a/platform/platform-impl/src/org/jetbrains/io/netty.kt b/platform/platform-impl/src/org/jetbrains/io/netty.kt index 653910718ef2..fe102ed62ccd 100644 --- a/platform/platform-impl/src/org/jetbrains/io/netty.kt +++ b/platform/platform-impl/src/org/jetbrains/io/netty.kt @@ -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) -} \ No newline at end of file +} + +fun ByteBuf.readUtf8() = toString(Charsets.UTF_8) \ No newline at end of file