LimeWire Collection Component API

org.limewire.collection
Class ByteArrayCache

java.lang.Object
  extended by org.limewire.collection.ByteArrayCache

public class ByteArrayCache
extends Object

A cache of byte[]. Sets a limit to the number of byte arrays that can be created. ByteArrayCache has methods to create and release the byte arrays, as well as check the size (total size, remaining size, and byte array size).

When the maximum number of byte arrays are created (getCreated() == getMaxSize()), attempts to get a new byte array (get()) block until a byte array is freed (release(byte[])).

    ByteArrayCache bac = new ByteArrayCache(3, 2);

    byte[] b1 = bac.getQuick();
    byte[] b2 = bac.getQuick();

    for(int i = 0; i < b1.length; i++)
        b1[i] = 1;

    System.arraycopy(b1, 0, b2, 0, b1.length);
    for(int i = 0; i < b2.length; i++)
        System.out.println("b2[" + i + "]: " + b2[i]);

    Output:
        b2[0]: 1
        b2[1]: 1


Constructor Summary
ByteArrayCache()
          Constructs a new ByteArraCache using the default size of 512 & length of 1024
ByteArrayCache(int maxSize, int length)
          Constructs a new ByteArrayCache using the given maxSize & length.
 
Method Summary
 void clear()
          Clears all items in the cache.
 byte[] get()
          Attempts to retrieve a new byte[].
 int getCacheSize()
          Returns the size of all cached bytes.
 int getCreated()
          Returns the number of byte[]'s this cache has created.
 int getLength()
          Returns the length of the byte[]'s this creates.
 int getMaxSize()
          Returns the maximum number of byte[]'s this will create.
 byte[] getQuick()
          Attempts to retrieve a new byte[].
 boolean isBufferAvailable()
          Determines if there's space in the cache for another byte[].
 void release(byte[] data)
          Returns a byte[] to this cache.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ByteArrayCache

public ByteArrayCache()
Constructs a new ByteArraCache using the default size of 512 & length of 1024


ByteArrayCache

public ByteArrayCache(int maxSize,
                      int length)
Constructs a new ByteArrayCache using the given maxSize & length.

Method Detail

getCacheSize

public int getCacheSize()
Returns the size of all cached bytes.


get

public byte[] get()
           throws InterruptedException
Attempts to retrieve a new byte[]. If the cache is empty and getCreated() is >= getMaxSize(), this will block until a byte[] is returned to the cache.

Throws:
InterruptedException

getQuick

public byte[] getQuick()
Attempts to retrieve a new byte[]. If the cache is empty and getCreated() is >= getMaxSize(), this will return null.


release

public void release(byte[] data)
Returns a byte[] to this cache. The byte[] MUST HAVE BEEN A byte[] RETURNED FROM get().


clear

public void clear()
Clears all items in the cache.


isBufferAvailable

public boolean isBufferAvailable()
Determines if there's space in the cache for another byte[].


getCreated

public int getCreated()
Returns the number of byte[]'s this cache has created.


getLength

public int getLength()
Returns the length of the byte[]'s this creates.


getMaxSize

public int getMaxSize()
Returns the maximum number of byte[]'s this will create.


LimeWire Collection Component API

Copyright © 2008 Lime Wire LLC. All Rights Reserved.