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

com.jfireframework.jnet.common.decodec.impl.FixLengthDecodec Maven / Gradle / Ivy

The newest version!
package com.jfireframework.jnet.common.decodec.impl;

import com.jfireframework.baseutil.collection.buffer.ByteBuf;
import com.jfireframework.baseutil.collection.buffer.DirectByteBuf;
import com.jfireframework.jnet.common.decodec.DecodeResult;
import com.jfireframework.jnet.common.decodec.DecodeResultType;
import com.jfireframework.jnet.common.decodec.FrameDecodec;

public class FixLengthDecodec implements FrameDecodec
{
	private final int			frameLength;
	private final DecodeResult	result	= new DecodeResult();
	
	/**
	 * 固定长度解码器
	 * 
	 * @param frameLength 一个报文的固定长度
	 */
	public FixLengthDecodec(int frameLength)
	{
		this.frameLength = frameLength;
	}
	
	@Override
	public DecodeResult decodec(ByteBuf ioBuf)
	{
		if (ioBuf.remainRead() < frameLength)
		{
			result.setType(DecodeResultType.BUF_NOT_ENOUGH);
			result.setNeed(frameLength);
			return result;
		}
		ByteBuf buf = DirectByteBuf.allocate(frameLength);
		buf.put(ioBuf, frameLength);
		ioBuf.addReadIndex(frameLength);
		result.setType(DecodeResultType.NORMAL);
		result.setBuf(buf);
		return result;
	}
	
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy