Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  //
  //  ========================================================================
  //  Copyright (c) 1995-2012 Mort Bay Consulting Pty. Ltd.
  //  ------------------------------------------------------------------------
  //  All rights reserved. This program and the accompanying materials
  //  are made available under the terms of the Eclipse Public License v1.0
  //  and Apache License v2.0 which accompanies this distribution.
  //
  //      The Eclipse Public License is available at
 //      http://www.eclipse.org/legal/epl-v10.html
 //
 //      The Apache License v2.0 is available at
 //      http://www.opensource.org/licenses/apache2.0.php
 //
 //  You may elect to redistribute this code under either of these licenses.
 //  ========================================================================
 //
 
 package org.eclipse.jetty.io;
 
 
 
 /* ------------------------------------------------------------------------------- */
Stores a collection of Buffer objects. Buffers are stored in an ordered collection and can retreived by index or value
 
 public class BufferCache
 {
     private final HashMap _bufferMap=new HashMap();
     private final StringMap _stringMap=new StringMap(.);
     private final ArrayList _indexnew ArrayList();
 
     /* ------------------------------------------------------------------------------- */
    
Add a buffer to the cache at the specified index.

Parameters:
value The content of the buffer.
 
     public CachedBuffer add(String valueint ordinal)
     {
         CachedBuffer buffernew CachedBuffer(valueordinal);
         .put(bufferbuffer);
         .put(valuebuffer);
         while ((ordinal - .size()) >= 0)
             .add(null);
         if (.get(ordinal)==null)
             .add(ordinalbuffer);
         return buffer;
     }
 
     public CachedBuffer get(int ordinal)
     {
         if (ordinal < 0 || ordinal >= .size())
             return null;
         return (CachedBuffer).get(ordinal);
     }
 
     public CachedBuffer get(Buffer buffer)
     {
         return (CachedBuffer).get(buffer);
     }
 
     public CachedBuffer get(String value)
     {
         return (CachedBuffer).get(value);
     }
 
     public Buffer lookup(Buffer buffer)
     {
         if (buffer instanceof CachedBuffer)
             return buffer;
         
         Buffer bget(buffer);
         if (b == null)
         {
             if (buffer instanceof Buffer.CaseInsensitve)
                 return buffer;
             return new ByteArrayBuffer.CaseInsensitive(buffer.asArray(),0,buffer.length(),.);
         }
 
         return b;
     }
     
     public CachedBuffer getBest(byte[] valueint offsetint maxLength)
     {
         Entry entry = .getBestEntry(valueoffsetmaxLength);
         if (entry!=null)
             return (CachedBuffer)entry.getValue();
         return null;
     }
 
     public Buffer lookup(String value)
     {
         Buffer bget(value);
         if (b == null)
        {
            return new CachedBuffer(value,-1);
        }
        return b;
    }
    public String toString(Buffer buffer)
    {
        return lookup(buffer).toString();
    }
    public int getOrdinal(String value)
    {
        CachedBuffer buffer = (CachedBuffer).get(value);
        return buffer==null?-1:buffer.getOrdinal();
    }
    
    public int getOrdinal(Buffer buffer)
    {
        if (buffer instanceof CachedBuffer)
            return ((CachedBuffer)buffer).getOrdinal();
        buffer=lookup(buffer);
        if (buffer!=null && buffer instanceof CachedBuffer)
            return ((CachedBuffer)buffer).getOrdinal();
        return -1;
    }
    
    public static class CachedBuffer extends ByteArrayBuffer.CaseInsensitive
    {
        private final int _ordinal;
        private HashMap _associateMap=null;
        
        public CachedBuffer(String valueint ordinal)
        {
            super(value);
            ordinal;
        }
        public int getOrdinal()
        {
            return ;
        }
        public CachedBuffer getAssociate(Object key)
        {
            if (==null)
                return null;
            return (CachedBuffer).get(key);
        }
        // TODO Replace Associate with a mime encoding specific solution
        public void setAssociate(Object keyCachedBuffer associate)
        {
            if (==null)
                =new HashMap();
            .put(key,associate);
        }
    }
    
    
    @Override
    public String toString()
    {
        return "CACHE["+
        	"bufferMap="++
        	",stringMap="++
        	",index="++
        	"]";
    }
New to GrepCode? Check out our FAQ X