link.jfire.socket.socketserver.util.CheckReadBuffer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jfire-socket Show documentation
Show all versions of jfire-socket Show documentation
Jfire - socket is a server-side framework based on AIO. Users only need a simple implementation of a business logic processing interface can be the business data processing. The framework provides the client and server at the same time. Have strong connection capacity. Single server provides tens of thousands of connections.
The newest version!
package link.jfire.socket.socketserver.util;
import java.nio.ByteBuffer;
import link.jfire.socket.socketserver.exception.LessThanProtocolException;
import link.jfire.socket.socketserver.exception.NotFitProtocolException;
import link.jfire.socket.socketserver.exception.NotIntactDataException;
public class CheckReadBuffer
{
/**
* 对buffer从posi开始的11个字节进行检查,如果检查通过,返回报文体的长度。否则抛出异常。
* 检查不会对buffer的位置信息等造成改变
*
* @param buffer
* @param headFactory
* @return
* @throws LessThanProtocolException
* @throws NotFitProtocolException
* @throws NotIntactDataException
* @author windfire([email protected])
*/
public static int checkReadBuffer(ByteBuffer buffer, HeadFactory headFactory) throws LessThanProtocolException, NotFitProtocolException, NotIntactDataException
{
int posi = buffer.position();
if (buffer.remaining() < 11)
{
throw new LessThanProtocolException();
}
if (headFactory.fitHeadPtotocol(buffer, posi) == false)
{
throw new NotFitProtocolException();
}
int length = buffer.getInt(posi + 7);
if (length > buffer.remaining() - 11)
{
throw new NotIntactDataException(length);
}
return length;
}
}