PageRenderTime 766ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/QingTingFanBianYi/src/com/alibaba/fastjson/util/UTF8Decoder.java

https://gitlab.com/qt-prometheus/qt-prometheus
Java | 243 lines | 219 code | 20 blank | 4 comment | 69 complexity | d83fd98be55257661b1bf258a2ca5185 MD5 | raw file
  1. package com.alibaba.fastjson.util;
  2. import java.nio.Buffer;
  3. import java.nio.ByteBuffer;
  4. import java.nio.CharBuffer;
  5. import java.nio.charset.Charset;
  6. import java.nio.charset.CharsetDecoder;
  7. import java.nio.charset.CoderResult;
  8. public class UTF8Decoder extends CharsetDecoder
  9. {
  10. private static final Charset charset = Charset.forName("UTF-8");
  11. public UTF8Decoder()
  12. {
  13. super(charset, 1.0F, 1.0F);
  14. }
  15. private CoderResult decodeArrayLoop(ByteBuffer paramByteBuffer, CharBuffer paramCharBuffer)
  16. {
  17. byte[] arrayOfByte = paramByteBuffer.array();
  18. int i = paramByteBuffer.arrayOffset() + paramByteBuffer.position();
  19. int m = paramByteBuffer.arrayOffset() + paramByteBuffer.limit();
  20. char[] arrayOfChar = paramCharBuffer.array();
  21. int k = paramCharBuffer.arrayOffset() + paramCharBuffer.position();
  22. int n = paramCharBuffer.arrayOffset() + paramCharBuffer.limit();
  23. int i1 = Math.min(m - i, n - k);
  24. int j = k;
  25. while ((j < k + i1) && (arrayOfByte[i] >= 0))
  26. {
  27. arrayOfChar[j] = ((char)arrayOfByte[i]);
  28. j += 1;
  29. i += 1;
  30. continue;
  31. arrayOfChar[j] = ((char)k);
  32. i += 1;
  33. j += 1;
  34. }
  35. while (true)
  36. {
  37. if (i < m)
  38. {
  39. k = arrayOfByte[i];
  40. if (k >= 0)
  41. {
  42. if (j < n)
  43. break;
  44. return xflow(paramByteBuffer, i, m, paramCharBuffer, j, 1);
  45. }
  46. if (k >> 5 == -2)
  47. {
  48. if ((m - i < 2) || (j >= n))
  49. return xflow(paramByteBuffer, i, m, paramCharBuffer, j, 2);
  50. i1 = arrayOfByte[(i + 1)];
  51. if (isMalformed2(k, i1))
  52. return malformed(paramByteBuffer, i, paramCharBuffer, j, 2);
  53. arrayOfChar[j] = ((char)(k << 6 ^ i1 ^ 0xF80));
  54. i += 2;
  55. j += 1;
  56. continue;
  57. }
  58. int i2;
  59. if (k >> 4 == -2)
  60. {
  61. if ((m - i < 3) || (j >= n))
  62. return xflow(paramByteBuffer, i, m, paramCharBuffer, j, 3);
  63. i1 = arrayOfByte[(i + 1)];
  64. i2 = arrayOfByte[(i + 2)];
  65. if (isMalformed3(k, i1, i2))
  66. return malformed(paramByteBuffer, i, paramCharBuffer, j, 3);
  67. arrayOfChar[j] = ((char)(k << 12 ^ i1 << 6 ^ i2 ^ 0x1F80));
  68. i += 3;
  69. j += 1;
  70. continue;
  71. }
  72. if (k >> 3 == -2)
  73. {
  74. if ((m - i < 4) || (n - j < 2))
  75. return xflow(paramByteBuffer, i, m, paramCharBuffer, j, 4);
  76. i1 = arrayOfByte[(i + 1)];
  77. i2 = arrayOfByte[(i + 2)];
  78. int i3 = arrayOfByte[(i + 3)];
  79. k = (k & 0x7) << 18 | (i1 & 0x3F) << 12 | (i2 & 0x3F) << 6 | i3 & 0x3F;
  80. if ((isMalformed4(i1, i2, i3)) || (!Surrogate.neededFor(k)))
  81. return malformed(paramByteBuffer, i, paramCharBuffer, j, 4);
  82. i1 = j + 1;
  83. arrayOfChar[j] = Surrogate.high(k);
  84. j = i1 + 1;
  85. arrayOfChar[i1] = Surrogate.low(k);
  86. i += 4;
  87. continue;
  88. }
  89. return malformed(paramByteBuffer, i, paramCharBuffer, j, 1);
  90. }
  91. return xflow(paramByteBuffer, i, m, paramCharBuffer, j, 0);
  92. }
  93. }
  94. private static final boolean isMalformed2(int paramInt1, int paramInt2)
  95. {
  96. return ((paramInt1 & 0x1E) == 0) || ((paramInt2 & 0xC0) != 128);
  97. }
  98. private static boolean isMalformed3(int paramInt1, int paramInt2, int paramInt3)
  99. {
  100. return ((paramInt1 == -32) && ((paramInt2 & 0xE0) == 128)) || ((paramInt2 & 0xC0) != 128) || ((paramInt3 & 0xC0) != 128);
  101. }
  102. private static final boolean isMalformed4(int paramInt1, int paramInt2, int paramInt3)
  103. {
  104. return ((paramInt1 & 0xC0) != 128) || ((paramInt2 & 0xC0) != 128) || ((paramInt3 & 0xC0) != 128);
  105. }
  106. private static boolean isNotContinuation(int paramInt)
  107. {
  108. return (paramInt & 0xC0) != 128;
  109. }
  110. private static CoderResult lookupN(ByteBuffer paramByteBuffer, int paramInt)
  111. {
  112. int i = 1;
  113. while (i < paramInt)
  114. {
  115. if (isNotContinuation(paramByteBuffer.get()))
  116. return CoderResult.malformedForLength(i);
  117. i += 1;
  118. }
  119. return CoderResult.malformedForLength(paramInt);
  120. }
  121. private static CoderResult malformed(ByteBuffer paramByteBuffer, int paramInt1, CharBuffer paramCharBuffer, int paramInt2, int paramInt3)
  122. {
  123. paramByteBuffer.position(paramInt1 - paramByteBuffer.arrayOffset());
  124. CoderResult localCoderResult = malformedN(paramByteBuffer, paramInt3);
  125. updatePositions(paramByteBuffer, paramInt1, paramCharBuffer, paramInt2);
  126. return localCoderResult;
  127. }
  128. public static CoderResult malformedN(ByteBuffer paramByteBuffer, int paramInt)
  129. {
  130. int i = 2;
  131. switch (paramInt)
  132. {
  133. default:
  134. throw new IllegalStateException();
  135. case 1:
  136. paramInt = paramByteBuffer.get();
  137. if (paramInt >> 2 == -2)
  138. {
  139. if (paramByteBuffer.remaining() < 4)
  140. return CoderResult.UNDERFLOW;
  141. return lookupN(paramByteBuffer, 5);
  142. }
  143. if (paramInt >> 1 == -2)
  144. {
  145. if (paramByteBuffer.remaining() < 5)
  146. return CoderResult.UNDERFLOW;
  147. return lookupN(paramByteBuffer, 6);
  148. }
  149. return CoderResult.malformedForLength(1);
  150. case 2:
  151. return CoderResult.malformedForLength(1);
  152. case 3:
  153. paramInt = paramByteBuffer.get();
  154. int j = paramByteBuffer.get();
  155. if ((paramInt != -32) || ((j & 0xE0) != 128))
  156. {
  157. paramInt = i;
  158. if (!isNotContinuation(j));
  159. }
  160. else
  161. {
  162. paramInt = 1;
  163. }
  164. return CoderResult.malformedForLength(paramInt);
  165. case 4:
  166. }
  167. paramInt = paramByteBuffer.get() & 0xFF;
  168. i = paramByteBuffer.get() & 0xFF;
  169. if ((paramInt > 244) || ((paramInt == 240) && ((i < 144) || (i > 191))) || ((paramInt == 244) && ((i & 0xF0) != 128)) || (isNotContinuation(i)))
  170. return CoderResult.malformedForLength(1);
  171. if (isNotContinuation(paramByteBuffer.get()))
  172. return CoderResult.malformedForLength(2);
  173. return CoderResult.malformedForLength(3);
  174. }
  175. static final void updatePositions(Buffer paramBuffer1, int paramInt1, Buffer paramBuffer2, int paramInt2)
  176. {
  177. paramBuffer1.position(paramInt1);
  178. paramBuffer2.position(paramInt2);
  179. }
  180. private static CoderResult xflow(Buffer paramBuffer1, int paramInt1, int paramInt2, Buffer paramBuffer2, int paramInt3, int paramInt4)
  181. {
  182. updatePositions(paramBuffer1, paramInt1, paramBuffer2, paramInt3);
  183. if ((paramInt4 == 0) || (paramInt2 - paramInt1 < paramInt4))
  184. return CoderResult.UNDERFLOW;
  185. return CoderResult.OVERFLOW;
  186. }
  187. protected CoderResult decodeLoop(ByteBuffer paramByteBuffer, CharBuffer paramCharBuffer)
  188. {
  189. return decodeArrayLoop(paramByteBuffer, paramCharBuffer);
  190. }
  191. private static class Surrogate
  192. {
  193. public static final int UCS4_MAX = 1114111;
  194. public static final int UCS4_MIN = 65536;
  195. static
  196. {
  197. if (!UTF8Decoder.class.desiredAssertionStatus());
  198. for (boolean bool = true; ; bool = false)
  199. {
  200. $assertionsDisabled = bool;
  201. return;
  202. }
  203. }
  204. public static char high(int paramInt)
  205. {
  206. assert (neededFor(paramInt));
  207. return (char)(0xD800 | paramInt - 65536 >> 10 & 0x3FF);
  208. }
  209. public static char low(int paramInt)
  210. {
  211. assert (neededFor(paramInt));
  212. return (char)(0xDC00 | paramInt - 65536 & 0x3FF);
  213. }
  214. public static boolean neededFor(int paramInt)
  215. {
  216. return (paramInt >= 65536) && (paramInt <= 1114111);
  217. }
  218. }
  219. }
  220. /* Location: C:\Users\User\dex2jar-2.0\dex\qting\classes-dex2jar.jar
  221. * Qualified Name: com.alibaba.fastjson.util.UTF8Decoder
  222. * JD-Core Version: 0.6.2
  223. */