package org.boilit.bsl.encoding;

import java.io.IOException;
import java.io.OutputStream;

/**
 * @author Boilit
 * @see
 */
public final class UTF8Encoder extends AbstractEncoder {

    public UTF8Encoder(final String encoding) {
        super(encoding);
    }

    @Override
    public final void write(final OutputStream outputStream, final String string) throws IOException {
        final int n = string.length();
        final FixedByteArray fb = this.getFixedByteArray();
        fb.dilatation(n << 2);
        for (int i = 0; i < n; i++) {
            encode(fb, string.charAt(i));
        }
        outputStream.write(fb.bytes(), 0, fb.size());
    }

    private final void encode(final FixedByteArray fb, final char c) {
//        if (c < 0x80) {
//            fb.append((byte) c);
//        } else if (c < 0x800) {
//            fb.append((byte) (0xc0 | c >> 6));
//            fb.append((byte) (0x80 | c & 0x3f));
//        } else if (c > 0xDFFF || c < 0xD800) {
//            fb.append((byte) (0xe0 | c >> 12));
//            fb.append((byte) (0x80 | c >> 6 & 0x3f));
//            fb.append((byte) (0x80 | c & 0x3f));
//        } else {
//            fb.append((byte) 0x3F);
//        }

        if(c < 0) {
            fb.append((byte) 0x3Ff);
        } else if (c < 0x80) {
            fb.append((byte) c);
        } else if (c < 0x800) {
            fb.append((byte) (0xc0 | c >> 6));
            fb.append((byte) (0x80 | c & 0x3f));
        } else if(c < 0xD800){
            fb.append((byte) (0xe0 | c >> 12));
            fb.append((byte) (0x80 | c >> 6 & 0x3f));
            fb.append((byte) (0x80 | c & 0x3f));
        } else if(c < 0xE000){
            fb.append((byte) 0x3F);
        } else if (c < 0x10000) {
            fb.append((byte) (0xe0 | c >> 12));
            fb.append((byte) (0x80 | c >> 6 & 0x3f));
            fb.append((byte) (0x80 | c & 0x3f));
        } else if (c < 0x200000) {
            fb.append((byte) (0xf0 | c >> 18));
            fb.append((byte) (0x80 | c >> 12));
            fb.append((byte) (0x80 | c >> 6 & 0x3f));
            fb.append((byte) (0x80 | c & 0x3f));
        } else if (c < 0x4000000) {
            fb.append((byte) (0xf8 | c >> 24));
            fb.append((byte) (0x80 | c >> 18));
            fb.append((byte) (0x80 | c >> 12));
            fb.append((byte) (0x80 | c >> 6 & 0x3f));
            fb.append((byte) (0x80 | c & 0x3f));
        } else {
            fb.append((byte) (0xfc | c >> 30));
            fb.append((byte) (0x80 | c >> 24));
            fb.append((byte) (0x80 | c >> 18));
            fb.append((byte) (0x80 | c >> 12));
            fb.append((byte) (0x80 | c >> 6 & 0x3f));
            fb.append((byte) (0x80 | c & 0x3f));
        }
    }
}

@@ -663,545 +663,181 @@
     
-private final void encode(final FixedByteArray fb, final char c) %7B%0A//        if (c %3C 0x80) %7B%0A//            fb.append((byte) c);%0A//        %7D else if (c %3C 0x800) %7B%0A//            fb.append((byte) (0xc0 %7C c %3E%3E 6));%0A//  
+/**%0A     * encode result same as jdk String.getBytes(%22UTF-8%22)%0A     * @param fb%0A     * @param c%0A
      
+*/%0A
     
- fb.append((byte) (0x80 %7C c & 0x3f));%0A//        %7D else if (c %3E 0xDFFF %7C%7C c %3C 0xD800) %7B%0A//            fb.append((byte) (0xe0 %7C c %3E%3E 12));%0A//            fb.append((byte) (0x80 %7C c %3E%3E 6 & 0x3f));%0A//            fb.append((byte) (0x80 %7C c & 0x3f));%0A//        %7D else %7B%0A//            fb.append((byte) 0x3F);%0A//        %7D%0A
+private final void encode(final FixedByteArray fb, final char c) %7B
 %0A   
@@ -885,17 +885,16 @@
 yte) 0x3
-F
 f);%0A    

update to 1.1.0