protected CoderResult encodeLoop(CharBuffer in, ByteBuffer out) {
            int remaining = in.remaining();

            while (remaining-- > 0) {
                // make sure we have output space (the max we might need)
                if (out.remaining() < 4)
                    return CoderResult.OVERFLOW;

                // get next byte
                char c = in.get();

                // if not in shift sequence
                if (!shifted) {
                    // if char is in set D, write it as byte directly
                    if (isDirect(c)) {
                        out.put((byte)c);
                    } else if (c == '+') { // specially encoded char
                        out.put((byte)'+').put((byte)'-');
                    } else { // start shift sequence
                        out.put((byte)'+');
                        shifted = true;
                        requiredBits = 6;
                        writeBase64Char(out,c);
                    }
                } else { // shifted
                    if (isDirect(c)) { // direct char
                        // terminate shift sequence
                        shifted = false;
                        if (requiredBits != 6) // dump last encoded byte, zero-bit padded
                            out.put(toBase64((byte)((encodedByte << requiredBits) & 0x3F)));
                        if (isBSet(c) || c == '-') // requires explicit termination
                            out.put((byte)'-');
                        // write direct char
                        out.put((byte)c);
                    } else { // another encoded char
                        writeBase64Char(out,c);
                    }
                }
            }

            return CoderResult.UNDERFLOW;
        }