internal static int WriteUTF(string str, IDataOutput @out)
        {
            int strlen = str.Length;
            int utflen = 0;
            int c, count = 0;

            /* use charAt instead of copying String to char array */
            for (int i = 0; i < strlen; i++)
            {
                c = str[i];
                if ((c >= 0x0001) && (c <= 0x007F))
                {
                    utflen++;
                }
                else if (c > 0x07FF)
                {
                    utflen += 3;
                }
                else
                {
                    utflen += 2;
                }
            }

            if (utflen > 65535)
                throw new FormatException(
                    "encoded string too long: " + utflen + " bytes");

            byte[] bytearr = null;
            if (@out is DataOutputStream) {
                DataOutputStream dos = (DataOutputStream)@out;
                if (dos.bytearr == null || (dos.bytearr.Length < (utflen + 2)))
                    dos.bytearr = new byte[(utflen * 2) + 2];
                bytearr = dos.bytearr;
            } else {
                bytearr = new byte[utflen + 2];
            }

            bytearr[count++] = (byte)(int)(((uint)utflen >> 8) & 0xFF);
            bytearr[count++] = (byte)(int)(((uint)utflen >> 0) & 0xFF);

            int i2 = 0;
            for (i2 = 0; i2 < strlen; i2++)
            {
                c = str[i2];
                if (!((c >= 0x0001) && (c <= 0x007F))) break;
                bytearr[count++] = (byte)c;
            }

            for (; i2 < strlen; i2++)
            {
                c = str[i2];
                if ((c >= 0x0001) && (c <= 0x007F))
                {
                    bytearr[count++] = (byte)c;

                }
                else if (c > 0x07FF)
                {
                    bytearr[count++] = (byte)(0xE0 | ((c >> 12) & 0x0F));
                    bytearr[count++] = (byte)(0x80 | ((c >> 6) & 0x3F));
                    bytearr[count++] = (byte)(0x80 | ((c >> 0) & 0x3F));
                }
                else
                {
                    bytearr[count++] = (byte)(0xC0 | ((c >> 6) & 0x1F));
                    bytearr[count++] = (byte)(0x80 | ((c >> 0) & 0x3F));
                }
            }
            @out.Write(bytearr, 0, utflen + 2);
            return utflen + 2;
        }
Example #2
0
        internal static int WriteUTF(string str, IDataOutput @out)
        {
            int strlen = str.Length;
            int utflen = 0;
            int c, count = 0;

            /* use charAt instead of copying String to char array */
            for (int i = 0; i < strlen; i++)
            {
                c = str[i];
                if ((c >= 0x0001) && (c <= 0x007F))
                {
                    utflen++;
                }
                else if (c > 0x07FF)
                {
                    utflen += 3;
                }
                else
                {
                    utflen += 2;
                }
            }

            if (utflen > 65535)
            {
                throw new FormatException(
                          "encoded string too long: " + utflen + " bytes");
            }

            byte[] bytearr = null;
            if (@out is DataOutputStream)
            {
                DataOutputStream dos = (DataOutputStream)@out;
                if (dos.bytearr == null || (dos.bytearr.Length < (utflen + 2)))
                {
                    dos.bytearr = new byte[(utflen * 2) + 2];
                }
                bytearr = dos.bytearr;
            }
            else
            {
                bytearr = new byte[utflen + 2];
            }

            bytearr[count++] = (byte)(int)(((uint)utflen >> 8) & 0xFF);
            bytearr[count++] = (byte)(int)(((uint)utflen >> 0) & 0xFF);

            int i2 = 0;

            for (i2 = 0; i2 < strlen; i2++)
            {
                c = str[i2];
                if (!((c >= 0x0001) && (c <= 0x007F)))
                {
                    break;
                }
                bytearr[count++] = (byte)c;
            }

            for (; i2 < strlen; i2++)
            {
                c = str[i2];
                if ((c >= 0x0001) && (c <= 0x007F))
                {
                    bytearr[count++] = (byte)c;
                }
                else if (c > 0x07FF)
                {
                    bytearr[count++] = (byte)(0xE0 | ((c >> 12) & 0x0F));
                    bytearr[count++] = (byte)(0x80 | ((c >> 6) & 0x3F));
                    bytearr[count++] = (byte)(0x80 | ((c >> 0) & 0x3F));
                }
                else
                {
                    bytearr[count++] = (byte)(0xC0 | ((c >> 6) & 0x1F));
                    bytearr[count++] = (byte)(0x80 | ((c >> 0) & 0x3F));
                }
            }
            @out.Write(bytearr, 0, utflen + 2);
            return(utflen + 2);
        }