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(Buffer buffer)
107     {
108         if (buffer instanceof CachedBuffer)
109             return ((CachedBuffer)buffer).getOrdinal();
110         buffer=lookup(buffer);
111         if (buffer!=null && buffer instanceof CachedBuffer)
112             return ((CachedBuffer)buffer).getOrdinal();
113         return -1;
114     }
115     
116     public static class CachedBuffer extends ByteArrayBuffer.CaseInsensitive
117     {
118         private final int _ordinal;
119         private HashMap _associateMap=null;
120         
121         public CachedBuffer(String value, int ordinal)
122         {
123             super(value);
124             _ordinal= ordinal;
125         }
126 
127         public int getOrdinal()
128         {
129             return _ordinal;
130         }
131 
132         public CachedBuffer getAssociate(Object key)
133         {
134             if (_associateMap==null)
135                 return null;
136             return (CachedBuffer)_associateMap.get(key);
137         }
138 
139         // TODO Replace Associate with a mime encoding specific solution
140         public void setAssociate(Object key, CachedBuffer associate)
141         {
142             if (_associateMap==null)
143                 _associateMap=new HashMap();
144             _associateMap.put(key,associate);
145         }
146     }
147     
148     
149     @Override
150     public String toString()
151     {
152         return "CACHE["+
153         	"bufferMap="+_bufferMap+
154         	",stringMap="+_stringMap+
155         	",index="+_index+
156         	"]";
157     }
158 }