public StringBuilder appendToStringBuilder()

in lang/src/main/java/com/epam/deltix/util/memory/MemoryDataInput.java [406:490]


    public StringBuilder    appendToStringBuilder (StringBuilder sb) {
        int         utflen = readUnsignedShort ();
        
        if (utflen == 0xFFFF)
            return (null);
        
        if (utflen == 0)
            return (sb);
        
        int c;
        int char2, char3;
        int count = 0;        
        
        for (;;) {
            c = readByte ();
            if (c < 0) //WAS: if (c > 127 || c < 0)
                break;
            
            count++;
            sb.append ((char) c);
            
            if (count >= utflen)
                return (sb);
        }
        //  If we are here, we have broken out of the previous loop and there is an
        //  unhandled escape character in variable c.        
        for (;;) {
            switch ((0xff & c) >> 4) {
                case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
                    /* 0xxxxxxx*/
                    count++;
                    sb.append ((char)c);
                    break;
                    
                case 12: case 13:
                    /* 110x xxxx   10xx xxxx*/
                    count += 2;
                    
                    if (count > utflen)
                        throw new UncheckedIOException(
                            "malformed input: partial character at end"
                        );
                    
                    char2 = readByte ();
                    
                    if ((char2 & 0xC0) != 0x80)
                        throw new UncheckedIOException (
                            "malformed input around byte " + count
                        ); 
                    
                    sb.append ((char)(((c & 0x1F) << 6) | (char2 & 0x3F)));  
                    break;
                    
                case 14:
                    /* 1110 xxxx  10xx xxxx  10xx xxxx */
                    count += 3;
                    if (count > utflen)
                        throw new UncheckedIOException(
                            "malformed input: partial character at end"
                        );
                    char2 = readByte ();
                    char3 = readByte ();
                    if (((char2 & 0xC0) != 0x80) || ((char3 & 0xC0) != 0x80))
                        throw new UncheckedIOException(
                            "malformed input around byte " + (count-1));
                    sb.append ((char)(((c & 0x0F) << 12) |
                                                    ((char2 & 0x3F) << 6)  |
                                                     (char3 & 0x3F)));
                    break;
                    
                default:
                    /* 10xx xxxx,  1111 xxxx */
                    throw new UncheckedIOException (
                        "malformed input around byte " + count
                    );
            }
                        
            if (count >= utflen)
                break;
            
            c = readByte ();
        }        
        
        return (sb);
    }