package net.ihiroky.niotty.sample;

import net.ihiroky.niotty.LoadStage;
import net.ihiroky.niotty.LoadStageContext;
import net.ihiroky.niotty.buffer.CodecBuffer;
import net.ihiroky.niotty.TransportStateEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.nio.charset.CharacterCodingException;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CodingErrorAction;

/**
 * Created on 13/01/18, 14:12
 *
 * @author Hiroki Itoh
 */
public class StringDecoder implements LoadStage<CodecBuffer, String> {

    private Logger logger_ = LoggerFactory.getLogger(StringDecoder.class);

    private static final Charset CHARSET = Charset.forName("UTF-8");

    private CharsetDecoder decoder_ = CHARSET.newDecoder()
            .onMalformedInput(CodingErrorAction.IGNORE)
            .onUnmappableCharacter(CodingErrorAction.IGNORE);

    @Override
    public void load(LoadStageContext<CodecBuffer, String> context, CodecBuffer input) {
        try {
            String s = decoder_.decode(input.toByteBuffer()).toString();
            context.proceed(s);
        } catch (CharacterCodingException cce) {
            cce.printStackTrace();
        }
    }

    @Override
    public void load(LoadStageContext<CodecBuffer, String> context, TransportStateEvent event) {
        logger_.info(event.toString());
        context.proceed(event);
    }
}

@@ -140,26 +140,27 @@
 tty.
-buffer.CodecBuffer
+TransportStateEvent
 ;%0Aim
@@ -183,35 +183,34 @@
 .niotty.
-TransportStateEvent
+buffer.CodecBuffer
 ;%0Aimport
@@ -293,144 +293,11 @@
 Char
-acterCodingException;%0Aimport java.nio.charset.Charset;%0Aimport java.nio.charset.CharsetDecoder;%0Aimport java.nio.charset.CodingErrorAction
+set
 ;%0A%0A/
@@ -585,497 +585,193 @@
     
-private CharsetDecoder decoder_ = CHARSET.newDecoder()%0A            .onMalformedInput(CodingErrorAction.IGNORE)%0A            .onUnmappableCharacter(CodingErrorAction.IGNORE);%0A%0A    @Override%0A    public void load(LoadStageContext%3CCodecBuffer, String%3E context, CodecBuffer input) %7B%0A        try %7B%0A            String s = decoder_.decode(input.toByteBuffer()).toString();%0A            context.proceed(s);%0A        %7D catch (CharacterCodingException cce) %7B%0A            cce.printStackTrace();%0A        %7D
+@Override%0A    public void load(LoadStageContext%3CCodecBuffer, String%3E context, CodecBuffer input) %7B%0A        String s = input.readString(CHARSET.newDecoder());%0A        context.proceed(s);
 %0A   

Fix invalid decoder in sample