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          Buffer b= get(buffer);
70          if (b == null)
71          {
72              if (buffer instanceof Buffer.CaseInsensitve)
73                  return buffer;
74              return new View.CaseInsensitive(buffer);
75          }
76  
77          return b;
78      }
79      
80      public CachedBuffer getBest(byte[] value, int offset, int maxLength)
81      {
82          Entry entry = _stringMap.getBestEntry(value, offset, maxLength);
83          if (entry!=null)
84              return (CachedBuffer)entry.getValue();
85          return null;
86      }
87  
88      public Buffer lookup(String value)
89      {
90          Buffer b= get(value);
91          if (b == null)
92          {
93              return new CachedBuffer(value,-1);
94          }
95          return b;
96      }
97  
98      public String toString(Buffer buffer)
99      {
100         return lookup(buffer).toString();
101     }
102 
103     public int getOrdinal(Buffer buffer)
104     {
105         if (buffer instanceof CachedBuffer)
106             return ((CachedBuffer)buffer).getOrdinal();
107         buffer=lookup(buffer);
108         if (buffer!=null && buffer instanceof CachedBuffer)
109             return ((CachedBuffer)buffer).getOrdinal();
110         return -1;
111     }
112     
113     public static class CachedBuffer extends ByteArrayBuffer.CaseInsensitive
114     {
115         private final int _ordinal;
116         private HashMap _associateMap=null;
117         
118         public CachedBuffer(String value, int ordinal)
119         {
120             super(value);
121             _ordinal= ordinal;
122         }
123 
124         public int getOrdinal()
125         {
126             return _ordinal;
127         }
128 
129         public CachedBuffer getAssociate(Object key)
130         {
131             if (_associateMap==null)
132                 return null;
133             return (CachedBuffer)_associateMap.get(key);
134         }
135 
136         // TODO Replace Associate with a mime encoding specific solution
137         public void setAssociate(Object key, CachedBuffer associate)
138         {
139             if (_associateMap==null)
140                 _associateMap=new HashMap();
141             _associateMap.put(key,associate);
142         }
143     }
144     
145     
146     @Override
147     public String toString()
148     {
149         return "CACHE["+
150         	"bufferMap="+_bufferMap+
151         	",stringMap="+_stringMap+
152         	",index="+_index+
153         	"]";
154     }
155 }