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

cn.tom.protocol.co.Message Maven / Gradle / Ivy

The newest version!
package cn.tom.protocol.co;

import java.io.IOException;

import org.xerial.snappy.Snappy;

import cn.tom.kit.IoBuffer;

public class Message {
	
	private int minCompress = 1024;  // 最小到达压缩值
	
	private Header header;

	private int fieldSize;
	private Field[] fields;

	private Row body;

	public void initLength() {
		header.setLength(bodyLen());
	}
	
	public void initCompressLength(int bodyLen) {
		header.setLength(bodyLen);
	}

	public int bodyLen() {
		if(fieldSize >0){
			int size = 2;  // short.bytes fieldSize
			for (Field field : fields) {
				size += field.getFieldLen();
			}
			size += body.getLength();
			return size;
		}
		return 0;
	}

	public IoBuffer toBytes() {
		initLength();
		IoBuffer buffer = null;
		byte[] cbyte = compress();  // 压缩
		if(cbyte == null){
			header.setCompress((byte)0);
			buffer = IoBuffer.allocate(header.getLength() + 5);
			header.toBytes(buffer); // header 
			if(fieldSize > 0 ) {
				buffer.writeShort((short)fieldSize);  // fieldsize
				
				for (Field field : fields) {    // fields
					field.toBytes(buffer);
				}
				body.toBytes(buffer);    // body
			}
			
		}else{
			header.setCompress((byte)1);
			initCompressLength(cbyte.length);
			buffer = IoBuffer.allocate(header.getLength() + 5);
			header.toBytes(buffer); // header 
			buffer.writeBytes(cbyte); // fields+body
		}
		
		buffer.flip();
		
		return buffer;
	}
	
	public  IoBuffer toBodyBytes() {
		
		IoBuffer buffer = IoBuffer.allocate(bodyLen());

		buffer.writeShort((short)fieldSize);  // fieldsize
		
		for (Field field : fields) {    // fields
			field.toBytes(buffer);
		}
		body.toBytes(buffer);    // body
		
		return buffer;
	}
	
	
	public byte[] compress(){
		if(header.getCompress() == 1  && header.getLength() > minCompress){  // 启动压缩
			IoBuffer buf = toBodyBytes();
			byte[] cbytes = null;
			try {
				cbytes = Snappy.compress(buf.array());
			} catch (IOException e) {
				throw new RuntimeException(e);
			}
			return cbytes;
		}
		return null;
	}
	
	public byte[] uncompress(byte [] cbyte){
		byte[] unbyte = null;
		try {
			unbyte = Snappy.uncompress(cbyte);
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
		return unbyte;
	}


	public Header getHeader() {
		return header;
	}

	public void setHeader(Header header) {
		this.header = header;
	}

	public Field[] getFields() {
		return fields;
	}

	public void setFields(Field... fields) {
		this.fields = fields;
		this.fieldSize = fields.length;
	}

	public Row getBody() {
		return body;
	}

	public void setBody(Row body) {
		this.body = body;
	}

	public int getFieldSize() {
		return fieldSize;
	}

	public void setFieldSize(int fieldSize) {
		this.fieldSize = fieldSize;
	}

	public int getMinCompress() {
		return minCompress;
	}

	public void setMinCompress(int minCompress) {
		this.minCompress = minCompress;
	}
	
	
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy