Mercurial Hosting > luan
annotate src/org/eclipse/jetty/io/BufferCache.java @ 889:5731a7707466
remove _forwarded
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Thu, 06 Oct 2016 23:24:30 -0600 |
parents | 3ff59e08a1b7 |
children | 74b9daf2826c |
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) ); | |
837 | 109 return key.regionMatches(0,floorKey,0,i) ? (CachedBuffer)floor.getValue() : key.regionMatches(0,ceilingKey,0,i) || i > 1 ? (CachedBuffer)ceiling.getValue() : null; |
836 | 110 } |
111 /* | |
112 Entry entry = _stringMap.getBestEntry(value, offset, maxLength); | |
113 if (entry!=null) | |
114 return (CachedBuffer)entry.getValue(); | |
115 return null; | |
116 */ | |
117 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
118 |
836 | 119 public Buffer lookup(String value) |
120 { | |
121 Buffer b= get(value); | |
122 if (b == null) | |
123 { | |
124 return new CachedBuffer(value,-1); | |
125 } | |
126 return b; | |
127 } | |
128 | |
129 public String toString(Buffer buffer) | |
130 { | |
131 return lookup(buffer).toString(); | |
132 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
133 |
836 | 134 public int getOrdinal(String value) |
135 { | |
136 CachedBuffer buffer = (CachedBuffer)_stringMap.get(value.toLowerCase()); | |
137 return buffer==null?-1:buffer.getOrdinal(); | |
138 } | |
139 | |
140 public int getOrdinal(Buffer buffer) | |
141 { | |
142 if (buffer instanceof CachedBuffer) | |
143 return ((CachedBuffer)buffer).getOrdinal(); | |
144 buffer=lookup(buffer); | |
145 if (buffer!=null && buffer instanceof CachedBuffer) | |
146 return ((CachedBuffer)buffer).getOrdinal(); | |
147 return -1; | |
148 } | |
149 | |
150 public static class CachedBuffer extends ByteArrayBuffer.CaseInsensitive | |
151 { | |
152 private final int _ordinal; | |
153 private HashMap _associateMap=null; | |
154 | |
155 public CachedBuffer(String value, int ordinal) | |
156 { | |
157 super(value); | |
158 _ordinal= ordinal; | |
159 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
160 |
836 | 161 public int getOrdinal() |
162 { | |
163 return _ordinal; | |
164 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
165 |
836 | 166 public CachedBuffer getAssociate(Object key) |
167 { | |
168 if (_associateMap==null) | |
169 return null; | |
170 return (CachedBuffer)_associateMap.get(key); | |
171 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
172 |
836 | 173 // TODO Replace Associate with a mime encoding specific solution |
174 public void setAssociate(Object key, CachedBuffer associate) | |
175 { | |
176 if (_associateMap==null) | |
177 _associateMap=new HashMap(); | |
178 _associateMap.put(key,associate); | |
179 } | |
180 } | |
181 | |
182 | |
183 @Override | |
184 public String toString() | |
185 { | |
186 return "CACHE["+ | |
187 "bufferMap="+_bufferMap+ | |
188 ",stringMap="+_stringMap+ | |
189 ",index="+_index+ | |
190 "]"; | |
191 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
192 } |