comparison src/org/eclipse/jetty/io/StringCache.java @ 1022:3718afd99988

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