📄 bytearraybuffer.java
字号:
package cn.ialvin.web.upload;
import java.util.Arrays;
import java.util.LinkedList;
public class ByteArrayBuffer {
public static void main(String[] a) {
ByteArrayBuffer array = new ByteArrayBuffer(0);
array.append(new byte[] { 1, 2, 3, 4, 5 });
array.append(new byte[] { 11, 13, 25 });
System.out.println(Arrays.toString(array.subArray(0, array.length)));
array.set(0, (byte) 18);
System.out.println(Arrays.toString(array.subArray(0, array.length)));
byte[] bbs = array.shift(2);
System.out.println(array.length());
bbs = array.shift(4);
System.out.println(Arrays.toString(bbs));
System.out.println(array.length());
System.out.println(Arrays.toString(array.subArray(0, array.length())));
array.shift(array.length());
System.out.println(Arrays.toString(array.subArray(0, array.length())));
}
private void _shift(int l) {
while (l > this.blockSize) {
l -= this.blockSize;
this.queue.removeFirst();
this.blockNum--;
}
this.blankH = l;
}
public byte[] shift(int l) {
synchronized (this.lock) {
if (l < 0 || l > this.length)
throw new ArrayIndexOutOfBoundsException();
byte[] res = new byte[l];
for (int i = 0; i < l; i++) {
res[i] = this._get(i);
}
this.length -= l;
l += this.blankH;
this._shift(l);
return res;
}
}
private LinkedList<byte[]> queue = new LinkedList<byte[]>();
private int blockSize = 1024;
private int length = 0;
private int blockNum = 0;
private int blankR = 0;
private int blankH = 0;
private Object lock = new Object();
public ByteArrayBuffer(int blockSize) {
if (blockSize > 0)
this.blockSize = blockSize;
this.appendBlock();
}
private byte[] appendBlock() {
byte[] bits = new byte[this.blockSize];
this.queue.add(bits);
this.blockNum++;
this.blankR = this.blockSize;
return bits;
}
public void append(byte[] arr, int len) {
synchronized (this.lock) {
byte[] rear = this.queue.getLast();
for (int i = 0; i < len; i++) {
if (this.blankR == 0)
rear = this.appendBlock();
rear[blockSize - this.blankR] = arr[i];
this.blankR--;
this.length++;
}
}
}
private byte _get(int i) {
i += this.blankH;
return this.queue.get(i / this.blockSize)[i % this.blockSize];
}
public byte get(int i) {
synchronized (this.lock) {
if (i < 0 || i >= this.length)
throw new ArrayIndexOutOfBoundsException();
return this._get(i);
}
}
public boolean set(int i, byte b) {
synchronized (this.lock) {
if (i < 0 || i >= this.length)
throw new ArrayIndexOutOfBoundsException();
i += this.blankH;
this.queue.get(i / this.blockSize)[i % this.blockSize] = b;
return true;
}
}
public int indexOf(byte[] b) {
return this.indexOf(b, 0);
}
public int indexOf(byte[] b, int pos) {
if (b.length < 1)
return -1;
if (pos < 0) pos = 0;
if (b.length + pos > this.length)
return -1;
int sKey = 0;
synchronized (this.lock) {
int v;
while (this.length > pos) {
v = this._get(pos);
if (v == b[sKey]) {
sKey++;
if (sKey == b.length)
return pos - sKey + 1;
} else if (v == b[0]) {
sKey = 1;
} else {
sKey = 0;
}
pos++;
}
return -1;
}
}
public void each(ByteEnumerator be, int offset, int len) {
synchronized (this.lock) {
if (offset + len > this.length || len < 0)
throw new ArrayIndexOutOfBoundsException();
for (int i = 0; i < len; i++, offset++)
be.forEach(this._get(offset));
}
}
public byte[] subArray(int offset, int len) {
synchronized (this.lock) {
if (offset + len > this.length || len < 0)
throw new ArrayIndexOutOfBoundsException();
byte[] bits = new byte[len];
for (int i = 0; i < len; i++, offset++)
bits[i] = this._get(offset);
return bits;
}
}
public int length() {
return this.length;
}
public void append(byte[] arr) {
this.append(arr, arr.length);
}
public void append(byte b) {
byte[] bs = new byte[1];
bs[0] = b;
this.append(bs);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -