All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net_io.core.ByteArray Maven / Gradle / Ivy

The newest version!
package net_io.core;

import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.ArrayList;
import java.util.List;

import net_io.msg.MsgReadWrite;

public class ByteArray {
	/**
	 * 低字节在前。
	 */
	private static final ByteOrder ENDIAN = ByteOrder.LITTLE_ENDIAN;
	/**
	 * ByteArray编码类型。
	 */
	public static final String CHARSET = "utf-8";
	/** 最大单次读取长度 **/
	public static final int MAX_LENGTH = 1024 * 1024 * 8; 

	private ByteBuffer buff;
	private int maxCapacity;
		
	public ByteArray() {
		buff = ByteBuffer.allocate(8192);
		buff.order(ENDIAN);
		this.maxCapacity = MAX_LENGTH;		
	}
	
	public ByteArray(int capacity) {
		buff = ByteBuffer.allocate(capacity);
		buff.order(ENDIAN);
		this.maxCapacity = MAX_LENGTH;
	}
	
	public ByteArray(int capacity, int maxCapacity) {
		buff = ByteBuffer.allocate(capacity);
		buff.order(ENDIAN);
		this.maxCapacity = maxCapacity;
	}

	public ByteArray(ByteBuffer buff) {
		this.buff = buff;
		buff.order(ENDIAN);
		this.maxCapacity = buff.capacity();
	}
	
	public ByteBuffer getByteBuffer() {
		return buff;
	}
	
	public void setByteBuffer(ByteBuffer buff) {
		buff.order(this.buff.order());
		this.buff = buff;
	}
	
	/** 获取字节顺序 **/
	public ByteOrder getOrder() {
		return buff.order();
	}
	
	/** 设置字节顺序 **/
	public void setOrder(ByteOrder order) {
		buff.order(order);
	}
	
	public static ByteArray wrap(String str, String charset) throws UnsupportedEncodingException {
		byte[] bytes = str.getBytes(charset);
		ByteArray arr = new ByteArray(ByteBuffer.wrap(bytes));
		return arr;
	}
	
	/**
	 * 复制 源ByteBuffer 中的剩余字节到
	 * @param src 源ByteBuffer
	 * @return 非空ByteArray
	 */
	public static ByteArray copy(ByteBuffer src) {
		int srcPostion = src.position();
		ByteBuffer dst = ByteBuffer.allocate(src.remaining());
		dst.put(src);
		src.position(srcPostion);
		return new ByteArray(dst);
	}
	
	public int size() {
		return buff.limit();
	}
	public int position() {
		return buff.position();
	}
	public void position(int position) {
		buff.position(position);
	}
	public void append(ByteArray data) {
		this.append(data.getByteBuffer());
	}
	public void append(ByteBuffer buff) {
		int remainingNum = buff.remaining();
		autoIncreaseBuff(remainingNum); //检查并自动扩充数组
		this.buff.put(buff);
	}
	public void rewind() {
		buff.rewind();
	}
	public boolean hasRemaining() {
		return buff.hasRemaining();
	}
	/**
	 * 写入完成时调用
	 */
	public void finishWrite() {
		buff.flip();
	}
	public byte readInt8() {
		return buff.get();
	}
	public byte readByte() {
		return buff.get();
	}
	
	public short readInt16() {
		return buff.getShort();
	}
	
	public int readInt32() {
		return buff.getInt();
	}
	
	public long readInt64() {
		return buff.getLong();
	}
	
	public short readUInt8() {
		return (short)(buff.get() & 0xFF);
	}
	
	public int readUInt16() {
		return buff.getShort() & 0xFFFF;
	}
	
	public long readUInt32() {
		return buff.getInt() & 0xFFFFFFFFL;
	}
	
	public long readUInt64() {
		return buff.getLong();
	}
	public boolean readBoolean() {
		return buff.get() != 0;
	}
	
	public byte[] readBytes() {
		return readBytesDirect(buff.getInt());
	}
	
	public void readBytesTo(byte[] dst) {
		buff.get(dst);
	}
	
	public byte[] readBytesDirect(int size) {
		if(size > MAX_LENGTH) {
			throw new IllegalArgumentException("read size overflow: "+size);
		}
		byte[] data = new byte[size];
		for(int i=0; i List readArray(Class itemType) {
		int count = buff.getInt();
		if(count <= 0) {
			return null;
		}
		List list = new ArrayList();
		for(int i=0; i void writeArray(List list) {
		if(list == null || list.size() == 0) {
			writeInt32(0);
			return;
		}
		buff.putInt(list.size());
		for(int i=0; i MAX_LENGTH) {
			throw new IllegalArgumentException("read size overflow: "+bytes);
		}
		byte[] arr = new byte[bytes];
		buff.get(arr);
//		int i = 0;
//		for(byte b : arr) {
//			if(b == 0) {
//				break;
//			}
//			i++;
//		}
		try {
//			return new String(arr, 0, i, CHARSET);
			return new String(arr, CHARSET);
		} catch (UnsupportedEncodingException e) {
			throw new RuntimeException("UnsupportedEncodingException: "+e.getMessage());
		}
	}
	
	public String readString() {
		int bytes = buff.getInt(); //字符数量
		if(bytes <= 0) {
			return null;
		}
		return readString(bytes);
	}
	
	public void writeString(String str) {
		autoIncreaseBuff(4); //检查并自动扩充数组
		try {
			//空字符串,只写入数量字段
			if(str == null || str.length() == 0) {
				buff.putInt(0);
				return;
			}
			byte[] arr = str.getBytes(CHARSET);
			buff.putInt(arr.length); //字符数量
			autoIncreaseBuff(arr.length); //检查并自动扩充数组
			for(int i = 0; i= needSize) {
			return;
		}
		int minIncrease = needSize - avaliable;
		int maxIncrease = this.maxCapacity - capacity;
		if(minIncrease > maxIncrease) {
			throw new ArrayIndexOutOfBoundsException("ByteArray capacity: "+capacity+", need increase: "+minIncrease);
		}
		int increaseNum = Math.max(Math.round(minIncrease * 1.2f), 1024);
		byte[] dstBytes = new byte[capacity+increaseNum];
		byte[] srcBytes = buff.array();
		System.arraycopy(srcBytes, 0, dstBytes, 0, position);
		ByteBuffer dstBuff = ByteBuffer.wrap(dstBytes);
		dstBuff.position(position);
		dstBuff.order(buff.order());
		buff = dstBuff;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy