View Javadoc

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