PageRenderTime 51ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/qt-prometheus/qt-prometheus
Java | 116 lines | 101 code | 11 blank | 4 comment | 16 complexity | 0ee129903782f94656200de456a998ea MD5 | raw file
  1. package com.alibaba.fastjson.util;
  2. import java.lang.ref.SoftReference;
  3. import java.nio.charset.CharsetDecoder;
  4. public class ThreadLocalCache
  5. {
  6. public static final int BYTES_CACH_INIT_SIZE = 1024;
  7. public static final int BYTeS_CACH_MAX_SIZE = 131072;
  8. public static final int CHARS_CACH_INIT_SIZE = 1024;
  9. public static final int CHARS_CACH_MAX_SIZE = 131072;
  10. private static final ThreadLocal<SoftReference<byte[]>> bytesBufLocal = new ThreadLocal();
  11. private static final ThreadLocal<SoftReference<char[]>> charsBufLocal = new ThreadLocal();
  12. private static final ThreadLocal<CharsetDecoder> decoderLocal = new ThreadLocal();
  13. private static char[] allocate(int paramInt)
  14. {
  15. int i = getAllocateLength(1024, 131072, paramInt);
  16. if (i <= 131072)
  17. {
  18. char[] arrayOfChar = new char[i];
  19. charsBufLocal.set(new SoftReference(arrayOfChar));
  20. return arrayOfChar;
  21. }
  22. return new char[paramInt];
  23. }
  24. private static byte[] allocateBytes(int paramInt)
  25. {
  26. int i = getAllocateLength(1024, 131072, paramInt);
  27. if (i <= 131072)
  28. {
  29. byte[] arrayOfByte = new byte[i];
  30. bytesBufLocal.set(new SoftReference(arrayOfByte));
  31. return arrayOfByte;
  32. }
  33. return new byte[paramInt];
  34. }
  35. public static void clearBytes()
  36. {
  37. bytesBufLocal.set(null);
  38. }
  39. public static void clearChars()
  40. {
  41. charsBufLocal.set(null);
  42. }
  43. private static int getAllocateLength(int paramInt1, int paramInt2, int paramInt3)
  44. {
  45. int i;
  46. do
  47. {
  48. if (paramInt1 >= paramInt3)
  49. return paramInt1;
  50. i = paramInt1 * 2;
  51. paramInt1 = i;
  52. }
  53. while (i <= paramInt2);
  54. return paramInt3;
  55. }
  56. public static byte[] getBytes(int paramInt)
  57. {
  58. Object localObject = (SoftReference)bytesBufLocal.get();
  59. if (localObject == null)
  60. localObject = allocateBytes(paramInt);
  61. byte[] arrayOfByte;
  62. do
  63. {
  64. return localObject;
  65. arrayOfByte = (byte[])((SoftReference)localObject).get();
  66. if (arrayOfByte == null)
  67. return allocateBytes(paramInt);
  68. localObject = arrayOfByte;
  69. }
  70. while (arrayOfByte.length >= paramInt);
  71. return allocateBytes(paramInt);
  72. }
  73. public static char[] getChars(int paramInt)
  74. {
  75. Object localObject = (SoftReference)charsBufLocal.get();
  76. if (localObject == null)
  77. localObject = allocate(paramInt);
  78. char[] arrayOfChar;
  79. do
  80. {
  81. return localObject;
  82. arrayOfChar = (char[])((SoftReference)localObject).get();
  83. if (arrayOfChar == null)
  84. return allocate(paramInt);
  85. localObject = arrayOfChar;
  86. }
  87. while (arrayOfChar.length >= paramInt);
  88. return allocate(paramInt);
  89. }
  90. public static CharsetDecoder getUTF8Decoder()
  91. {
  92. CharsetDecoder localCharsetDecoder = (CharsetDecoder)decoderLocal.get();
  93. Object localObject = localCharsetDecoder;
  94. if (localCharsetDecoder == null)
  95. {
  96. localObject = new UTF8Decoder();
  97. decoderLocal.set(localObject);
  98. }
  99. return localObject;
  100. }
  101. }
  102. /* Location: C:\Users\User\dex2jar-2.0\dex\qting\classes-dex2jar.jar
  103. * Qualified Name: com.alibaba.fastjson.util.ThreadLocalCache
  104. * JD-Core Version: 0.6.2
  105. */