final int endOfLength = buf.indexOf(12, buf.readableBytes(), (byte) '\01');
if(endOfLength==-1){
if(buf.readableBytes()>maxlength){
//too many characters read, but no length field found
throw new TooLongFrameException("End of length field not found within "+(maxlength+12)+" bytes");
}
//end of length field not found
return null;
}
final String lengthStr = buf.slice(12, endOfLength-12).toString(Charset.forName("US-ASCII"));
final int length = Integer.parseInt(lengthStr);
final int totalLength = length+endOfLength+8;
if(totalLength>9999){
throw new TooLongFrameException("Frame length may not be greater than 9999 bytes");
}
if (buf.readableBytes() < totalLength) {
return null;
}