PageRenderTime 48ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/xiejuntao/xdesktop
Java | 117 lines | 85 code | 31 blank | 1 comment | 17 complexity | 4300b75e8225f5d4130b437d1ad6b1ec 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. public final static int CHARS_CACH_INIT_SIZE = 1024; // 1k;
  6. public final static int CHARS_CACH_MAX_SIZE = 1024 * 128; // 1k;
  7. private final static ThreadLocal<SoftReference<char[]>> charsBufLocal = new ThreadLocal<SoftReference<char[]>>();
  8. private final static ThreadLocal<CharsetDecoder> decoderLocal = new ThreadLocal<CharsetDecoder>();
  9. public static CharsetDecoder getUTF8Decoder() {
  10. CharsetDecoder decoder = decoderLocal.get();
  11. if (decoder == null) {
  12. decoder = new UTF8Decoder();
  13. decoderLocal.set(decoder);
  14. }
  15. return decoder;
  16. }
  17. public static void clearChars() {
  18. charsBufLocal.set(null);
  19. }
  20. public static char[] getChars(int length) {
  21. SoftReference<char[]> ref = charsBufLocal.get();
  22. if (ref == null) {
  23. return allocate(length);
  24. }
  25. char[] chars = ref.get();
  26. if (chars == null) {
  27. return allocate(length);
  28. }
  29. if (chars.length < length) {
  30. chars = allocate(length);
  31. }
  32. return chars;
  33. }
  34. private static char[] allocate(int length) {
  35. int allocateLength = getAllocateLength(CHARS_CACH_INIT_SIZE, CHARS_CACH_MAX_SIZE, length);
  36. if (allocateLength <= CHARS_CACH_MAX_SIZE) {
  37. char[] chars = new char[allocateLength];
  38. charsBufLocal.set(new SoftReference<char[]>(chars));
  39. return chars;
  40. }
  41. return new char[length];
  42. }
  43. private static int getAllocateLength(int init, int max, int length) {
  44. int value = init;
  45. for (;;) {
  46. if (value >= length) {
  47. return value;
  48. }
  49. value *= 2;
  50. if (value > max) {
  51. break;
  52. }
  53. }
  54. return length;
  55. }
  56. // /////////
  57. public final static int BYTES_CACH_INIT_SIZE = 1024; // 1k;
  58. public final static int BYTeS_CACH_MAX_SIZE = 1024 * 128; // 1k;
  59. private final static ThreadLocal<SoftReference<byte[]>> bytesBufLocal = new ThreadLocal<SoftReference<byte[]>>();
  60. public static void clearBytes() {
  61. bytesBufLocal.set(null);
  62. }
  63. public static byte[] getBytes(int length) {
  64. SoftReference<byte[]> ref = bytesBufLocal.get();
  65. if (ref == null) {
  66. return allocateBytes(length);
  67. }
  68. byte[] bytes = ref.get();
  69. if (bytes == null) {
  70. return allocateBytes(length);
  71. }
  72. if (bytes.length < length) {
  73. bytes = allocateBytes(length);
  74. }
  75. return bytes;
  76. }
  77. private static byte[] allocateBytes(int length) {
  78. int allocateLength = getAllocateLength(CHARS_CACH_INIT_SIZE, CHARS_CACH_MAX_SIZE, length);
  79. if (allocateLength <= CHARS_CACH_MAX_SIZE) {
  80. byte[] chars = new byte[allocateLength];
  81. bytesBufLocal.set(new SoftReference<byte[]>(chars));
  82. return chars;
  83. }
  84. return new byte[length];
  85. }
  86. }