byteUtils共有
1588 ワード
ネットでbyteを操作するツールクラスを探して、自分で修正しました.主にbyteを動的に追加できる機能を実現し、StringbufferのようなbyteBuffer
package utils;
import java.util.Arrays;
/**
* byte , StringBuffer
* @author wangkexi
* @version 2015 11 19 3:44:50
*/
public class ByteUtils {
private int size;
private byte[] buffer;
public byte[] getBuffer() {
if(size>buffer.length){
return buffer;
}else{
return Arrays.copyOf(buffer,size);
}
}
private final int kBufferSizeIncrease = 512;
private final int kDefaultBufferSize = 512;
public ByteUtils() {
buffer = new byte[kDefaultBufferSize];
size = 0;
}
public long getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
public ByteUtils append(byte[] buf, int length) {
if (size + length > buffer.length) {
buffer = Arrays.copyOf(buffer, buffer.length + kBufferSizeIncrease);
}
System.arraycopy(buf, 0, buffer, size, length);
size += length;
return this;
}
public ByteUtils append(Byte byte1){
if (size + 1 > buffer.length) {
buffer = Arrays.copyOf(buffer, buffer.length + kBufferSizeIncrease);
}
buffer[size]=byte1;
size +=1;
return this;
}
public void erase(int begin, int count) {
if (begin + count > size) {
//Log.i("erase begin + count > size",".");
size = begin;
} else {
//Log.i("erase else",".");
int startIndex = begin + count;
System.arraycopy(buffer, startIndex, buffer, begin, size-startIndex);
size -= count;
}
}
public void clear()
{
buffer = new byte[kDefaultBufferSize];
size = 0;
}
}