org.limewire.collection
Class PowerOf2ByteArrayCache
java.lang.Object
org.limewire.collection.PowerOf2ByteArrayCache
public class PowerOf2ByteArrayCache
- extends java.lang.Object
Creates a byte array with a size that is a power of 2 (byte array size = 2^power).
If you try to get a byte array with a size that is not a power of two, the value
is set to the next power of 2. For example, setting a value of 511 returns a byte
array of size 512 because 511 is not a power of 2 (2^8 = 256 and 2^9 = 512).
(If you attempt to create a byte array less than a size zero (base 2^-2),
the size is set to 1.)
Additionally, PowerOf2ByteArrayCache stores the total size of
cached byte[]s.
PowerOf2ByteArrayCache p2 = new PowerOf2ByteArrayCache();
byte[] ba4 = p2.get(4);
System.out.println("ba4 size: " + ba4.length + " cache size is " + p2.getCacheSize());
byte[] ba5 = p2.get(5);
System.out.println("ba5 size: " + ba5.length + " cache size is " + p2.getCacheSize());
byte[] ba8 = p2.get(8);
System.out.println("ba8 size: " + ba8.length + " cache size is " + p2.getCacheSize());
byte[] ba9 = p2.get(9);
System.out.println("ba9 size: " + ba9.length + " cache size is " + p2.getCacheSize());
Output:
ba4 size: 4 cache size is 4
ba5 size: 8 cache size is 12
ba8 size: 8 cache size is 12
ba9 size: 16 cache size is 28
| Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
PowerOf2ByteArrayCache
public PowerOf2ByteArrayCache()
get
public byte[] get(int size)
- Returns:
- a byte array of the specified size, using cached one
if possible.
getCacheSize
public int getCacheSize()
clear
public void clear()
- Erases all data in the cache.
Copyright © 2009 Lime Wire LLC. All Rights Reserved.