Mercurial Hosting > luan
annotate src/org/eclipse/jetty/io/StringCache.java @ 1042:a62193321d6a
remove Buffer.reset()
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Fri, 04 Nov 2016 01:03:25 -0600 |
parents | 27f3dc761452 |
children |
rev | line source |
---|---|
1019
f126d30e04a4
start replacing BufferCache with StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
1 // to replace BufferCache |
f126d30e04a4
start replacing BufferCache with StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
2 |
f126d30e04a4
start replacing BufferCache with StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
3 package org.eclipse.jetty.io; |
f126d30e04a4
start replacing BufferCache with StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
4 |
1022
3718afd99988
HttpHeaders uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1020
diff
changeset
|
5 import java.util.Map; |
1019
f126d30e04a4
start replacing BufferCache with StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
6 import java.util.HashMap; |
1022
3718afd99988
HttpHeaders uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1020
diff
changeset
|
7 import java.util.TreeMap; |
1019
f126d30e04a4
start replacing BufferCache with StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
8 import org.eclipse.jetty.util.StringUtil; |
f126d30e04a4
start replacing BufferCache with StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
9 |
f126d30e04a4
start replacing BufferCache with StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
10 |
1020
6be43ef1eb96
HttpHeaderValues uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1019
diff
changeset
|
11 public final class StringCache { |
1019
f126d30e04a4
start replacing BufferCache with StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
12 private final Map<String,Integer> stringToOrdinal = new HashMap<String,Integer>(); |
f126d30e04a4
start replacing BufferCache with StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
13 // private final Map<Integer,String> ordinalToString = new HashMap<Integer,String>(); |
1022
3718afd99988
HttpHeaders uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1020
diff
changeset
|
14 private final TreeMap<String,String> stringMap = new TreeMap<String,String>(); |
1019
f126d30e04a4
start replacing BufferCache with StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
15 |
f126d30e04a4
start replacing BufferCache with StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
16 public byte[] add(String value, int ordinal) { |
1022
3718afd99988
HttpHeaders uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1020
diff
changeset
|
17 String valueLower = value.toLowerCase(); |
3718afd99988
HttpHeaders uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1020
diff
changeset
|
18 stringToOrdinal.put(valueLower, ordinal); |
1019
f126d30e04a4
start replacing BufferCache with StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
19 // ordinalToString.put(ordinal, value); |
1022
3718afd99988
HttpHeaders uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1020
diff
changeset
|
20 stringMap.put(valueLower,value); |
1019
f126d30e04a4
start replacing BufferCache with StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
21 return StringUtil.getBytes(value); |
f126d30e04a4
start replacing BufferCache with StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
22 } |
f126d30e04a4
start replacing BufferCache with StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
23 |
f126d30e04a4
start replacing BufferCache with StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
24 public int getOrdinal(String value) { |
f126d30e04a4
start replacing BufferCache with StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
25 Integer i = stringToOrdinal.get(value.toLowerCase()); |
f126d30e04a4
start replacing BufferCache with StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
26 return i==null ? -1 : i; |
f126d30e04a4
start replacing BufferCache with StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
27 } |
f126d30e04a4
start replacing BufferCache with StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
28 |
f126d30e04a4
start replacing BufferCache with StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
29 public boolean contains(String value) { |
f126d30e04a4
start replacing BufferCache with StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
30 return stringToOrdinal.containsKey(value.toLowerCase()); |
f126d30e04a4
start replacing BufferCache with StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
31 } |
1022
3718afd99988
HttpHeaders uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1020
diff
changeset
|
32 |
1023
27f3dc761452
MimeTypes uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1022
diff
changeset
|
33 public String get(String value) |
27f3dc761452
MimeTypes uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1022
diff
changeset
|
34 { |
27f3dc761452
MimeTypes uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1022
diff
changeset
|
35 return stringMap.get(value.toLowerCase()); |
27f3dc761452
MimeTypes uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1022
diff
changeset
|
36 } |
27f3dc761452
MimeTypes uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1022
diff
changeset
|
37 |
1022
3718afd99988
HttpHeaders uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1020
diff
changeset
|
38 public String getBest(String key) { |
3718afd99988
HttpHeaders uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1020
diff
changeset
|
39 key = key.toLowerCase(); |
3718afd99988
HttpHeaders uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1020
diff
changeset
|
40 String rtn = stringMap.get(key); |
3718afd99988
HttpHeaders uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1020
diff
changeset
|
41 if( rtn != null ) |
3718afd99988
HttpHeaders uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1020
diff
changeset
|
42 return rtn; |
3718afd99988
HttpHeaders uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1020
diff
changeset
|
43 Map.Entry<String,String> floor = stringMap.floorEntry(key); |
3718afd99988
HttpHeaders uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1020
diff
changeset
|
44 Map.Entry<String,String> ceiling = stringMap.ceilingEntry(key); |
3718afd99988
HttpHeaders uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1020
diff
changeset
|
45 if( floor==null ) { |
3718afd99988
HttpHeaders uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1020
diff
changeset
|
46 if( ceiling==null ) |
3718afd99988
HttpHeaders uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1020
diff
changeset
|
47 return null; |
3718afd99988
HttpHeaders uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1020
diff
changeset
|
48 String ceilingKey = ceiling.getKey(); |
3718afd99988
HttpHeaders uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1020
diff
changeset
|
49 return key.charAt(0) == ceilingKey.charAt(0) ? ceiling.getValue() : null; |
3718afd99988
HttpHeaders uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1020
diff
changeset
|
50 } else { |
3718afd99988
HttpHeaders uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1020
diff
changeset
|
51 String floorKey = (String)floor.getKey(); |
3718afd99988
HttpHeaders uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1020
diff
changeset
|
52 if( ceiling==null ) |
3718afd99988
HttpHeaders uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1020
diff
changeset
|
53 return key.charAt(0) == floorKey.charAt(0) ? floor.getValue() : null; |
3718afd99988
HttpHeaders uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1020
diff
changeset
|
54 String ceilingKey = ceiling.getKey(); |
3718afd99988
HttpHeaders uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1020
diff
changeset
|
55 int n = Math.min( key.length(), Math.min( floorKey.length(), ceilingKey.length() ) ); |
3718afd99988
HttpHeaders uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1020
diff
changeset
|
56 int i = 0; |
3718afd99988
HttpHeaders uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1020
diff
changeset
|
57 while( ++i <= n && key.regionMatches(0,floorKey,0,i) && key.regionMatches(0,ceilingKey,0,i) ); |
3718afd99988
HttpHeaders uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1020
diff
changeset
|
58 return key.regionMatches(0,floorKey,0,i) ? floor.getValue() : key.regionMatches(0,ceilingKey,0,i) || i > 1 ? ceiling.getValue() : null; |
3718afd99988
HttpHeaders uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1020
diff
changeset
|
59 } |
3718afd99988
HttpHeaders uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1020
diff
changeset
|
60 } |
3718afd99988
HttpHeaders uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1020
diff
changeset
|
61 |
1019
f126d30e04a4
start replacing BufferCache with StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
62 } |