convert ChannelBufferToString to kotlin
authorVladimir Krivosheev <vladimir.krivosheev@jetbrains.com>
Fri, 10 Jun 2016 15:46:05 +0000 (17:46 +0200)
committerVladimir Krivosheev <vladimir.krivosheev@jetbrains.com>
Fri, 10 Jun 2016 16:32:05 +0000 (18:32 +0200)
platform/platform-impl/src/org/jetbrains/io/ChannelBufferToString.java

index 01360eeaeb9419ec5a119a75b0b5267dc6b057f6..8316843e3932e3fe96ab6e50bf104ef36e459087 100644 (file)
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package org.jetbrains.io;
+package org.jetbrains.io
 
-import io.netty.buffer.ByteBuf;
-import io.netty.buffer.ByteBufUtil;
-import io.netty.buffer.ByteBufUtilEx;
-import org.jetbrains.annotations.NotNull;
-import org.jetbrains.annotations.Nullable;
+import io.netty.buffer.ByteBuf
+import io.netty.buffer.ByteBufUtil
+import io.netty.util.CharsetUtil
+import java.nio.ByteBuffer
+import java.nio.CharBuffer
+import java.nio.charset.CharacterCodingException
+import java.nio.charset.CharsetDecoder
 
-import java.io.IOException;
-import java.nio.CharBuffer;
-
-public final class ChannelBufferToString {
-  @NotNull
-  public static CharSequence readChars(@NotNull ByteBuf buffer) throws IOException {
-    return new CharSequenceBackedByChars(readIntoCharBuffer(buffer, buffer.readableBytes(), null));
+fun ByteBuf.readIntoCharBuffer(byteCount: Int = readableBytes(), charBuffer: CharBuffer) {
+  val decoder = CharsetUtil.decoder(CharsetUtil.UTF_8)
+  if (nioBufferCount() == 1) {
+    decodeString(decoder, internalNioBuffer(readerIndex(), byteCount), charBuffer)
   }
-
-  @SuppressWarnings("unused")
-  @NotNull
-  public static CharSequence readChars(@NotNull ByteBuf buffer, int byteCount) throws IOException {
-    return new CharSequenceBackedByChars(readIntoCharBuffer(buffer, byteCount, null));
+  else {
+    val buffer = alloc().heapBuffer(byteCount)
+    try {
+      buffer.writeBytes(this, readerIndex(), byteCount)
+      decodeString(decoder, buffer.internalNioBuffer(0, byteCount), charBuffer)
+    }
+    finally {
+      buffer.release()
+    }
   }
+}
 
-  @NotNull
-  public static CharBuffer readIntoCharBuffer(@NotNull ByteBuf buffer, int byteCount, @Nullable CharBuffer charBuffer) throws IOException {
-    if (charBuffer == null) {
-      charBuffer = CharBuffer.allocate(byteCount);
+private fun decodeString(decoder: CharsetDecoder, src: ByteBuffer, dst: CharBuffer) {
+  try {
+    var cr = decoder.decode(src, dst, true)
+    if (!cr.isUnderflow) {
+      cr.throwException()
+    }
+    cr = decoder.flush(dst)
+    if (!cr.isUnderflow) {
+      cr.throwException()
     }
-    ByteBufUtilEx.readUtf8(buffer, byteCount, charBuffer);
-    return charBuffer;
   }
-
-  public static void writeIntAsAscii(int value, @NotNull ByteBuf buffer) {
-    ByteBufUtil.writeAscii(buffer, new StringBuilder().append(value));
+  catch (x: CharacterCodingException) {
+    throw IllegalStateException(x)
   }
+
+}
+
+fun writeIntAsAscii(value: Int, buffer: ByteBuf) {
+  ByteBufUtil.writeAscii(buffer, StringBuilder().append(value))
 }
\ No newline at end of file