Advertisement
Guest User

Untitled

a guest
Oct 16th, 2014
456
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.97 KB | None | 0 0
  1. public String ReadUTF()
  2.     {
  3.         int utflen = this.ReadUnsignedShort ();
  4.         byte[] bytearr = null;
  5.         char[] chararr = null;
  6.  
  7.         if(bytearr.Length < utflen)
  8.         {
  9.             bytearr = new byte[utflen * 2];
  10.             chararr = new char[utflen * 2];
  11.         }
  12.            
  13.         int c, char2, char3;
  14.         int count = 0;
  15.         int chararr_count=0;
  16.        
  17.         this.ReadFully(bytearr, 0, utflen);
  18.        
  19.         while (count < utflen) {
  20.             c = (int) bytearr[count] & 0xff;
  21.             if (c > 127) break;
  22.             count++;
  23.             chararr[chararr_count++]=(char)c;
  24.         }
  25.        
  26.         while (count < utflen) {
  27.             c = (int) bytearr[count] & 0xff;
  28.             switch (c >> 4) {
  29.             case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
  30.                 /* 0xxxxxxx*/
  31.                 count++;
  32.                 chararr[chararr_count++]=(char)c;
  33.                 break;
  34.             case 12: case 13:
  35.                 /* 110x xxxx   10xx xxxx*/
  36.                 count += 2;
  37.                 if (count > utflen)
  38.                     throw new Exception(
  39.                         "malformed input: partial character at end");
  40.                 char2 = (int) bytearr[count-1];
  41.                 if ((char2 & 0xC0) != 0x80)
  42.                     throw new Exception(
  43.                         "malformed input around byte " + count);
  44.                 chararr[chararr_count++]=(char)(((c & 0x1F) << 6) |
  45.                                                 (char2 & 0x3F));
  46.                 break;
  47.             case 14:
  48.                 /* 1110 xxxx  10xx xxxx  10xx xxxx */
  49.                 count += 3;
  50.                 if (count > utflen)
  51.                     throw new Exception(
  52.                         "malformed input: partial character at end");
  53.                 char2 = (int) bytearr[count-2];
  54.                 char3 = (int) bytearr[count-1];
  55.                 if (((char2 & 0xC0) != 0x80) || ((char3 & 0xC0) != 0x80))
  56.                     throw new Exception(
  57.                         "malformed input around byte " + (count-1));
  58.                 chararr[chararr_count++]=(char)(((c     & 0x0F) << 12) |
  59.                                                 ((char2 & 0x3F) << 6)  |
  60.                                                 ((char3 & 0x3F) << 0));
  61.                 break;
  62.             default:
  63.                 /* 10xx xxxx,  1111 xxxx */
  64.                 throw new Exception(
  65.                     "malformed input around byte " + count);
  66.             }
  67.         }
  68.         // The number of chars produced may be less than utflen
  69.         return new String(chararr, 0, chararr_count);
  70.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement