Adding code to check the end of buffer
Fixed replace cp classname code, and added code to guard against too much fail if the java file is mangled