import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CodingErrorAction;
import java.nio.ByteBuffer;
import java.io.Reader;
import java.io.InputStreamReader;
import java.io.ByteArrayInputStream;

public class Test {

    public static void main(String [] args) throws Exception{

        final String z = "髙";
        Charset charset = Charset.forName("x-windows-iso2022jp");

        CharsetDecoder charsetDecoderForStr = charset.newDecoder()
                .onMalformedInput(CodingErrorAction.REPLACE)
                .onUnmappableCharacter(CodingErrorAction.REPLACE);

        CharsetDecoder charsetDecoderForStrm = charset.newDecoder()
                .onMalformedInput(CodingErrorAction.REPLACE)
                .onUnmappableCharacter(CodingErrorAction.REPLACE);

        ByteBuffer buffer = ByteBuffer.allocate(21);
        byte[] bytes = {27, 36, 66, 124, 98, 27, 40, 66, 27};
        buffer.put(bytes);

        System.out.println("Using new String without Decoder"+new String(bytes, charset).trim());
        System.out.println("Using Decoder "+charsetDecoderForStr.decode((ByteBuffer) buffer.flip()).toString().trim());

        Reader reader = new InputStreamReader(new ByteArrayInputStream(bytes), charsetDecoderForStrm);
        char[] chars = new char[10];
        reader.read(chars);
        System.out.println("Using StreamDecoder "+new String(chars).trim());
    }
}
