19
19
20
20
package org .elasticsearch .common .io .stream ;
21
21
22
- import org .apache .lucene .util .ArrayUtil ;
23
22
import org .apache .lucene .util .BytesRef ;
24
- import org .apache .lucene .util .RamUsageEstimator ;
23
+ import org .apache .lucene .util .CharsRef ;
25
24
import org .elasticsearch .Version ;
26
25
import org .elasticsearch .common .Nullable ;
27
26
import org .elasticsearch .common .Strings ;
34
33
35
34
import java .io .IOException ;
36
35
import java .io .InputStream ;
37
- import java .lang .ref .SoftReference ;
38
36
import java .util .*;
39
37
40
38
/**
41
39
*
42
40
*/
43
41
public abstract class StreamInput extends InputStream {
44
42
45
- private static final ThreadLocal <SoftReference <char []>> charCache = new ThreadLocal <>();
46
-
47
- private static char [] charCache (int size ) {
48
- SoftReference <char []> ref = charCache .get ();
49
- char [] arr = (ref == null ) ? null : ref .get ();
50
- if (arr == null || arr .length < size ) {
51
- arr = new char [ArrayUtil .oversize (size , RamUsageEstimator .NUM_BYTES_CHAR )];
52
- charCache .set (new SoftReference <>(arr ));
53
- }
54
- return arr ;
55
- }
56
-
57
43
private Version version = Version .CURRENT ;
58
44
59
45
public Version getVersion () {
@@ -268,11 +254,15 @@ public String readOptionalSharedString() throws IOException {
268
254
return null ;
269
255
}
270
256
257
+ private final CharsRef spare = new CharsRef ();
258
+
271
259
public String readString () throws IOException {
272
- int charCount = readVInt ();
273
- char [] chars = charCache (charCount );
274
- int c , charIndex = 0 ;
275
- while (charIndex < charCount ) {
260
+ final int charCount = readVInt ();
261
+ spare .offset = 0 ;
262
+ spare .length = 0 ;
263
+ spare .grow (charCount );
264
+ int c = 0 ;
265
+ while (spare .length < charCount ) {
276
266
c = readByte () & 0xff ;
277
267
switch (c >> 4 ) {
278
268
case 0 :
@@ -283,18 +273,18 @@ public String readString() throws IOException {
283
273
case 5 :
284
274
case 6 :
285
275
case 7 :
286
- chars [charIndex ++] = (char ) c ;
276
+ spare . chars [spare . length ++] = (char ) c ;
287
277
break ;
288
278
case 12 :
289
279
case 13 :
290
- chars [charIndex ++] = (char ) ((c & 0x1F ) << 6 | readByte () & 0x3F );
280
+ spare . chars [spare . length ++] = (char ) ((c & 0x1F ) << 6 | readByte () & 0x3F );
291
281
break ;
292
282
case 14 :
293
- chars [charIndex ++] = (char ) ((c & 0x0F ) << 12 | (readByte () & 0x3F ) << 6 | (readByte () & 0x3F ) << 0 );
283
+ spare . chars [spare . length ++] = (char ) ((c & 0x0F ) << 12 | (readByte () & 0x3F ) << 6 | (readByte () & 0x3F ) << 0 );
294
284
break ;
295
285
}
296
286
}
297
- return new String ( chars , 0 , charCount );
287
+ return spare . toString ( );
298
288
}
299
289
300
290
public String readSharedString () throws IOException {
0 commit comments