📄 bytebuffer.java
字号:
package com.reddragon2046.base.utilities.data.adapters;
import com.reddragon2046.base.utilities.data.*;
import java.util.Iterator;
import java.util.NoSuchElementException;
// Referenced classes of package com.reddragon2046.base.utilities.data.adapters:
// AbstractArrayAdapter, ByteArray, ByteIterator, AbstractArrayIterator,
// Algorithms
public class ByteBuffer extends AbstractArrayAdapter
{
public ByteBuffer()
{
clear();
}
public ByteBuffer(int size)
{
if(size < 0)
{
throw new IllegalArgumentException("Attempt to create an ByteBuffer with a negative size");
} else
{
length = size;
storage = new byte[length];
return;
}
}
public ByteBuffer(int size, byte object)
{
this(size);
for(int i = 0; i < length; i++)
storage[i] = object;
}
public ByteBuffer(byte array[])
{
this(array, true);
}
ByteBuffer(byte array[], boolean copyBuffer)
{
synchronized(array)
{
length = array.length;
if(copyBuffer)
{
storage = new byte[length];
System.arraycopy(array, 0, storage, 0, length);
} else
{
storage = array;
}
}
}
public ByteBuffer(ByteBuffer array)
{
this(array.storage, true);
length = array.length;
}
public synchronized Object clone()
{
return new ByteBuffer(this);
}
public boolean equals(Object object)
{
return (object instanceof ByteBuffer) && equals((ByteBuffer)object) || (object instanceof ByteArray) && equals((ByteArray)object);
}
public synchronized boolean equals(ByteBuffer buffer)
{
return ByteArray.sliceEquals(storage, length, buffer.storage, buffer.length);
}
public boolean equals(ByteArray array)
{
return equals(array.array);
}
public synchronized boolean equals(byte array[])
{
return ByteArray.sliceEquals(storage, length, array, array.length);
}
public synchronized String toString()
{
return Algorithms.Printing.toString(this, "ByteBuffer");
}
public synchronized void copy(ByteBuffer buffer)
{
if(this == buffer)
return;
synchronized(buffer)
{
if(buffer.length > storage.length)
{
storage = buffer.get();
} else
{
System.arraycopy(buffer.storage, 0, storage, 0, buffer.length);
for(int i = buffer.length; i < length; i++)
storage[i] = 0;
}
length = buffer.length;
}
}
public synchronized void copyTo(byte array[])
{
synchronized(array)
{
System.arraycopy(storage, 0, array, 0, Math.min(length, array.length));
}
}
public synchronized byte[] get()
{
byte data[] = new byte[length];
copyTo(data);
return data;
}
public synchronized int hashCode()
{
return Algorithms.Hashing.orderedHash(begin(), length);
}
public int size()
{
return length;
}
public int capacity()
{
return storage.length;
}
public int maxSize()
{
return 0x7fffffff;
}
public Object get(int index)
{
return new Byte(byteAt(index));
}
public synchronized byte byteAt(int index)
{
AbstractArrayAdapter.checkIndex(index, length);
return storage[index];
}
public synchronized void put(int index, Object object)
{
put(index, asByte(object));
}
public synchronized void put(int index, byte object)
{
AbstractArrayAdapter.checkIndex(index, length);
storage[index] = object;
}
public synchronized void clear()
{
storage = new byte[10];
length = 0;
}
public synchronized Object remove(int index)
{
AbstractArrayAdapter.checkIndex(index, length);
Object retval = new Byte(storage[index]);
System.arraycopy(storage, index + 1, storage, index, length - index - 1);
storage[--length] = 0;
return retval;
}
public synchronized int remove(int first, int last)
{
if(last < first)
return 0;
AbstractArrayAdapter.checkRange(first, last, length);
int amount = (last - first) + 1;
System.arraycopy(storage, last + 1, storage, first, length - last - 1);
for(int i = length - amount; i < length; i++)
storage[i] = 0;
length -= amount;
return amount;
}
public synchronized Object popBack()
{
if(length == 0)
{
throw new NoSuchElementException("ByteBuffer is empty");
} else
{
Object r = new Byte(storage[--length]);
storage[length] = 0;
return r;
}
}
public synchronized boolean add(Object object)
{
add(asByte(object));
return true;
}
public synchronized void add(byte object)
{
if(length == storage.length)
{
byte tmp[] = getNextStorage(1);
copyTo(tmp);
storage = tmp;
}
storage[length++] = object;
}
public void pushBack(Object object)
{
add(asByte(object));
}
public void pushBack(byte object)
{
add(object);
}
public ByteIterator insert(ByteIterator pos, Object object)
{
return insert(pos, asByte(object));
}
public ByteIterator insert(ByteIterator pos, byte object)
{
insert(pos.getInternalIndex(), object);
return new ByteIterator(this, pos.getInternalIndex());
}
public void insert(int index, Object object)
{
insert(index, asByte(object));
}
public synchronized void insert(int index, byte object)
{
AbstractArrayAdapter.checkIndex(index, length + 1);
if(length != storage.length)
{
if(index != length)
System.arraycopy(storage, index, storage, index + 1, length - index);
} else
{
byte tmp[] = getNextStorage(1);
System.arraycopy(storage, 0, tmp, 0, index);
System.arraycopy(storage, index, tmp, index + 1, length - index);
storage = tmp;
}
storage[index] = object;
length++;
}
public void insert(ByteIterator pos, int n, Object object)
{
insert(pos, n, asByte(object));
}
public void insert(ByteIterator pos, int n, byte object)
{
insert(pos.getInternalIndex(), n, object);
}
public void insert(int index, int n, Object object)
{
insert(index, n, asByte(object));
}
public synchronized void insert(int index, int n, byte object)
{
if(n < 0)
throw new IllegalArgumentException("Attempt to insert a negative number of objects.");
if(n == 0)
return;
AbstractArrayAdapter.checkIndex(index, length + 1);
if(storage.length - length >= n)
{
System.arraycopy(storage, index, storage, index + n, length - index);
} else
{
byte tmp[] = getNextStorage(n);
System.arraycopy(storage, 0, tmp, 0, index);
System.arraycopy(storage, index, tmp, index + n, length - index);
storage = tmp;
}
for(int i = index; i < index + n; i++)
storage[i] = object;
length += n;
}
public void insert(ByteIterator pos, ForwardIterator first, ForwardIterator last)
{
insert(pos.getInternalIndex(), first, last);
}
public synchronized void insert(int index, ForwardIterator first, ForwardIterator last)
{
int n = first.distance(last);
if(n == 0)
return;
ForwardIterator firstx = (ForwardIterator)first.clone();
if(storage.length - length >= n)
{
System.arraycopy(storage, index, storage, index + n, length - index);
} else
{
byte tmp[] = getNextStorage(n);
System.arraycopy(storage, 0, tmp, 0, index);
System.arraycopy(storage, index, tmp, index + n, length - index);
storage = tmp;
}
length += n;
for(int i = index; i < index + n; i++)
put(i, firstx.next());
}
public synchronized void swap(ByteBuffer array)
{
synchronized(array)
{
int oldLength = length;
byte oldStorage[] = storage;
length = array.length;
storage = array.storage;
array.length = oldLength;
array.storage = oldStorage;
}
}
public Iterator iterator()
{
return begin();
}
public synchronized BidirectionalIterator begin()
{
return new ByteIterator(this, 0);
}
public synchronized BidirectionalIterator end()
{
return new ByteIterator(this, length);
}
public synchronized void trimToSize()
{
if(length < storage.length)
storage = get();
}
public synchronized void ensureCapacity(int n)
{
if(n < 0)
throw new IllegalArgumentException("Attempt to reserve a negative size.");
if(storage.length < n)
{
byte tmp[] = new byte[n];
if(length > 0)
System.arraycopy(storage, 0, tmp, 0, length);
storage = tmp;
}
}
public synchronized Object popFront()
{
if(length == 0)
{
throw new NoSuchElementException("ByteBuffer is empty");
} else
{
Object result = new Byte(storage[0]);
remove(0);
return result;
}
}
public void pushFront(Object object)
{
insert(0, object);
}
public void pushFront(byte object)
{
insert(0, object);
}
public boolean remove(Object object)
{
return remove(object, 1) > 0;
}
public synchronized int remove(Object object, int count)
{
byte tmp = asByte(object);
int removed = 0;
while(count > 0)
{
int i = indexOf(tmp);
if(i < 0)
break;
count--;
removed++;
remove(i);
}
return removed;
}
public synchronized int remove(int first, int last, Object object)
{
asByte(object);
if(last < first)
{
return 0;
} else
{
AbstractArrayAdapter.checkRange(first, last, length);
ByteIterator firstx = new ByteIterator(this, first);
ByteIterator lastx = new ByteIterator(this, last + 1);
ByteIterator finish = (ByteIterator)Algorithms.Removing.remove(firstx, lastx, object);
return remove(finish.getInternalIndex(), last);
}
}
public int replace(Object oldValue, Object newValue)
{
return replace(asByte(oldValue), asByte(newValue));
}
public int replace(byte oldValue, byte newValue)
{
return replace(0, length, oldValue, newValue);
}
public int replace(int first, int last, Object oldValue, Object newValue)
{
return replace(first, last, asByte(oldValue), asByte(newValue));
}
public synchronized int replace(int first, int last, byte oldValue, byte newValue)
{
AbstractArrayAdapter.checkRange(first, last - 1, length);
int n = 0;
for(; first < last; first++)
if(storage[first] == oldValue)
{
storage[first] = newValue;
n++;
}
return n;
}
public int count(Object object)
{
return count(asByte(object));
}
public int count(byte object)
{
return count(0, length - 1, object);
}
public int count(int first, int last, Object object)
{
return count(first, last, asByte(object));
}
public synchronized int count(int first, int last, byte object)
{
AbstractArrayAdapter.checkRange(first, last, length);
int n = 0;
for(; first < last; first++)
if(storage[first] == object)
n++;
return n;
}
public int indexOf(Object object)
{
return indexOf(asByte(object));
}
public int indexOf(byte object)
{
return indexOf(0, length - 1, object);
}
public int indexOf(int first, int last, Object object)
{
return indexOf(first, last, asByte(object));
}
public synchronized int indexOf(int first, int last, byte object)
{
if(last < first)
return -1;
AbstractArrayAdapter.checkRange(first, last, length);
for(; first <= last; first++)
if(storage[first] == object)
return first;
return -1;
}
public void setSize(int newSize)
{
if(newSize < 0)
throw new IllegalArgumentException("Attempt to become a negative size.");
if(length > newSize)
remove(newSize, length - 1);
else
if(length < newSize)
insert(length, newSize - length, (byte)0);
}
public boolean contains(Object object)
{
return contains(asByte(object));
}
public boolean contains(byte object)
{
return indexOf(object) != -1;
}
private byte[] getNextStorage(int n)
{
int newSize = Math.max(AbstractArrayAdapter.getNextSize(length), length + n);
byte tmp[] = new byte[newSize];
return tmp;
}
static byte asByte(Object object)
{
return ((Number)object).byteValue();
}
static final byte defaultValue = 0;
byte storage[];
int length;
static final long serialVersionUID = 0x3d3c3551b3901bdcL;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -