5
5
6
6
public class ThreadLocalCache {
7
7
8
- public final static int CHARS_CACH_INIT_SIZE = 1024 ; // 1k, 2^10;
9
- public final static int CHARS_CACH_INIT_SIZE_EXP = 10 ;
10
- public final static int CHARS_CACH_MAX_SIZE = 1024 * 128 ; // 128k, 2^17;
11
- public final static int CHARS_CACH_MAX_SIZE_EXP = 17 ;
8
+ public final static int CHARS_CACH_INIT_SIZE = 1024 ; // 1k;
9
+ public final static int CHARS_CACH_MAX_SIZE = 1024 * 128 ; // 1k;
12
10
private final static ThreadLocal <SoftReference <char []>> charsBufLocal = new ThreadLocal <SoftReference <char []>>();
13
11
14
12
private final static ThreadLocal <CharsetDecoder > decoderLocal = new ThreadLocal <CharsetDecoder >();
@@ -47,30 +45,17 @@ public static char[] getChars(int length) {
47
45
}
48
46
49
47
private static char [] allocate (int length ) {
50
- if (length > CHARS_CACH_MAX_SIZE ) {
51
- return new char [length ];
52
- }
48
+ int allocateLength = getAllocateLength (CHARS_CACH_INIT_SIZE , CHARS_CACH_MAX_SIZE , length );
53
49
54
- int allocateLength = getAllocateLengthExp ( CHARS_CACH_INIT_SIZE_EXP , CHARS_CACH_MAX_SIZE_EXP , length );
55
- char [] chars = new char [allocateLength ];
56
- charsBufLocal .set (new SoftReference <char []>(chars ));
57
- return chars ;
58
- }
50
+ if ( allocateLength <= CHARS_CACH_MAX_SIZE ) {
51
+ char [] chars = new char [allocateLength ];
52
+ charsBufLocal .set (new SoftReference <char []>(chars ));
53
+ return chars ;
54
+ }
59
55
60
- private static int getAllocateLengthExp (int minExp , int maxExp , int length ) {
61
- assert maxExp >= length ;
62
- // int max = 1 << maxExp;
63
- // if(length>= max) {
64
- // return length;
65
- // }
66
- int part = length >>> minExp ;
67
- if (part <= 0 ) {
68
- return 1 << minExp ;
69
- }
70
- return 1 << 32 - Integer .numberOfLeadingZeros (length -1 );
56
+ return new char [length ];
71
57
}
72
-
73
- @ Deprecated
58
+
74
59
private static int getAllocateLength (int init , int max , int length ) {
75
60
int value = init ;
76
61
for (;;) {
@@ -89,10 +74,8 @@ private static int getAllocateLength(int init, int max, int length) {
89
74
}
90
75
91
76
// /////////
92
- public final static int BYTES_CACH_INIT_SIZE = 1024 ; // 1k, 2^10;
93
- public final static int BYTES_CACH_INIT_SIZE_EXP = 10 ;
94
- public final static int BYTES_CACH_MAX_SIZE = 1024 * 128 ; // 128k, 2^17;
95
- public final static int BYTES_CACH_MAX_SIZE_EXP = 17 ;
77
+ public final static int BYTES_CACH_INIT_SIZE = 1024 ; // 1k;
78
+ public final static int BYTeS_CACH_MAX_SIZE = 1024 * 128 ; // 1k;
96
79
private final static ThreadLocal <SoftReference <byte []>> bytesBufLocal = new ThreadLocal <SoftReference <byte []>>();
97
80
98
81
public static void clearBytes () {
@@ -120,14 +103,15 @@ public static byte[] getBytes(int length) {
120
103
}
121
104
122
105
private static byte [] allocateBytes (int length ) {
123
- if (length > CHARS_CACH_MAX_SIZE ) {
124
- return new byte [length ];
125
- }
106
+ int allocateLength = getAllocateLength (CHARS_CACH_INIT_SIZE , CHARS_CACH_MAX_SIZE , length );
126
107
127
- int allocateLength = getAllocateLengthExp (CHARS_CACH_INIT_SIZE_EXP , CHARS_CACH_MAX_SIZE_EXP , length );
128
- byte [] chars = new byte [allocateLength ];
129
- bytesBufLocal .set (new SoftReference <byte []>(chars ));
130
- return chars ;
108
+ if (allocateLength <= CHARS_CACH_MAX_SIZE ) {
109
+ byte [] chars = new byte [allocateLength ];
110
+ bytesBufLocal .set (new SoftReference <byte []>(chars ));
111
+ return chars ;
112
+ }
113
+
114
+ return new byte [length ];
131
115
}
132
116
133
117
}
0 commit comments