/*
 * Copyright 2000-2015 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.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
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 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));
  }

  @SuppressWarnings("unused")
  @NotNull
  public static CharSequence readChars(@NotNull ByteBuf buffer, int byteCount) throws IOException {
    return new CharSequenceBackedByChars(readIntoCharBuffer(buffer, byteCount, null));
  }

  @NotNull
  public static CharBuffer readIntoCharBuffer(@NotNull ByteBuf buffer, int byteCount, @Nullable CharBuffer charBuffer) throws IOException {
    if (charBuffer == null) {
      charBuffer = CharBuffer.allocate(byteCount);
    }
    ByteBufUtilEx.readUtf8(buffer, byteCount, charBuffer);
    return charBuffer;
  }

  public static void writeIntAsAscii(int value, @NotNull ByteBuf buffer) {
    ByteBufUtil.writeAscii(buffer, new StringBuilder().append(value));
  }
}
@@ -618,17 +618,16 @@
 rains.io
-;
 %0A%0Aimport
@@ -650,17 +650,16 @@
 .ByteBuf
-;
 %0Aimport 
@@ -685,17 +685,16 @@
 eBufUtil
-;
 %0Aimport 
@@ -706,37 +706,32 @@
 tty.
-buffer.ByteBuf
+util.Charset
 Util
-Ex;
 %0Aimport 
 org.
@@ -730,86 +730,54 @@
 ort 
-org.jetbrains.annotations.NotNull;%0Aimport org.jetbrains.annotations.Nullable;%0A
+java.nio.ByteBuffer%0Aimport java.nio.CharBuffer
 %0Aimp
@@ -789,13 +789,35 @@
 ava.
+n
 io.
-IO
+charset.CharacterCoding
 Exce
@@ -821,17 +821,16 @@
 xception
-;
 %0Aimport 
@@ -842,472 +842,472 @@
 nio.
-CharBuffer;%0A%0Apublic final class ChannelBufferToString %7B%0A  @NotNull%0A  public static CharSequence readChars(@NotNull ByteBuf buffer) throws IOException %7B%0A    return new CharSequenceBackedByChars(readIntoCharBuffer(buffer, buffer.readableBytes(), null));%0A  %7D%0A%0A  @SuppressWarnings(%22unused%22)%0A  @NotNull%0A  public static CharSequence readChars(@NotNull ByteBuf buffer, int byteCount) throws IOException %7B%0A    return new CharSequenceBackedByChars(readIntoChar
+charset.CharsetDecoder%0A%0Afun ByteBuf.readIntoCharBuffer(byteCount: Int = readableBytes(), charBuffer: CharBuffer) %7B%0A  val decoder = CharsetUtil.decoder(CharsetUtil.UTF_8)%0A  if (nioBufferCount() == 1) %7B%0A    decodeString(decoder, internalNioBuffer(readerIndex(), byteCount), charBuffer)%0A  %7D%0A  else %7B%0A    val buffer = alloc().heapBuffer(byteCount)%0A    try %7B%0A      buffer.writeBytes(this, readerIndex(), byteCount)%0A      decodeString(decoder, buffer.internalNio
 Buffer(
-buffer
+0
 , by
@@ -1317,96 +1317,141 @@
 ount
+)
 , 
-null));%0A
+charBuffer)%0A  
   %7D%0A
-%0A  @NotNull%0A  public static CharBuffer readIntoCharBuffer(@NotNull
+    finally %7B%0A      buffer.release()%0A    %7D%0A  %7D%0A%7D%0A%0Aprivate fun decodeString(decoder: CharsetDecoder, src:
  ByteBuf
  buf
@@ -1450,74 +1450,125 @@
 eBuf
- buf
 fer, 
-int byteCount, @Nullable CharBuffer charBuffer) 
+dst: CharBuffer) %7B%0A  try %7B%0A    var cr = decoder.decode(src, dst, true)%0A    if (!cr.isUnderflow) %7B%0A      cr.
 throw
-s IO
 Exce
@@ -1576,205 +1576,194 @@
 tion
- %7B%0A    if (charBuffer == null) %7B%0A      charBuffer = CharBuffer.allocate(byteCount);%0A    %7D%0A    ByteBufUtilEx.readUtf8(buffer, byteCount, charBuffer);%0A    return charBuffer;%0A  %7D%0A%0A  public static void
+()%0A    %7D%0A    cr = decoder.flush(dst)%0A    if (!cr.isUnderflow) %7B%0A      cr.throwException()%0A    %7D%0A  %7D%0A  catch (x: CharacterCodingException) %7B%0A    throw IllegalStateException(x)%0A  %7D%0A%0A%7D%0A%0Afun
  wri
@@ -1779,35 +1779,35 @@
 cii(
-int 
 value
-, @NotNull
+: Int, buffer:
  ByteBuf
  buf
@@ -1806,21 +1806,12 @@
 eBuf
- buffer
 ) %7B%0A
-  
   By
@@ -1842,12 +1842,8 @@
 fer,
- new
  Str
@@ -1873,11 +1873,6 @@
 ue))
-;%0A  %7D
 %0A%7D

convert ChannelBufferToString to kotlin