Mercurial Hosting > luan
annotate src/org/eclipse/jetty/io/BufferCache.java @ 836:e161dca40760
remove StringMap
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Fri, 16 Sep 2016 12:35:17 -0600 |
parents | 3428c60d7cfc |
children | 3ff59e08a1b7 |
rev | line source |
---|---|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
1 // |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
2 // ======================================================================== |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
3 // Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd. |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
4 // ------------------------------------------------------------------------ |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
5 // All rights reserved. This program and the accompanying materials |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
6 // are made available under the terms of the Eclipse Public License v1.0 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
7 // and Apache License v2.0 which accompanies this distribution. |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
8 // |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
9 // The Eclipse Public License is available at |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
10 // http://www.eclipse.org/legal/epl-v10.html |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
11 // |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
12 // The Apache License v2.0 is available at |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
13 // http://www.opensource.org/licenses/apache2.0.php |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
14 // |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
15 // You may elect to redistribute this code under either of these licenses. |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
16 // ======================================================================== |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
17 // |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
18 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
19 package org.eclipse.jetty.io; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
20 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
21 import java.util.ArrayList; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
22 import java.util.HashMap; |
836 | 23 import java.util.Map; |
24 import java.util.TreeMap; | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
25 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
26 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
27 /* ------------------------------------------------------------------------------- */ |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
28 /** |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
29 * Stores a collection of {@link Buffer} objects. |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
30 * Buffers are stored in an ordered collection and can retreived by index or value |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
31 * |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
32 */ |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
33 public class BufferCache |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
34 { |
836 | 35 private final HashMap _bufferMap=new HashMap(); |
36 private final TreeMap _stringMap = new TreeMap(); | |
37 private final ArrayList _index= new ArrayList(); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
38 |
836 | 39 /* ------------------------------------------------------------------------------- */ |
40 /** Add a buffer to the cache at the specified index. | |
41 * @param value The content of the buffer. | |
42 */ | |
43 public CachedBuffer add(String value, int ordinal) | |
44 { | |
45 CachedBuffer buffer= new CachedBuffer(value, ordinal); | |
46 _bufferMap.put(buffer, buffer); | |
47 _stringMap.put(value.toLowerCase(), buffer); | |
48 while ((ordinal - _index.size()) >= 0) | |
49 _index.add(null); | |
50 if (_index.get(ordinal)==null) | |
51 _index.add(ordinal, buffer); | |
52 return buffer; | |
53 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
54 |
836 | 55 public CachedBuffer get(int ordinal) |
56 { | |
57 if (ordinal < 0 || ordinal >= _index.size()) | |
58 return null; | |
59 return (CachedBuffer)_index.get(ordinal); | |
60 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
61 |
836 | 62 public CachedBuffer get(Buffer buffer) |
63 { | |
64 return (CachedBuffer)_bufferMap.get(buffer); | |
65 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
66 |
836 | 67 public CachedBuffer get(String value) |
68 { | |
69 return (CachedBuffer)_stringMap.get(value.toLowerCase()); | |
70 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
71 |
836 | 72 public Buffer lookup(Buffer buffer) |
73 { | |
74 if (buffer instanceof CachedBuffer) | |
75 return buffer; | |
76 | |
77 Buffer b= get(buffer); | |
78 if (b == null) | |
79 { | |
80 if (buffer instanceof Buffer.CaseInsensitve) | |
81 return buffer; | |
82 return new ByteArrayBuffer.CaseInsensitive(buffer.asArray(),0,buffer.length(),Buffer.IMMUTABLE); | |
83 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
84 |
836 | 85 return b; |
86 } | |
87 | |
88 public CachedBuffer getBest(byte[] value, int offset, int maxLength) | |
89 { | |
90 String key = new String(value,offset,maxLength).toLowerCase(); | |
91 CachedBuffer buffer = (CachedBuffer)_stringMap.get(key); | |
92 if( buffer != null ) | |
93 return buffer; | |
94 Map.Entry floor = _stringMap.floorEntry(key); | |
95 Map.Entry ceiling = _stringMap.ceilingEntry(key); | |
96 if( floor==null ) { | |
97 if( ceiling==null ) | |
98 return null; | |
99 String ceilingKey = (String)ceiling.getKey(); | |
100 return key.charAt(0) == ceilingKey.charAt(0) ? (CachedBuffer)ceiling.getValue() : null; | |
101 } else { | |
102 String floorKey = (String)floor.getKey(); | |
103 if( ceiling==null ) | |
104 return key.charAt(0) == floorKey.charAt(0) ? (CachedBuffer)floor.getValue() : null; | |
105 String ceilingKey = (String)ceiling.getKey(); | |
106 int n = Math.min( key.length(), Math.min( floorKey.length(), ceilingKey.length() ) ); | |
107 int i = 0; | |
108 while( ++i <= n && key.regionMatches(0,floorKey,0,i) && key.regionMatches(0,ceilingKey,0,i) ); | |
109 i--; | |
110 if( i==0 ) | |
111 return null; | |
112 return i==0 ? null : key.regionMatches(0,floorKey,0,i) ? (CachedBuffer)floor.getValue() : (CachedBuffer)ceiling.getValue(); | |
113 } | |
114 /* | |
115 Entry entry = _stringMap.getBestEntry(value, offset, maxLength); | |
116 if (entry!=null) | |
117 return (CachedBuffer)entry.getValue(); | |
118 return null; | |
119 */ | |
120 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
121 |
836 | 122 public Buffer lookup(String value) |
123 { | |
124 Buffer b= get(value); | |
125 if (b == null) | |
126 { | |
127 return new CachedBuffer(value,-1); | |
128 } | |
129 return b; | |
130 } | |
131 | |
132 public String toString(Buffer buffer) | |
133 { | |
134 return lookup(buffer).toString(); | |
135 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
136 |
836 | 137 public int getOrdinal(String value) |
138 { | |
139 CachedBuffer buffer = (CachedBuffer)_stringMap.get(value.toLowerCase()); | |
140 return buffer==null?-1:buffer.getOrdinal(); | |
141 } | |
142 | |
143 public int getOrdinal(Buffer buffer) | |
144 { | |
145 if (buffer instanceof CachedBuffer) | |
146 return ((CachedBuffer)buffer).getOrdinal(); | |
147 buffer=lookup(buffer); | |
148 if (buffer!=null && buffer instanceof CachedBuffer) | |
149 return ((CachedBuffer)buffer).getOrdinal(); | |
150 return -1; | |
151 } | |
152 | |
153 public static class CachedBuffer extends ByteArrayBuffer.CaseInsensitive | |
154 { | |
155 private final int _ordinal; | |
156 private HashMap _associateMap=null; | |
157 | |
158 public CachedBuffer(String value, int ordinal) | |
159 { | |
160 super(value); | |
161 _ordinal= ordinal; | |
162 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
163 |
836 | 164 public int getOrdinal() |
165 { | |
166 return _ordinal; | |
167 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
168 |
836 | 169 public CachedBuffer getAssociate(Object key) |
170 { | |
171 if (_associateMap==null) | |
172 return null; | |
173 return (CachedBuffer)_associateMap.get(key); | |
174 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
175 |
836 | 176 // TODO Replace Associate with a mime encoding specific solution |
177 public void setAssociate(Object key, CachedBuffer associate) | |
178 { | |
179 if (_associateMap==null) | |
180 _associateMap=new HashMap(); | |
181 _associateMap.put(key,associate); | |
182 } | |
183 } | |
184 | |
185 | |
186 @Override | |
187 public String toString() | |
188 { | |
189 return "CACHE["+ | |
190 "bufferMap="+_bufferMap+ | |
191 ",stringMap="+_stringMap+ | |
192 ",index="+_index+ | |
193 "]"; | |
194 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
195 } |